js-extensions
Version:
Object and String extensions
1 lines • 11.3 kB
JavaScript
;!Object,Object.values||(Object.values=function(t){for(var r=[],n=Object.keys(t),e=0,i=n.length;i>e;e++)r.push(t[n[e]]);return r}),Object.clone||(Object.clone=function(t){return JSONfn.clone(t,!0)}),Object.equals||(Object.equals=function(t,r){return Object.isNullOrUndefined(t)||Object.isNullOrUndefined(r)?null===t&&null===r:"string"==typeof t?t===r:"object"!=typeof t?t===r:JSON.stringify(t)===JSON.stringify(r)}),Object.hashCode||(Object.hashCode=function(t){if("undefined"==typeof t||null===t||0===t.length)return 0;for(var r=JSON.stringify(t),n=0,e=0;e<r.length;e++){var i=r.charCodeAt(e);n=(n<<5)-n+i,n&=n}return n}),Object.isNullOrUndefined||(Object.isNullOrUndefined=function(t){return"undefined"==typeof t||null===t}),Object.merge||(Object.merge=function(){for(var t={},r="",n=0;n<arguments.length;n++){var e=arguments[n];Object.isNullOrUndefined(e)||(r=r.concat(JSON.stringify(e)))}return r.length>0&&(t=JSON.parse(r.replace(/}{/g,","))),t}),!JSON,JSON.safeStringify||(JSON.safeStringify=function(t,r,n,e){return 2==arguments.length&&null!=r&&"number"==typeof r&&(n=r,r=void 0),JSON.stringify(t,JSON.serializer(r,e),n)}),JSON.serializer||(JSON.serializer=function(t,r){var n=[],e=[];return null==r&&(r=function(t,r){return n[0]===r?"[Circular ~]":"[Circular ~."+e.slice(0,n.indexOf(r)).join(".")+"]"}),function(i,o){if(n.length>0){var a=n.indexOf(this);~a?(n.splice(a+1),e.splice(a,1/0,i)):(n.push(this),e.push(i)),~n.indexOf(o)&&(o=r.call(this,i,o))}else n.push(o);return null==t?o:t.call(this,i,o)}}),!String,String.EMPTY||(String.EMPTY=""),String.clone||(String.clone=function(t){return Object.clone(t)}),String.endsWith||(String.endsWith=function(t,r){return-1!==t.indexOf(r,this.length-r.length)}),String.equals||(String.equals=function(t,r){return Object.equals(t,r)}),String.format||(String.format=function(t){for(var r=t,n=1;n<arguments.length;n++){var e=new RegExp("\\{"+(n-1)+"\\}","gm");r=r.replace(e,arguments[n])}return r}),String.generatePassword||(String.generatePassword=function(t,r,n){var e,i,o="aeiou".split(String.EMPTY),a="bcdfghjklmnpqrstvwxyz".split(String.EMPTY),s="!@#$%^&*?".split(String.EMPTY),g=String.EMPTY;t||(t=8);var u=0;for(r&&(u+=3),n&&(u+=1),e=0;t-u>e;e++){var p;p=e%2==0?o[Math.floor(4*Math.random())]:a[Math.floor(20*Math.random())],g+=0==e?p.toUpperCase():p}return r&&(i=Math.floor(99*Math.random())+String.EMPTY,1==i.length?i="00"+i:2==i.length&&(i="0"+i),g+=i),n&&(g+=s[Math.floor(8*Math.random())]),g.substr(0,t)}),String.hashCode||(String.hashCode=function(t){return Object.hashCode(t)}),String.ltrim||(String.ltrim=function(t){return t.replace(/^\s+/,"")}),String.isNullOrEmpty||(String.isNullOrEmpty=function(t){return Object.isNullOrUndefined(t)}),String.md5||(String.md5=function(t){function r(t){var r,o,s,g,u,p,S,d,y,m=new Array,b=7,C=12,O=17,v=22,M=5,j=9,w=14,N=20,A=4,W=11,x=16,T=23,E=6,J=10,k=15,U=21,P=n(t);m=e(P),u=1732584193,p=4023233417,S=2562383102,d=271733878;for(var y=0;y<m.length;y+=16)r=u,o=p,s=S,g=d,u=l(u,p,S,d,m[y+0],b,3614090360),d=l(d,u,p,S,m[y+1],C,3905402710),S=l(S,d,u,p,m[y+2],O,606105819),p=l(p,S,d,u,m[y+3],v,3250441966),u=l(u,p,S,d,m[y+4],b,4118548399),d=l(d,u,p,S,m[y+5],C,1200080426),S=l(S,d,u,p,m[y+6],O,2821735955),p=l(p,S,d,u,m[y+7],v,4249261313),u=l(u,p,S,d,m[y+8],b,1770035416),d=l(d,u,p,S,m[y+9],C,2336552879),S=l(S,d,u,p,m[y+10],O,4294925233),p=l(p,S,d,u,m[y+11],v,2304563134),u=l(u,p,S,d,m[y+12],b,1804603682),d=l(d,u,p,S,m[y+13],C,4254626195),S=l(S,d,u,p,m[y+14],O,2792965006),p=l(p,S,d,u,m[y+15],v,1236535329),u=f(u,p,S,d,m[y+1],M,4129170786),d=f(d,u,p,S,m[y+6],j,3225465664),S=f(S,d,u,p,m[y+11],w,643717713),p=f(p,S,d,u,m[y+0],N,3921069994),u=f(u,p,S,d,m[y+5],M,3593408605),d=f(d,u,p,S,m[y+10],j,38016083),S=f(S,d,u,p,m[y+15],w,3634488961),p=f(p,S,d,u,m[y+4],N,3889429448),u=f(u,p,S,d,m[y+9],M,568446438),d=f(d,u,p,S,m[y+14],j,3275163606),S=f(S,d,u,p,m[y+3],w,4107603335),p=f(p,S,d,u,m[y+8],N,1163531501),u=f(u,p,S,d,m[y+13],M,2850285829),d=f(d,u,p,S,m[y+2],j,4243563512),S=f(S,d,u,p,m[y+7],w,1735328473),p=f(p,S,d,u,m[y+12],N,2368359562),u=h(u,p,S,d,m[y+5],A,4294588738),d=h(d,u,p,S,m[y+8],W,2272392833),S=h(S,d,u,p,m[y+11],x,1839030562),p=h(p,S,d,u,m[y+14],T,4259657740),u=h(u,p,S,d,m[y+1],A,2763975236),d=h(d,u,p,S,m[y+4],W,1272893353),S=h(S,d,u,p,m[y+7],x,4139469664),p=h(p,S,d,u,m[y+10],T,3200236656),u=h(u,p,S,d,m[y+13],A,681279174),d=h(d,u,p,S,m[y+0],W,3936430074),S=h(S,d,u,p,m[y+3],x,3572445317),p=h(p,S,d,u,m[y+6],T,76029189),u=h(u,p,S,d,m[y+9],A,3654602809),d=h(d,u,p,S,m[y+12],W,3873151461),S=h(S,d,u,p,m[y+15],x,530742520),p=h(p,S,d,u,m[y+2],T,3299628645),u=c(u,p,S,d,m[y+0],E,4096336452),d=c(d,u,p,S,m[y+7],J,1126891415),S=c(S,d,u,p,m[y+14],k,2878612391),p=c(p,S,d,u,m[y+5],U,4237533241),u=c(u,p,S,d,m[y+12],E,1700485571),d=c(d,u,p,S,m[y+3],J,2399980690),S=c(S,d,u,p,m[y+10],k,4293915773),p=c(p,S,d,u,m[y+1],U,2240044497),u=c(u,p,S,d,m[y+8],E,1873313359),d=c(d,u,p,S,m[y+15],J,4264355552),S=c(S,d,u,p,m[y+6],k,2734768916),p=c(p,S,d,u,m[y+13],U,1309151649),u=c(u,p,S,d,m[y+4],E,4149444226),d=c(d,u,p,S,m[y+11],J,3174756917),S=c(S,d,u,p,m[y+2],k,718787259),p=c(p,S,d,u,m[y+9],U,3951481745),u=a(u,r),p=a(p,o),S=a(S,s),d=a(d,g);var q=i(u)+i(p)+i(S)+i(d);return q.toLowerCase()}function n(t){t=t.replace(/\r\n/g,"\n");for(var r="",n=0;n<t.length;n++){var e=t.charCodeAt(n);128>e?r+=String.fromCharCode(e):e>127&&2048>e?(r+=String.fromCharCode(e>>6|192),r+=String.fromCharCode(63&e|128)):(r+=String.fromCharCode(e>>12|224),r+=String.fromCharCode(e>>6&63|128),r+=String.fromCharCode(63&e|128))}return r}function e(t){for(var r,n=t.length,e=n+8,i=(e-e%64)/64,o=16*(i+1),a=(new Array(o-1),0),s=0;n>s;)r=(s-s%4)/4,a=s%4*8,lWordArray[r]=lWordArray[r]|t.charCodeAt(s)<<a,s++;return r=(s-s%4)/4,a=s%4*8,lWordArray[r]=lWordArray[r]|128<<a,lWordArray[o-2]=n<<3,lWordArray[o-1]=n>>>29,lWordArray}function i(t){var r,n,e="",i="";for(n=0;3>=n;n++)r=t>>>8*n&255,i="0"+r.toString(16),e+=i.substr(i.length-2,2);return e}function o(t,r){return t<<r|t>>>32-r}function a(t,r){var n,e,i,o,a;return i=2147483648&t,o=2147483648&r,n=1073741824&t,e=1073741824&r,a=(1073741823&t)+(1073741823&r),n&e?2147483648^a^i^o:n|e?1073741824&a?3221225472^a^i^o:1073741824^a^i^o:a^i^o}function s(t,r,n){return t&r|~t&n}function g(t,r,n){return t&n|r&~n}function u(t,r,n){return t^r^n}function p(t,r,n){return r^(t|~n)}function l(t,r,n,e,i,g,u){return t=a(t,a(a(s(r,n,e),i),u)),a(o(t,g),r)}function f(t,r,n,e,i,s,u){return t=a(t,a(a(g(r,n,e),i),u)),a(o(t,s),r)}function h(t,r,n,e,i,s,g){return t=a(t,a(a(u(r,n,e),i),g)),a(o(t,s),r)}function c(t,r,n,e,i,s,g){return t=a(t,a(a(p(r,n,e),i),g)),a(o(t,s),r)}return r(t)}),String.print||(String.print=function(){console.log(String.format.apply(this,arguments))}),String.printf||(String.printf=function(){console.log(String.sprintf.apply(this,arguments))}),String.rtrim||(String.rtrim=function(t){return t.replace(/\s+$/,"")}),String.sprintf||(String.sprintf=function(t){if("string"!=typeof t)throw"sprintf: The first arguments need to be a valid format string.";for(var r,n=new RegExp(/%(\+)?([0 ]|'(.))?(-)?([0-9]+)?(\.([0-9]+))?([%bcdfosxX])/g),e=[],i=1;r=n.exec(t);){if(i>=arguments.length&&"%"!=r[8])throw"sprintf: At least one argument was missing.";e[e.length]={begin:r.index,end:r.index+r[0].length,sign:"+"==r[1],negative:parseFloat(arguments[i])<0?!0:!1,padding:void 0==r[2]?" ":"'"==r[2].substring(0,1)?r[3]:r[2],alignLeft:"-"==r[4],width:void 0!=r[5]?r[5]:!1,precision:void 0!=r[7]?r[7]:!1,type:r[8],data:"%"!=r[8]?String(arguments[i++]):!1}}for(var o="",a=0,s=0;s<e.length;++s){o+=t.substring(a,e[s].begin),a=e[s].end;var g="";switch(e[s].type){case"%":g="%";break;case"b":g=Math.abs(parseInt(e[s].data)).toString(2);break;case"c":g=String.fromCharCode(Math.abs(parseInt(e[s].data)));break;case"d":g=String(Math.abs(parseInt(e[s].data)));break;case"f":g=e[s].precision===!1?String(Math.abs(parseFloat(e[s].data))):Math.abs(parseFloat(e[s].data)).toFixed(e[s].precision);break;case"o":g=Math.abs(parseInt(e[s].data)).toString(8);break;case"s":g=e[s].data.substring(0,e[s].precision?e[s].precision:e[s].data.length);break;case"x":g=Math.abs(parseInt(e[s].data)).toString(16).toLowerCase();break;case"X":g=Math.abs(parseInt(e[s].data)).toString(16).toUpperCase();break;default:throw'sprintf: Unknown type "'+e[s].type+'" detected. This should never happen. Maybe the regex is wrong.'}if("%"!=e[s].type){if(0!=e[s].width&&e[s].width>g.length)for(var u=g.length,p=0;p<e[s].width-u;++p)g=1==e[s].alignLeft?g+e[s].padding:e[s].padding+g;("b"==e[s].type||"d"==e[s].type||"o"==e[s].type||"f"==e[s].type||"x"==e[s].type||"X"==e[s].type)&&(1==e[s].negative?g="-"+g:1==e[s].sign&&(g="+"+g)),o+=g}else o+=g}return o+=t.substring(a,t.length)}),String.startsWith||(String.startsWith=function(t,r){return 0===t.indexOf(r)}),String.toBoolean||(String.toBoolean=function(t){return"true"===t.toLowerCase()||"yes"===t.toLowerCase()}),String.toCamelCase||(String.toCamelCase=function(t){var r=this.replace(/[-_ .]+(.)?/g,function(t,r){return r?r.toUpperCase():String.EMPTY}).replace(/[^\w]/gi,String.EMPTY);return r}),String.toTitleCase||(String.toTitleCase=function(t){var r=/^(a|an|and|as|at|but|by|en|for|if|in|is|nor|of|on|or|per|the|to|vs?\.?|via)$/i;return t.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g,function(t,n,e){return n>0&&n+t.length!==e.length&&t.search(r)>-1&&":"!==e.charAt(n-2)&&("-"!==e.charAt(n+t.length)||"-"===e.charAt(n-1))&&e.charAt(n-1).search(/[^\s-]/)<0?t.toLowerCase():t.substr(1).search(/[A-Z]|\../)>-1?t:t.charAt(0).toUpperCase()+t.substr(1)})}),String.prototype.clone||(String.prototype.clone=function(){return String.clone(this)}),String.prototype.endsWith||(String.prototype.endsWith=function(t){return String.endsWith(this,t)}),String.prototype.equals||(String.prototype.equals=function(t){return String.equals(this,t)}),String.prototype.format||(String.prototype.format=function(){for(var t=[this],r=0;r<arguments.length;r++)t.push(arguments[r]);return String.format.apply(this,t)}),String.prototype.hashCode||(String.prototype.hashCode=function(){return String.hashCode(this)}),String.prototype.isNullOrEmpty||(String.prototype.isNullOrEmpty=function(){return String.isNullOrUndefined(this)}),String.prototype.ltrim||(String.prototype.ltrim=function(){return String.ltrim(this)}),String.prototype.md5||(String.prototype.md5=function(){return String.md5(this)}),String.prototype.print||(String.prototype.print=function(){for(var t=[this],r=0;r<arguments.length;r++)t.push(arguments[r]);String.print.apply(this,t)}),String.prototype.printf||(String.prototype.printf=function(){for(var t=[this],r=0;r<arguments.length;r++)t.push(arguments[r]);String.printf.apply(this,t)}),String.prototype.rtrim||(String.prototype.rtrim=function(){return String.rtrim(this)}),String.prototype.sprintf||(String.prototype.sprintf=function(){for(var t=[this],r=0;r<arguments.length;r++)t.push(arguments[r]);return String.sprintf.apply(this,t)}),String.prototype.startsWith||(String.prototype.startsWith=function(t){return String.startsWith(this,t)}),String.prototype.toBoolean||(String.toBoolean=function(){return String.toBoolean(this)}),String.prototype.toCamelCase||(String.prototype.toCamelCase=function(){return String.toCamelCase(this)}),String.prototype.toTitleCase||(String.prototype.toTitleCase=function(){return String.toTitleCase(this)});