-
Notifications
You must be signed in to change notification settings - Fork 0
/
lasso.min.js
2 lines (2 loc) · 8.4 KB
/
lasso.min.js
1
2
/*! lasso-string 2017-09-01 */
!function(){function Lasso(string){this.value=string}function lasso(string){return new Lasso(string)}function isArray(array){return Object.prototype.toString.call(array)===arrayTag}function map(array,iterator){var i=0,n=array.length,val=[];if(isArray(array))for(;i<n;i++)val.push(iterator(array[i],i));return val}function between(a,b,string,index){var start,alen,blen,t,o=0,i=index||0,n=string.length;if(a instanceof RegExp){if(!(t=string.match(a)))return!1;a=t[0]}if(b instanceof RegExp){if(!(t=string.match(b)))return!1;b=t[0]}for(alen=a.length,blen=b.length;string[i]&&string.substring(i,i+alen)!==a;)i++;for(start=i;i<n;i++)if(string.substring(i,i+alen)===a?o+=1:string.substring(i,i+blen)===b&&(o-=1),0===o)return{start:start+alen,end:i,length:i-start,value:string.substring(start+alen,i)};return!1}function camelCase(string){return string.length?string.replace(/[-\._]+|\s+/g," ").trim().replace(/[A-Z][a-z0-9]+/g,function(a,i){return i>0?" "+a:a}).match(/[A-Za-z0-9 ]/g).join("").split(" ").map(function(a,i){return 0===i?a.toLowerCase():capitalCase(a)}).join(""):string}function capitalCase(string){return map(string.trim().split(" "),function(a){return/^[A-Z]/.test(a)?a:a[0].toUpperCase()+a.substr(1).toLowerCase()}).join(" ")}function differentWords(a,b){var index,i,n,z=[];for(a=a.match(/[a-zA-Z0-9 ]+/g).join(" ").replace(/[ ]+/g," ").split(" "),b=b.match(/[a-zA-Z0-9 ]+/g).join(" ").replace(/[ ]+/g," ").split(" "),i=0,n=a.length;i<n;i++)for(index=b.indexOf(a[i]),-1===index&&z.push(a[i]);index>-1;)b.splice(index,1),index=b.indexOf(a[i]);for(i=0,n=b.length;i<n;i++)for(index=a.indexOf(b[i]),-1===index&&-1===z.indexOf(b[i])&&z.push(b[i]);index>-1;)a.splice(index,1),index=a.indexOf(b[i]);return z}function distance(a,b){var i,j,matrix=[];if(0===a.length)return b.length;if(0===b.length)return a.length;for(i=0;i<=b.length;i++)matrix[i]=[i];for(j=0;j<=a.length;j++)matrix[0][j]=j;for(i=1;i<=b.length;i++)for(j=1;j<=a.length;j++)b.charAt(i-1)===a.charAt(j-1)?matrix[i][j]=matrix[i-1][j-1]:matrix[i][j]=Math.min(matrix[i-1][j-1]+1,matrix[i][j-1]+1,matrix[i-1][j]+1);return matrix[b.length][a.length]}function ellipsis(string,length){return string?string.length>length?trimEnd(string.substr(0,length))+"...":string:""}function fuzzy(a,b){var o,index=a.indexOf(b[0]),opt=[],n=0;for(opt.distance=0,opt.closest=a.length,opt.farthest=0;index>-1;)o={index:index+n,length:1,match:b[opt.length]},opt.push(o),opt.distance=o.index-opt[0].index,n=index,index=a.substr(n,a.length-n).indexOf(b[opt.length]),index>-1&&index+n-o.index<opt.closest&&(opt.closest=index+n-o.index),n-index>opt.farthest&&(opt.farthest=n-index);return opt.length===b.length&&(opt.difference=opt.farthest-opt.closest,opt)}function group(string){var s=string.toString().match(/[\d\.]/g).join("").split("."),n=s[0].replace(/\s/g,"").split("").reverse();if(n.length>3)for(var i=n.length;i>=0;i--)i<n.length&&i>0&&i%3==0&&n.splice(i,0,",");return 2===s.length?n.reverse().join("")+"."+s.slice(1).join("."):n.reverse().join("")}function indexesOf(string,match){function isRegularExpression(){for(var matched=match.exec(string.substr(0,max));matched;)currentIndex=matched.index,indexes.push({index:index+currentIndex,length:matched[0].length,match:matched[0]}),index+=currentIndex+matched[0].length,matched=match.exec(string.substr(index,max-index))}function isString(){for(currentIndex=string.substr(index,max-index).indexOf(match);-1!==currentIndex&&index<max;)indexes.push({index:index+currentIndex,length:match.length,match:match}),index+=currentIndex+match.length,currentIndex=string.substr(index,max-index).indexOf(match)}var currentIndex,index=0,indexes=[],max=string.length;if(2!==arguments.length)throw"Error (lasso.indexesOf): Missing Arguments";return"string"==typeof match?isString():"function"==typeof match.test&&isRegularExpression(),indexes}function len(string){return string.length}function matchType(string){function typeIndex(chr){return/[a-zA-Z ]/.test(chr)?0:/[0-9]/.test(chr)?1:/[\-\_]/.test(chr)?2:/[\.\,\;\:\?\!]/.test(chr)?3:/[\$\%\^\*\#\@\&\+\=]/.test(chr)?4:/[\[\]\{\}\(\)<>\"\']/.test(chr)?5:6}var type,match=[""],matchIndex=0;type=typeIndex(string[0]);for(var i=0,n=string.length;i<n;i++)type!==typeIndex(string[i])&&(match.push(""),matchIndex+=1),match[matchIndex]+=string[i],type=typeIndex(string[i]);return match}function sameWords(a,b){var index,x,y,i,n,same=[];for(a=a.match(/[a-zA-Z0-9 ]+/g).join(" ").replace(/[ ]+/g," ").split(" "),b=b.match(/[a-zA-Z0-9 ]+/g).join(" ").replace(/[ ]+/g," ").split(" "),x=a,y=b,a.length<b.length&&(x=b,y=a),i=0,n=x.length;i<n;i++)for(index=y.indexOf(x[i]),index>-1&&same.push(x[i]);index>-1;)y.splice(index,1),index=y.indexOf(x[i]);return same}function splice(string,start,length,newString){return string.substr(0,start)+newString+string.substr(start+length,string.length-start-length)}function template(string){for(var i=0,n=arguments.length-1,a=new Array(n);i<n;i++)a[i]=arguments[i+1];return i=0,string.replace(/(?:%s|%([0-9]+))/g,function(x,b){return i+=1,b?a[Number(b)]:a[i-1]})}function toChar(code){return Array.isArray(code)?code.map(function(a){return String.fromCharCode(a)}).join(""):String.fromCharCode(code)}function toCharCode(string){return Array.prototype.map.call(string.split(""),function(a){return a.charCodeAt(0)})}function toCurrency(prefix,value){return 1===arguments.length&&(value=prefix,prefix="$"),value<0||"-"===value.toString()[0]?"-"+prefix+lasso.group((-1*Number(value)).toFixed(2)):prefix+lasso.group(Number(value).toFixed(2))}function toPercentage(value){return value&&value!==1/0||(value=0),value+"%"}function trimEnd(string){return string.replace(/\s+$/,"")}function trimStart(string){return string.replace(/^\s+/,"")}function trimStartUntil(str,match){if("string"==typeof match)for(;str[0]!==match&&str.length;)str=str.substr(1);else for(;!match.test(str[0])&&str.length;)str=str.substr(1);return str}var arrayTag="[object Array]";Lasso.prototype.between=function(start,end){return this.value=between(start,end,this.value),this},Lasso.prototype.camelCase=function(){return this.value=camelCase(this.value),this},Lasso.prototype.capitalCase=function(){return this.value=capitalCase(this.value),this},Lasso.prototype.differentWords=function(word){return this.value=differentWords(this.value,word),this},Lasso.prototype.distance=function(word){return this.value=distance(this.value,word),this},Lasso.prototype.ellipsis=function(length){return this.value=ellipsis(this.value,length),this},Lasso.prototype.fuzzy=function(word){return this.value=fuzzy(this.value,word),this},Lasso.prototype.group=function(){return this.value=group(this.value),this},Lasso.prototype.indexesOf=function(match){return this.value=indexesOf(this.value,match),this},Lasso.prototype.matchType=function(){return this.value=matchType(this.value),this},Lasso.prototype.sameWords=function(word){return this.value=sameWords(this.value,word),this},Lasso.prototype.splice=function(start,length,newString){return this.value=sameWords(this.value,start,length,newString),this},Lasso.prototype.template=function(){for(var i=0,n=arguments.length,a=new Array(n);i<n;i++)a[i]=arguments[i];return this.value=template.apply(null,[this.value].concat(a)),this},Lasso.prototype.toChar=function(){return this.value=toChar(this.value),this},Lasso.prototype.toCharCode=function(){return this.value=toCharCode(this.value),this},Lasso.prototype.toCurrency=function(symbol){return this.value=toCurrency(symbol,this.value),this},Lasso.prototype.toPercentage=function(){return this.value=toPercentage(this.value),this},Lasso.prototype.trimEnd=function(){return this.value=trimEnd(this.value),this},Lasso.prototype.trimStart=function(){return this.value=trimStart(this.value),this},Lasso.prototype.trimStartUntil=function(until){return this.value=trimStartUntil(this.value,until),this},Lasso.prototype.forEach=function(iterator){this.value=map(this.value,iterator)},Lasso.prototype.len=function(){return this.value=this.value.length,this},lasso.between=between,lasso.camelCase=camelCase,lasso.capitalCase=capitalCase,lasso.differentWords=differentWords,lasso.distance=distance,lasso.ellipsis=ellipsis,lasso.fuzzy=fuzzy,lasso.group=group,lasso.indexesOf=indexesOf,lasso.matchType=matchType,lasso.sameWords=sameWords,lasso.splice=splice,lasso.template=template,lasso.toChar=toChar,lasso.toCharCode=toCharCode,lasso.toCurrency=toCurrency,lasso.toPercentage=toPercentage,lasso.trimEnd=trimEnd,lasso.trimStart=trimStart,lasso.trimStartUntil=trimStartUntil,lasso.len=len,"object"==typeof module?module.exports=lasso:"object"==typeof window&&(window.lasso=lasso)}();