mirror of
https://github.com/zhigang1992/deployd.git
synced 2026-06-04 19:42:50 +08:00
1 line
7.1 KiB
JavaScript
1 line
7.1 KiB
JavaScript
(function(a){"use strict";var b=String.prototype.trim,c=function(a){return a*1||0},d=function(a,b){for(var c=[];b>0;c[--b]=a);return c.join("")},e=function(a){return Array.prototype.slice.call(a)},f=function(a){return a!=null?"["+h.escapeRegExp(""+a)+"]":"\\s"},g=function(){function a(a){return Object.prototype.toString.call(a).slice(8,-1).toLowerCase()}var b=d,c=function(){return c.cache.hasOwnProperty(arguments[0])||(c.cache[arguments[0]]=c.parse(arguments[0])),c.format.call(null,c.cache[arguments[0]],arguments)};return c.format=function(c,d){var e=1,f=c.length,h="",i,j=[],k,l,m,n,o,p;for(k=0;k<f;k++){h=a(c[k]);if(h==="string")j.push(c[k]);else if(h==="array"){m=c[k];if(m[2]){i=d[e];for(l=0;l<m[2].length;l++){if(!i.hasOwnProperty(m[2][l]))throw new Error(g('[_.sprintf] property "%s" does not exist',m[2][l]));i=i[m[2][l]]}}else m[1]?i=d[m[1]]:i=d[e++];if(/[^s]/.test(m[8])&&a(i)!="number")throw new Error(g("[_.sprintf] expecting number but found %s",a(i)));switch(m[8]){case"b":i=i.toString(2);break;case"c":i=String.fromCharCode(i);break;case"d":i=parseInt(i,10);break;case"e":i=m[7]?i.toExponential(m[7]):i.toExponential();break;case"f":i=m[7]?parseFloat(i).toFixed(m[7]):parseFloat(i);break;case"o":i=i.toString(8);break;case"s":i=(i=String(i))&&m[7]?i.substring(0,m[7]):i;break;case"u":i=Math.abs(i);break;case"x":i=i.toString(16);break;case"X":i=i.toString(16).toUpperCase()}i=/[def]/.test(m[8])&&m[3]&&i>=0?"+"+i:i,o=m[4]?m[4]=="0"?"0":m[4].charAt(1):" ",p=m[6]-String(i).length,n=m[6]?b(o,p):"",j.push(m[5]?i+n:n+i)}}return j.join("")},c.cache={},c.parse=function(a){var b=a,c=[],d=[],e=0;while(b){if((c=/^[^\x25]+/.exec(b))!==null)d.push(c[0]);else if((c=/^\x25{2}/.exec(b))!==null)d.push("%");else{if((c=/^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(b))===null)throw new Error("[_.sprintf] huh?");if(c[2]){e|=1;var f=[],g=c[2],h=[];if((h=/^([a-z_][a-z_\d]*)/i.exec(g))===null)throw new Error("[_.sprintf] huh?");f.push(h[1]);while((g=g.substring(h[0].length))!=="")if((h=/^\.([a-z_][a-z_\d]*)/i.exec(g))!==null)f.push(h[1]);else{if((h=/^\[(\d+)\]/.exec(g))===null)throw new Error("[_.sprintf] huh?");f.push(h[1])}c[2]=f}else e|=2;if(e===3)throw new Error("[_.sprintf] mixing positional and named placeholders is not (yet) supported");d.push(c)}b=b.substring(c[0].length)}return d},c}(),h={VERSION:"2.0.0",isBlank:function(a){return/^\s*$/.test(a)},stripTags:function(a){return(""+a).replace(/<\/?[^>]+>/ig,"")},capitalize:function(a){return a=""+a,a.charAt(0).toUpperCase()+a.substring(1).toLowerCase()},chop:function(a,b){a+="",b=~~b||a.length;var c=[];for(var d=0;d<a.length;)c.push(a.slice(d,d+b)),d+=b;return c},clean:function(a){return h.strip((""+a).replace(/\s+/g," "))},count:function(a,b){a=""+a,b=""+b;var c=0,d;for(var e=0;e<a.length;)d=a.indexOf(b,e),d>=0&&c++,e=e+(d>=0?d:0)+b.length;return c},chars:function(a){return(""+a).split("")},escapeHTML:function(a){return(""+a).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'")},unescapeHTML:function(a){return(""+a).replace(/</g,"<").replace(/>/g,">").replace(/"/g,'"').replace(/'/g,"'").replace(/&/g,"&")},escapeRegExp:function(a){return a.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1")},insert:function(a,b,c){var d=(""+a).split("");return d.splice(~~b,0,""+c),d.join("")},include:function(a,b){return(""+a).indexOf(b)!==-1},join:function(a){var b=e(arguments);return b.join(b.shift())},lines:function(a){return(""+a).split("\n")},reverse:function(a){return Array.prototype.reverse.apply(String(a).split("")).join("")},splice:function(a,b,c,d){var e=(""+a).split("");return e.splice(~~b,~~c,d),e.join("")},startsWith:function(a,b){return a=""+a,b=""+b,a.length>=b.length&&a.substring(0,b.length)===b},endsWith:function(a,b){return a=""+a,b=""+b,a.length>=b.length&&a.substring(a.length-b.length)===b},succ:function(a){a=""+a;var b=a.split("");return b.splice(a.length-1,1,String.fromCharCode(a.charCodeAt(a.length-1)+1)),b.join("")},titleize:function(a){return(""+a).replace(/\b./g,function(a){return a.toUpperCase()})},camelize:function(a){return h.trim(a).replace(/(\-|_|\s)+(.)?/g,function(a,b,c){return c?c.toUpperCase():""})},underscored:function(a){return h.trim(a).replace(/([a-z\d])([A-Z]+)/g,"$1_$2").replace(/[-\s]+/g,"_").toLowerCase()},dasherize:function(a){return h.trim(a).replace(/[_\s]+/g,"-").replace(/([A-Z])/g,"-$1").replace(/-+/g,"-").toLowerCase()},humanize:function(a){return h.capitalize(this.underscored(a).replace(/_id$/,"").replace(/_/g," "))},trim:function(a,c){return a=""+a,!c&&b?b.call(a):(c=f(c),a.replace(new RegExp("^"+c+"+|"+c+"+$","g"),""))},ltrim:function(a,b){return b=f(b),(""+a).replace(new RegExp("^"+b+"+","g"),"")},rtrim:function(a,b){return b=f(b),(""+a).replace(new RegExp(b+"+$","g"),"")},truncate:function(a,b,c){return a=""+a,c=c||"...",b=~~b,a.length>b?a.slice(0,b)+c:a},prune:function(a,b,c){a=""+a,b=~~b,c=c!=null?""+c:"...";var d,e,f=a.replace(/\W/g,function(a){return a.toUpperCase()!==a.toLowerCase()?"A":" "});return e=f[b],d=f.slice(0,b),e&&e.match(/\S/)&&(d=d.replace(/\s\S+$/,"")),d=h.rtrim(d),(d+c).length>a.length?a:a.substring(0,d.length)+c},words:function(a,b){return(""+a).split(b||" ")},pad:function(a,b,c,e){a=""+a;var f="",g=0;b=~~b,c?c.length>1&&(c=c.charAt(0)):c=" ";switch(e){case"right":g=b-a.length,f=d(c,g),a+=f;break;case"both":g=b-a.length,f={left:d(c,Math.ceil(g/2)),right:d(c,Math.floor(g/2))},a=f.left+a+f.right;break;default:g=b-a.length,f=d(c,g),a=f+a}return a},lpad:function(a,b,c){return h.pad(a,b,c)},rpad:function(a,b,c){return h.pad(a,b,c,"right")},lrpad:function(a,b,c){return h.pad(a,b,c,"both")},sprintf:g,vsprintf:function(a,b){return b.unshift(a),g.apply(null,b)},toNumber:function(a,b){var d=c(c(a).toFixed(~~b));return d===0&&""+a!="0"?Number.NaN:d},strRight:function(a,b){a=""+a,b=b!=null?""+b:b;var c=b?a.indexOf(b):-1;return c!=-1?a.slice(c+b.length,a.length):a},strRightBack:function(a,b){a=""+a,b=b!=null?""+b:b;var c=b?a.lastIndexOf(b):-1;return c!=-1?a.slice(c+b.length,a.length):a},strLeft:function(a,b){a=""+a,b=b!=null?""+b:b;var c=b?a.indexOf(b):-1;return c!=-1?a.slice(0,c):a},strLeftBack:function(a,b){a=""+a,b=b!=null?""+b:b;var c=a.lastIndexOf(b);return c!=-1?a.slice(0,c):a},toSentence:function(a,b,c){b||(b=", "),c||(c=" and ");var d=a.length,e="";for(var f=0;f<d;f++)e+=a[f],f===d-2?e+=c:f<d-1&&(e+=b);return e},slugify:function(a){var b="àáäâèéëêìíïîòóöôùúüûñç·/_:;",c="aaaaeeeeiiiioooouuuunc",d=new RegExp(f(b),"g");return a=(""+a).toLowerCase(),a=a.replace(d,function(a){return c[b.indexOf(a)]||"-"}),h.trim(a.replace(/[^\w\s-]/g,"").replace(/[-\s]+/g,"-"),"-")},exports:function(){var a={};for(var b in this){if(!this.hasOwnProperty(b)||b=="include"||b=="contains"||b=="reverse")continue;a[b]=this[b]}return a}};h.strip=h.trim,h.lstrip=h.ltrim,h.rstrip=h.rtrim,h.center=h.lrpad,h.rjust=h.lpad,h.ljust=h.rpad,h.contains=h.include,typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(module.exports=h),exports._s=h):typeof define=="function"&&define.amd?define("underscore.string",function(){return h}):typeof a._!="undefined"?(a._.string=h,a._.str=a._.string):a._={string:h,str:h}})(this||window); |