Comparing version 5.4.24 to 6.0.0
/** | ||
* bellajs | ||
* v5.4.24 | ||
* built: Wed, 05 Oct 2016 03:14:10 GMT | ||
* v6.0.0 | ||
* built: Mon, 07 Nov 2016 10:21:53 GMT | ||
* git: https://github.com/ndaidong/bellajs | ||
@@ -9,26 +9,32 @@ * author: @ndaidong | ||
**/ | ||
;var $jscomp={scope:{},checkStringArgs:function(f,m,l){if(null==f)throw new TypeError("The 'this' value for String.prototype."+l+" must not be null or undefined");if(m instanceof RegExp)throw new TypeError("First argument to String.prototype."+l+" must not be a regular expression");return f+""}}; | ||
$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(f,m,l){if(l.get||l.set)throw new TypeError("ES3 does not support getters and setters.");f!=Array.prototype&&f!=Object.prototype&&(f[m]=l.value)};$jscomp.getGlobal=function(f){return"undefined"!=typeof window&&window===f?f:"undefined"!=typeof global?global:f};$jscomp.global=$jscomp.getGlobal(this); | ||
$jscomp.polyfill=function(f,m,l,q){if(m){l=$jscomp.global;f=f.split(".");for(q=0;q<f.length-1;q++){var h=f[q];h in l||(l[h]={});l=l[h]}f=f[f.length-1];q=l[f];m=m(q);m!=q&&null!=m&&$jscomp.defineProperty(l,f,{configurable:!0,writable:!0,value:m})}};$jscomp.polyfill("String.prototype.includes",function(f){return f?f:function(f,l){return-1!==$jscomp.checkStringArgs(this,f,"includes").indexOf(f,l||0)}},"es6-impl","es3"); | ||
(function(f,m){if("undefined"!==typeof module&&module.exports)module.exports=m();else{var l=window||{};l.define&&l.define.amd?l.define([],m):l.exports?l.exports=m():l[f]=m()}})("Bella",function(){var f="undefined"!==typeof module&&module.exports?"node":"browser",m=function(a){var b=Object.prototype.toString,c=typeof a;if("object"===c){if(a){if(-1!==b.call(a).indexOf("HTML")&&-1!==b.call(a).indexOf("Element"))return"element";if(a instanceof Array||!(a instanceof Object)&&"[object Array]"===b.call(a)|| | ||
"number"===typeof a.length&&"undefined"!==typeof a.splice&&"undefined"!==typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if(!(a instanceof Object||"[object Function]"!==b.call(a)&&("undefined"===typeof a.call||"undefined"===typeof a.propertyIsEnumerable||a.propertyIsEnumerable("call"))))return"function"}return"object"}return"function"===c&&"undefined"===typeof a.call?"object":c},l=function(a){return"undefined"!==m(a)},q=function(a){return null===m(a)||null===a},h=function(a){return!q(a)&& | ||
"string"===m(a)},u=function(a){return""!==a&&!q(a)&&l(a)&&!isNaN(a)&&"number"===m(a)},E=function(a){return u(a)&&isFinite(a)&&Math.floor(a)===a},p=function(a){return!q(a)&&"array"===m(a)},r=function(a){return!q(a)&&"object"===m(a)},A=function(a){return a instanceof Date&&!isNaN(a.valueOf())},F=function(a){return a&&"node"===f&&a._root?!0:!q(a)&&"element"===m(a)},B=function(a){return!l(a)||q(a)||h(a)&&""===a||p(a)&&"[]"===JSON.stringify(a)||r(a)&&"{}"===JSON.stringify(a)},t=function(a,b){if(!a||!b)return!1; | ||
var c=!0;l(a[b])||(c=b in a);return c},C=function(a,b){var c=!0;if(B(a)&&B(b))return!0;if(A(a)&&A(b))return a.getTime()===b.getTime();if(u(a)&&u(b)||h(a)&&h(b))return a===b;if(p(a)&&p(b)){if(a.length!==b.length)return!1;if(0<a.length)for(var e=0,d=a.length;e<d;e++)if(!C(a[e],b[e])){c=!1;break}}else if(r(a)&&r(b)){var e=[],d=[],n;for(n in a)t(a,n)&&e.push(n);for(var k in b)t(b,k)&&d.push(k);if(e.length!==d.length)return!1;for(var v in a)if(!t(b,v)||!C(a[v],b[v])){c=!1;break}}return c},w=function(a, | ||
b){return h(a)?(a=a?a.replace(/^[\s\xa0]+|[\s\xa0]+$/g,""):a||"")&&b?a.replace(/\r?\n|\r/g," ").replace(/\s\s+|\r/g," "):a:""},G=function(a){return h(a)?a.toLowerCase():""},H=function(a){if(!h(a))return"";if(1===a.length)return a.toUpperCase();a=a.toLowerCase();return a.charAt(0).toUpperCase()+a.slice(1)},I=function(a,b,c){a=String(a);b=b||2;return a.length>=b?a:Array(b-a.length+1).join(c||"0")+a},J=function(a,b){for(var c=[];10>c.length;)c.push(Math.random().toString(36).slice(2));var c=c.join(""), | ||
e=c.length;b=b||"";for(a=Math.max(a||32,b.length);b.length<a;)b+=c.charAt(Math.floor(Math.random()*e))||"";return b},D=function(a,b){if(!a||0>a)a=0;b||(b=9007199254740991);if(a===b)return b;a>b&&(a=Math.min(a,b),b=Math.max(a,b));return Math.floor(Math.random()*(b-a+1))+a},K=function(a,b,c){if(p(a))for(var e=0;e<a.length;e++){var d=a[e];if(c&&d[c]===b[c]||d===b)return!0}else{if(r(a)&&h(b))return t(a,b);if(h(a)&&h(b))return a.includes(b)}return!1},x=function(a){if(!(a instanceof Object))return a;var b; | ||
b=a.constructor;switch(b){case RegExp:b=new b(a);break;case Date:b=new b(a.getTime());break;default:b=new b}for(var c in a)b[c]=x(a[c]);return b},L=function(a,b,c,e){c=c||!1;e=e||[];for(var d in a)if(!(0<e.length&&K(e,d))&&(!c||c&&b.hasOwnProperty(d))){var n=a[d],k=b[d];r(k)&&r(n)||p(k)&&p(n)?b[d]=L(n,b[d],c,e):b[d]=x(n)}return b},M=function(a){a=x(a);a.sort(function(){return Math.random()-.5});return a},P=function(){for(var a=[],b=0;64>b;)a[b]=0|4294967296*Math.abs(Math.sin(++b));return function(b){var c, | ||
d,n,k,v=[];b=unescape(encodeURI(b));for(var g=b.length,h=[c=1732584193,d=-271733879,~c,~d],f=0;f<=g;)v[f>>2]|=(b.charCodeAt(f)||128)<<8*(f++%4);v[b=16*(g+8>>6)+14]=8*g;for(f=0;f<b;f+=16){g=h;for(k=0;64>k;)g=[n=g[3],(c=g[1]|0)+((n=g[0]+[c&(d=g[2])|~c&n,n&c|~n&d,c^d^n,d^(c|~n)][g=k>>4]+(a[k]+(v[[k,5*k+1,3*k+5,7*k][g]%16+f]|0)))<<(g=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*g+k++%4])|n>>>32-g),c,d];for(k=4;k;)h[--k]=h[k]+g[k]}for(b="";32>k;)b+=(h[k>>3]>>4*(1^k++&7)&15).toString(16);return b}}(), | ||
y=function(a,b,c){u(a)&&(a=String(a));if(!a||!h(a))return"";u(b)&&(b=String(b));u(c)&&(c=String(c));if(h(b)&&h(c))a=a.split(b).join(c);else if(p(b)&&h(c))b.forEach(function(b){a=y(a,b,c)});else if(p(b)&&p(c)&&b.length===c.length){var e=b.length;if(0<e)for(var d=0;d<e;d++)a=y(a,b[d],c[d])}return a},N=function(a){if(u(a))return String(a);if(!h(a))return"";var b={a:"\u00e1|\u00e0|\u1ea3|\u00e3|\u1ea1|\u0103|\u1eaf|\u1eb7|\u1eb1|\u1eb3|\u1eb5|\u00e2|\u1ea5|\u1ea7|\u1ea9|\u1eab|\u1ead|\u00e4",A:"\u00c1|\u00c0|\u1ea2|\u00c3|\u1ea0|\u0102|\u1eae|\u1eb6|\u1eb0|\u1eb2|\u1eb4|\u00c2|\u1ea4|\u1ea6|\u1ea8|\u1eaa|\u1eac|\u00c4", | ||
c:"\u00e7",C:"\u00c7",d:"\u0111",D:"\u0110",e:"\u00e9|\u00e8|\u1ebb|\u1ebd|\u1eb9|\u00ea|\u1ebf|\u1ec1|\u1ec3|\u1ec5|\u1ec7|\u00eb",E:"\u00c9|\u00c8|\u1eba|\u1ebc|\u1eb8|\u00ca|\u1ebe|\u1ec0|\u1ec2|\u1ec4|\u1ec6|\u00cb",i:"\u00ed|\u00ec|\u1ec9|\u0129|\u1ecb|\u00ef|\u00ee",I:"\u00cd|\u00cc|\u1ec8|\u0128|\u1eca|\u00cf|\u00ce",o:"\u00f3|\u00f2|\u1ecf|\u00f5|\u1ecd|\u00f4|\u1ed1|\u1ed3|\u1ed5|\u1ed7|\u1ed9|\u01a1|\u1edb|\u1edd|\u1edf|\u1ee1|\u1ee3|\u00f6",O:"\u00d3|\u00d2|\u1ece|\u00d5|\u1ecc|\u00d4|\u1ed0|\u1ed2|\u1ed4|\u00d4|\u1ed8|\u01a0|\u1eda|\u1edc|\u1ede|\u1ee0|\u1ee2|\u00d6", | ||
u:"\u00fa|\u00f9|\u1ee7|\u0169|\u1ee5|\u01b0|\u1ee9|\u1eeb|\u1eed|\u1eef|\u1ef1|\u00fb",U:"\u00da|\u00d9|\u1ee6|\u0168|\u1ee4|\u01af|\u1ee8|\u1eea|\u1eec|\u1eee|\u1ef0|\u00db",y:"\u00fd|\u1ef3|\u1ef7|\u1ef9|\u1ef5",Y:"\u00dd|\u1ef2|\u1ef6|\u1ef8|\u1ef4"},c;for(c in b)if(t(b,c))for(var e=b[c].split("|"),d=0;d<e.length;d++)a=y(a,e[d],c);return a},z=function(){return new Date},O=function(){return Date.now()},Q=function(){var a="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),b="January February March April May June July August September October November December".split(" "), | ||
c=function(){var a=z().getTimezoneOffset();return["GMT",0>a?"+":"-",I(Math.abs(a/60),4)].join("")}(),e=function(d,e){var k=!1,f,g;e=e?(new Date(e)).getTime():O();d&&h(d)||(d="D, M d, Y h:i:s A");d.match(/(\.*)a{1}(\.*)*/i)&&(k=!0);var n=function(a){return String(10>a?"0"+a:a)};f=e instanceof Date?e:new Date(e);if(isNaN(f.getTime()))if(/^(\d+-\d+-\d+)\s(\d+:\d+:\d+)$/i.test(e))f=new Date(e.replace(" ","T"));else return e+" !";g={Y:function(){return f.getFullYear()},y:function(){return(g.Y()+"").slice(-2)}, | ||
F:function(){return b[g.n()-1]},M:function(){return(g.F()+"").slice(0,3)},m:function(){return n(g.n())},n:function(){return f.getMonth()+1},S:function(){var a=g.j()+" ",a=a.charAt(a.length-2);return"1"===a?"st":"2"===a?"nd":"3"===a?"rd":"th"},j:function(){return f.getDate()},d:function(){return n(g.j())},t:function(){return(new Date(g.Y(),g.n(),0)).getDate()},w:function(){return f.getDay()},l:function(){return a[g.w()]},D:function(){return(g.l()+"").slice(0,3)},G:function(){return f.getHours()},g:function(){return g.G()% | ||
12||12},h:function(){return n(k?g.g():g.G())},i:function(){return n(f.getMinutes())},s:function(){return n(f.getSeconds())},a:function(){return 11<g.G()?"pm":"am"},A:function(){return g.a().toUpperCase()},O:function(){return c}};return d.replace(/\.*\\?([a-z])/gi,function(a,b){return g[a]?g[a]():b})};return{utc:function(a){return(new Date(a||z())).toUTCString()},local:function(a){return e("D, j M Y h:i:s O",a)},strtotime:function(a){return(new Date(a)).getTime()},format:e,relativize:function(a){var b= | ||
a instanceof Date?a:new Date(a);a=z()-b;b=parseInt(b,10);isNaN(b)&&(b=0);if(a<=b)return"Just now";var b=null,c={millisecond:1,second:1E3,minute:60,hour:60,day:24,month:30,year:12},d;for(d in c)if(a<c[d])break;else b=d,a/=c[d];a=Math.floor(a);1!==a&&(b+="s");return[a,b].join(" ")+" ago"}}}();return{ENV:f,id:J(),isDef:l,isNull:q,isString:h,isNumber:u,isInteger:E,isBoolean:function(a){return!0===a||!1===a},isArray:p,isObject:r,isDate:A,isFunction:function(a){return!q(a)&&"function"===m(a)},isElement:F, | ||
isEmpty:B,isLetter:function(a){var b=/^[a-z]+$/i;return h(a)&&b.test(a)},isEmail:function(a){var b=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;return h(a)&&b.test(a)},isGeneratedKey:function(a){var b=/^[A-Z0-9]+$/i;return h(a)&&b.test(a)},hasProperty:t,equals:C,encode:function(a){return h(a)?encodeURIComponent(a):""},decode:function(a){return h(a)?decodeURIComponent(a.replace(/\+/g," ")):""},trim:w,truncate:function(a,b){a=w(a);if(!a)return a;b=b||140;if(a.length<= | ||
b)return a;var c=a.substring(0,b),e=c.split(" "),d="";1<e.length?(e.pop(),d+=e.join(" "),d.length<a.length&&(d+="...")):(c=c.substring(0,b-3),d=c+"...");return d},stripTags:function(a){if(!h(a))return"";(a=a.replace(/<.*?>/gi," "))&&(a=w(a.replace(/\s\s+/g," ")));return a},escapeHTML:function(a){return h(a)?a.replace(/&/g,"\x26amp;").replace(/</g,"\x26lt;").replace(/>/g,"\x26gt;").replace(/"/g,"\x26quot;"):""},unescapeHTML:function(a){return h(a)?a.replace(/"/g,'"').replace(/</g,"\x3c").replace(/>/g, | ||
"\x3e").replace(/&/g,"\x26"):""},strtolower:G,strtoupper:function(a){return h(a)?a.toUpperCase():""},ucfirst:H,ucwords:function(a){if(!h(a))return"";var b=[];a.split(" ").forEach(function(a){b.push(H(a))});return b.join(" ")},leftPad:I,rightPad:function(a,b,c){a=String(a);b=b||2;return a.length>=b?a:a+Array(b-a.length+1).join(c||"0")},repeat:function(a,b){if(!a||!h(a))return"";if(!E(b)||1>b)return a;for(var c=[];c.length<b;)c.push(a);return c.join("")},replaceAll:y,stripAccent:N,createAlias:function(a, | ||
b){a=String(a);if(a=N(a))b=b||"-",a=G(a),a=w(a),a=a.replace(/\W+/g," "),a=a.replace(/\s+/g," "),a=a.replace(/\s/g,b);return a},compile:function(a,b){var c=[],e=function(a,b,f){f&&c.push(f);f=[];for(var d in b)if(t(b,d)){var g=b[d];if(r(g)||p(g))f.push({key:d,data:g});else if(h(g)){var g=y(g,["{","}"],["\x26#123;","\x26#125;"]),k=c.concat([d]),k=new RegExp("{"+k.join(".")+"}","gi");a=a.replace(k,g)}}0<f.length&&f.forEach(function(b){a=e(a,b.data,b.key)});return w(a,!0)};return b&&(h(b)||r(b)||p(b))? | ||
e(a,b):a},md5:P,createId:J,random:D,min:function(a){return p(a)?Math.min.apply({},a):a},max:function(a){return p(a)?Math.max.apply({},a):a},unique:function(a){if(p(a)){for(var b=[],c=0;c<a.length;c++)-1===b.indexOf(a[c])&&b.push(a[c]);return b}return a||[]},contains:K,first:function(a){return a[0]},last:function(a){return a[a.length-1]},getIndex:function(a,b){for(var c=-1,e=0;e<a.length;e++)if(a[e]===b){c=e;break}return c},getLastIndex:function(a,b){for(var c=-1,e=a.length-1;0<=e;e--)if(a[e]===b){c= | ||
e;break}return c},sort:function(a,b){var c=[],e={},d=b||1;if(p(a)&&0<a.length)if(c=x(a),e=c[0],1===d||-1===d)c.sort(function(a,b){return a>b?d:a<b?-1*d:0});else if(h(d)&&t(e,d))c.sort(function(a,b){return a[d]>b[d]?1:a[d]<b[d]?-1:0});else if(r(d)){a={};for(var f in d)a.key=f,t(e,a.key)&&(a.order=-1===d[a.key]?-1:1,c.sort(function(a){return function(b,c){return b[a.key]>c[a.key]?a.order:b[a.key]<c[a.key]?-1*a.order:0}}(a))),a={key:a.key,order:a.order}}return c},shuffle:M,pick:function(a,b){var c=b? | ||
Math.min(b,a.length):1;1>c&&(c=1);b=M(a);if(c>=a.length)return b;if(1===c)return c=D(0,a.length-1),b[c];for(a=[];a.length<c;){var e=D(0,b.length-1);a.push(b[e]);b.splice(e,1)}return a},empty:function(a){if(p(a)){for(var b=a.length-1;0<=b;b--)a[b]=null,delete a[b];a.length=0}else if(r(a))for(b in a)t(a,b)&&(a[b]=null,delete a[b]);else h(a)?a="":F(a)&&(a.innerHTML="");return a},copies:L,clone:x,now:z,time:O,date:Q}}); | ||
;var $jscomp={scope:{}};$jscomp.defineProperty="function"==typeof Object.defineProperties?Object.defineProperty:function(b,d,e){if(e.get||e.set)throw new TypeError("ES3 does not support getters and setters.");b!=Array.prototype&&b!=Object.prototype&&(b[d]=e.value)};$jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global?global:b};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_"; | ||
$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.symbolCounter_=0;$jscomp.Symbol=function(b){return $jscomp.SYMBOL_PREFIX+(b||"")+$jscomp.symbolCounter_++}; | ||
$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var b=$jscomp.global.Symbol.iterator;b||(b=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[b]&&$jscomp.defineProperty(Array.prototype,b,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};$jscomp.arrayIterator=function(b){var d=0;return $jscomp.iteratorPrototype(function(){return d<b.length?{done:!1,value:b[d++]}:{done:!0}})}; | ||
$jscomp.iteratorPrototype=function(b){$jscomp.initSymbolIterator();b={next:b};b[$jscomp.global.Symbol.iterator]=function(){return this};return b};$jscomp.makeIterator=function(b){$jscomp.initSymbolIterator();var d=b[Symbol.iterator];return d?d.call(b):$jscomp.arrayIterator(b)};$jscomp.arrayFromIterator=function(b){for(var d,e=[];!(d=b.next()).done;)e.push(d.value);return e};$jscomp.arrayFromIterable=function(b){return b instanceof Array?b:$jscomp.arrayFromIterator($jscomp.makeIterator(b))}; | ||
$jscomp.checkStringArgs=function(b,d,e){if(null==b)throw new TypeError("The 'this' value for String.prototype."+e+" must not be null or undefined");if(d instanceof RegExp)throw new TypeError("First argument to String.prototype."+e+" must not be a regular expression");return b+""}; | ||
$jscomp.polyfill=function(b,d,e,f){if(d){e=$jscomp.global;b=b.split(".");for(f=0;f<b.length-1;f++){var k=b[f];k in e||(e[k]={});e=e[k]}b=b[b.length-1];f=e[b];d=d(f);d!=f&&null!=d&&$jscomp.defineProperty(e,b,{configurable:!0,writable:!0,value:d})}};$jscomp.polyfill("String.prototype.includes",function(b){return b?b:function(b,e){return-1!==$jscomp.checkStringArgs(this,b,"includes").indexOf(b,e||0)}},"es6-impl","es3"); | ||
$jscomp.polyfill("Array.prototype.fill",function(b){return b?b:function(b,e,f){var d=this.length||0;0>e&&(e=Math.max(0,d+e));if(null==f||f>d)f=d;f=Number(f);0>f&&(f=Math.max(0,d+f));for(e=Number(e||0);e<f;e++)this[e]=b;return this}},"es6-impl","es3");$jscomp.owns=function(b,d){return Object.prototype.hasOwnProperty.call(b,d)}; | ||
$jscomp.polyfill("Object.assign",function(b){return b?b:function(b,e){for(var d=1;d<arguments.length;d++){var k=arguments[d];if(k)for(var n in k)$jscomp.owns(k,n)&&(b[n]=k[n])}return b}},"es6-impl","es3");$jscomp.array=$jscomp.array||{}; | ||
$jscomp.iteratorFromArray=function(b,d){$jscomp.initSymbolIterator();b instanceof String&&(b+="");var e=0,f={next:function(){if(e<b.length){var k=e++;return{value:d(k,b[k]),done:!1}}f.next=function(){return{done:!0,value:void 0}};return f.next()}};f[Symbol.iterator]=function(){return f};return f};$jscomp.polyfill("Array.prototype.keys",function(b){return b?b:function(){return $jscomp.iteratorFromArray(this,function(b){return b})}},"es6-impl","es3"); | ||
(function(b,d){if("undefined"!==typeof module&&module.exports)module.exports=d();else{var e=window||global||{};e.define&&e.define.amd?e.define([],d):e.exports?e.exports=d():e[b]=d()}})("Bella",function(){var b=module.exports?"node":"browser",d=function(a){return null!==a&&"string"===typeof a},e=function(a){return""!==a&&null!==a&&!0&&!isNaN(a)&&"number"===typeof a},f=function(a){return e(a)&&isFinite(a)&&Math.floor(a)===a},k=function(a){return null!==a&&Array.isArray(a)},n=function(a){return null!== | ||
a&&"object"===typeof a&&!1===k(a)},u=function(a){return a instanceof Date&&!isNaN(a.valueOf())},z=function(a){return!a||!1||null===a||d(a)&&""===a||k(a)&&"[]"===JSON.stringify(a)||n(a)&&"{}"===JSON.stringify(a)},t=function(a,c){return a&&c?Object.prototype.hasOwnProperty.call(a,c):!1},A=function(a,c){var b=!0;if(z(a)&&z(c))return!0;if(u(a)&&u(c))return a.getTime()===c.getTime();if(e(a)&&e(c)||d(a)&&d(c))return a===c;if(k(a)&&k(c)){if(a.length!==c.length)return!1;if(0<a.length)for(var r=0,g=a.length;r< | ||
g;r++)if(!A(a[r],c[r])){b=!1;break}}else if(n(a)&&n(c)){var r=[],g=[],B;for(B in a)t(a,B)&&r.push(B);for(var l in c)t(c,l)&&g.push(l);if(r.length!==g.length)return!1;for(var C in a)if(!t(c,C)||!A(a[C],c[C])){b=!1;break}}return b},v=function(a,c){return d(a)?(a=a?a.replace(/^[\s\xa0]+|[\s\xa0]+$/g,""):a||"")&&c?a.replace(/\r?\n|\r/g," ").replace(/\s\s+|\r/g," "):a:""},E=function(a){if(!d(a))return"";if(1===a.length)return a.toUpperCase();a=a.toLowerCase();return a.charAt(0).toUpperCase()+a.slice(1)}, | ||
F=function(a,c,b){c=void 0===c?2:c;a=String(a);return a.length>=c?a:Array(c-a.length+1).join(void 0===b?"0":b)+a},G=function(a,c){c=void 0===c?"":c;for(var b=[];10>b.length;)b.push(Math.random().toString(36).slice(2));var b=b.join(""),d=b.length;for(a=Math.max(a||32,c.length);c.length<a;)c+=b.charAt(Math.floor(Math.random()*d))||"";return c},D=function(a,c){if(!a||0>a)a=0;c||(c=9007199254740991);if(a===c)return c;a>c&&(a=Math.min(a,c),c=Math.max(a,c));return Math.floor(Math.random()*(c-a+1))+a},q= | ||
function(){var a=function(a){a=void 0===a?[]:a;var c=[].concat($jscomp.arrayFromIterable(a)),b=function(a,b){for(var l=[],d=1;d<arguments.length;++d)l[d-1]=arguments[d];a=void 0===a?0:a;var g=[].concat($jscomp.arrayFromIterable(c)),d=g.slice(0,a),g=g.slice(a,g.length);return q([].concat.apply([],[].concat([d],$jscomp.arrayFromIterable(l),[g])))},d=function(a){a=[].concat($jscomp.arrayFromIterable(c)).sort(a);return q(a)};[["min",function(){return Math.min.apply({},c)}],["max",function(){return Math.max.apply({}, | ||
c)}],["unique",function(){for(var a=[].concat($jscomp.arrayFromIterable(c)),b=[],d=0;d<a.length;d++)-1===b.indexOf(a[d])&&b.push(a[d]);return q(b)}],["first",function(){var a=[].concat($jscomp.arrayFromIterable(c))[0];return q(a)}],["last",function(){var a=[].concat($jscomp.arrayFromIterable(c))[c.length-1];return q(a)}],["pick",function(a){a=void 0===a?1:a;var b=c.shuffle();a=Math.max(Math.min(a,b.length),1);if(a>=b.length)return b;if(1===a)return a=D(0,b.length-1),b[a];for(var d=[];d.length<a;){var g= | ||
D(0,b.length-1);d.push(b[g]);b=b.splice(g,1)}return d}],["insert",b],["append",function(a){for(var d=[],g=0;g<arguments.length;++g)d[g-0]=arguments[g];return b(c.length,d)}],["remove",function(a,b){a=void 0===a?0:a;b=void 0===b?0:b;a=[].concat($jscomp.arrayFromIterable(c.slice(0,a)),$jscomp.arrayFromIterable(c.slice(a+b)));return q(a)}],["isort",d],["ireverse",function(){var a=[].concat($jscomp.arrayFromIterable(c)).reverse();return q(a)}],["shuffle",function(){return d(function(){return Math.random()- | ||
.5})}]].map(function(a){Object.defineProperty(c,a[0],{enumerable:!1,configurable:!1,writable:!1,value:a[1]})});return c},c=function(a){a=void 0===a?{}:a;var c=Object.create({}),b={enumerable:!0,configurable:!1,writable:!1,value:"undefined"};Object.keys(a).map(function(d){var g=Object.assign({},b);g.value=a[d];Object.defineProperty(c,d,g)});Object.defineProperty(c,"get",{enumerable:!1,configurable:!1,writable:!1,value:function(a){return c[a]}});Object.defineProperty(c,"set",{enumerable:!1,configurable:!1, | ||
writable:!1,value:function(a,b){b=void 0===b?!1:b;var d=Object.assign({},c);n(a)?Object.keys(a).forEach(function(c){d[c]=a[c]}):d[a]=b;return q(d)}});return c};return function(b){return k(b)?a(b):n(b)?c(b):b}}(),J=function(){for(var a=[],c=0;64>c;)a[c]=0|4294967296*Math.abs(Math.sin(++c));return function(c){var b,d,e,l,x=[];c=unescape(encodeURI(c));for(var h=c.length,f=[b=1732584193,d=-271733879,~b,~d],p=0;p<=h;)x[p>>2]|=(c.charCodeAt(p)||128)<<8*(p++%4);x[c=16*(h+8>>6)+14]=8*h;for(p=0;p<c;p+=16){h= | ||
f;for(l=0;64>l;)h=[e=h[3],(b=h[1]|0)+((e=h[0]+[b&(d=h[2])|~b&e,e&b|~e&d,b^d^e,d^(b|~e)][h=l>>4]+(a[l]+(x[[l,5*l+1,3*l+5,7*l][h]%16+p]|0)))<<(h=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*h+l++%4])|e>>>32-h),b,d];for(l=4;l;)f[--l]=f[l]+h[l]}for(c="";32>l;)c+=(f[l>>3]>>4*(1^l++&7)&15).toString(16);return c}}(),w=function(a,c,b){e(a)&&(a=String(a));if(!a||!d(a))return"";e(c)&&(c=String(c));e(b)&&(b=String(b));if(d(c)&&d(b))a=a.split(c).join(b);else if(k(c)&&d(b))c.forEach(function(c){a=w(a,c,b)}); | ||
else if(k(c)&&k(b)&&c.length===b.length){var f=c.length;if(0<f)for(var g=0;g<f;g++)a=w(a,c[g],b[g])}return a},H=function(a){if(e(a))return String(a);if(!d(a))return"";var b={a:"\u00e1|\u00e0|\u1ea3|\u00e3|\u1ea1|\u0103|\u1eaf|\u1eb7|\u1eb1|\u1eb3|\u1eb5|\u00e2|\u1ea5|\u1ea7|\u1ea9|\u1eab|\u1ead|\u00e4",A:"\u00c1|\u00c0|\u1ea2|\u00c3|\u1ea0|\u0102|\u1eae|\u1eb6|\u1eb0|\u1eb2|\u1eb4|\u00c2|\u1ea4|\u1ea6|\u1ea8|\u1eaa|\u1eac|\u00c4",c:"\u00e7",C:"\u00c7",d:"\u0111",D:"\u0110",e:"\u00e9|\u00e8|\u1ebb|\u1ebd|\u1eb9|\u00ea|\u1ebf|\u1ec1|\u1ec3|\u1ec5|\u1ec7|\u00eb", | ||
E:"\u00c9|\u00c8|\u1eba|\u1ebc|\u1eb8|\u00ca|\u1ebe|\u1ec0|\u1ec2|\u1ec4|\u1ec6|\u00cb",i:"\u00ed|\u00ec|\u1ec9|\u0129|\u1ecb|\u00ef|\u00ee",I:"\u00cd|\u00cc|\u1ec8|\u0128|\u1eca|\u00cf|\u00ce",o:"\u00f3|\u00f2|\u1ecf|\u00f5|\u1ecd|\u00f4|\u1ed1|\u1ed3|\u1ed5|\u1ed7|\u1ed9|\u01a1|\u1edb|\u1edd|\u1edf|\u1ee1|\u1ee3|\u00f6",O:"\u00d3|\u00d2|\u1ece|\u00d5|\u1ecc|\u00d4|\u1ed0|\u1ed2|\u1ed4|\u00d4|\u1ed8|\u01a0|\u1eda|\u1edc|\u1ede|\u1ee0|\u1ee2|\u00d6",u:"\u00fa|\u00f9|\u1ee7|\u0169|\u1ee5|\u01b0|\u1ee9|\u1eeb|\u1eed|\u1eef|\u1ef1|\u00fb", | ||
U:"\u00da|\u00d9|\u1ee6|\u0168|\u1ee4|\u01af|\u1ee8|\u1eea|\u1eec|\u1eee|\u1ef0|\u00db",y:"\u00fd|\u1ef3|\u1ef7|\u1ef9|\u1ef5",Y:"\u00dd|\u1ef2|\u1ef6|\u1ef8|\u1ef4"},f={},k;for(k in b)f.key=k,t(b,f.key)&&b[f.key].split("|").forEach(function(b){return function(c){a=w(a,c,b.key)}}(f)),f={key:f.key};return a},y=function(){return new Date},I=function(){return Date.now()},K=function(){var a="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),b="January February March April May June July August September October November December".split(" "), | ||
e=function(){var a=y().getTimezoneOffset();return["GMT",0>a?"+":"-",F(Math.abs(a/60),4)].join("")}(),f=function(c,f){f=void 0===f?I():f;var g=u(f)?f:new Date(f);if(!u(g))return"Invalid input!";c&&d(c)||(c="D, M d, Y h:i:s A");var k=c.match(/(\.*)a{1}(\.*)*/i),h=function(a){return String(10>a?"0"+a:a)},m={Y:function(){return g.getFullYear()},y:function(){return(m.Y()+"").slice(-2)},F:function(){return b[m.n()-1]},M:function(){return(m.F()+"").slice(0,3)},m:function(){return h(m.n())},n:function(){return g.getMonth()+ | ||
1},S:function(){var a=m.j()+" ",a=a.charAt(a.length-2);return"1"===a?"st":"2"===a?"nd":"3"===a?"rd":"th"},j:function(){return g.getDate()},d:function(){return h(m.j())},t:function(){return(new Date(m.Y(),m.n(),0)).getDate()},w:function(){return g.getDay()},l:function(){return a[m.w()]},D:function(){return(m.l()+"").slice(0,3)},G:function(){return g.getHours()},g:function(){return m.G()%12||12},h:function(){return h(k?m.g():m.G())},i:function(){return h(g.getMinutes())},s:function(){return h(g.getSeconds())}, | ||
a:function(){return 11<m.G()?"pm":"am"},A:function(){return m.a().toUpperCase()},O:function(){return e}};return c.replace(/\.*\\?([a-z])/gi,function(a,b){return m[a]?m[a]():b})};return{utc:function(a){return(new Date(a||y())).toUTCString()},local:function(a){return f("D, j M Y h:i:s O",a)},strtotime:function(a){return(new Date(a)).getTime()},format:f,relativize:function(a){var b=a instanceof Date?a:new Date(a);a=y()-b;b=parseInt(b,10);isNaN(b)&&(b=0);if(a<=b)return"Just now";var b=null,c={millisecond:1, | ||
second:1E3,minute:60,hour:60,day:24,month:30,year:12},d;for(d in c)if(a<c[d])break;else b=d,a/=c[d];a=Math.floor(a);1!==a&&(b+="s");return[a,b].join(" ")+" ago"}}}();return{ENV:b,id:G(),isUndefined:function(a){return!1},isNull:function(a){return null===a},isString:d,isNumber:e,isInteger:f,isBoolean:function(a){return!0===a||!1===a},isArray:k,isObject:n,isDate:u,isFunction:function(a){return"function"===typeof a},isElement:function(a){if(a&&"node"===b&&a._root)return!0;var c=Object.prototype.toString.call(a); | ||
return"object"===typeof a&&c.includes("HTML")&&c.includes("Element")},isEmpty:z,isLetter:function(a){var b=/^[a-z]+$/i;return d(a)&&b.test(a)},isEmail:function(a){var b=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;return d(a)&&b.test(a)},isGeneratedKey:function(a){var b=/^[A-Z0-9]+$/i;return d(a)&&b.test(a)},hasProperty:t,equals:A,encode:function(a){return d(a)?encodeURIComponent(a):""},decode:function(a){return d(a)?decodeURIComponent(a.replace(/\+/g," ")): | ||
""},trim:v,truncate:function(a,b){a=v(a);if(!a)return a;b=b||140;if(a.length<=b)return a;var c=a.substring(0,b),d=c.split(" "),e="";1<d.length?(d.pop(),e+=d.join(" "),e.length<a.length&&(e+="...")):(c=c.substring(0,b-3),e=c+"...");return e},stripTags:function(a){if(!d(a))return"";(a=a.replace(/<.*?>/gi," "))&&(a=v(a.replace(/\s\s+/g," ")));return a},escapeHTML:function(a){return d(a)?a.replace(/&/g,"\x26amp;").replace(/</g,"\x26lt;").replace(/>/g,"\x26gt;").replace(/"/g,"\x26quot;"):""},unescapeHTML:function(a){return d(a)? | ||
a.replace(/"/g,'"').replace(/</g,"\x3c").replace(/>/g,"\x3e").replace(/&/g,"\x26"):""},ucfirst:E,ucwords:function(a){if(!d(a))return"";var b=[];a.split(" ").forEach(function(a){b.push(E(a))});return b.join(" ")},leftPad:F,rightPad:function(a,b,d){b=void 0===b?2:b;a=String(a);return a.length>=b?a:a+Array(b-a.length+1).join(void 0===d?"0":d)},repeat:function(a,b){if(!a||!d(a))return"";if(!f(b)||1>b)return a;var c=[];c.length=b;return c.fill(a,0,b).join("")},replaceAll:w,stripAccent:H, | ||
createAlias:function(a,b){a=String(a);if(a=H(a))b=b||"-",a=a.toLowerCase(),a=v(a),a=a.replace(/\W+/g," "),a=a.replace(/\s+/g," "),a=a.replace(/\s/g,b);return a},compile:function(a,b){var c=[],e=function(a,b,f){f&&c.push(f);f=[];for(var g in b)if(t(b,g)){var h=b[g];if(n(h)||k(h))f.push({key:g,data:h});else if(d(h)){var h=w(h,["{","}"],["\x26#123;","\x26#125;"]),l=c.concat([g]),l=new RegExp("{"+l.join(".")+"}","gi");a=a.replace(l,h)}}0<f.length&&f.forEach(function(b){a=e(a,b.data,b.key)});return v(a, | ||
!0)};return b&&(d(b)||n(b)||k(b))?e(a,b):a},md5:J,createId:G,random:D,stabilize:q,now:y,time:I,date:K}}); |
{ | ||
"version": "5.4.24", | ||
"version": "6.0.0", | ||
"name": "bellajs", | ||
@@ -4,0 +4,0 @@ "description": "A useful helper for any javascript program", |
363
README.md
@@ -19,3 +19,3 @@ BellaJS | ||
* [DataType detection](#datatype-detection) | ||
* [Array & Object](#array--object) | ||
* [Immutable array/object](#array--object) | ||
* [String manipulation](#string-manipulation) | ||
@@ -49,96 +49,292 @@ * [Template compiler](#template-compiler) | ||
### DataType detection | ||
- .isArray(Anything val) | ||
- .isBoolean(Anything val) | ||
- .isDate(Anything val) | ||
- .isDef(Anything val) | ||
- .isElement(Anything val) | ||
- .isEmail(Anything val) | ||
- .isEmpty(Anything val) | ||
- .isFunction(Anything val) | ||
- .isGeneratedKey(Anything val) | ||
- .isInteger(Anything val) | ||
- .isLetter(Anything val) | ||
- .isNull(Anything val) | ||
- .isNumber(Anything val) | ||
- .isObject(Anything val) | ||
- .isString(Anything val) | ||
- .isArray(Anything val): check if val is Array | ||
- .isBoolean(Anything val): check if val is Boolean | ||
- .isDate(Anything val): check if val is Date | ||
- .isElement(Anything val): check if val is likely a DOM element | ||
- .isEmail(Anything val): check if val is well-format email address | ||
- .isEmpty(Anything val): check if val is [], {} or '' | ||
- .isFunction(Anything val): check if val is Function | ||
- .isGeneratedKey(Anything val): check if val is a unique key generated by using bella.createId method | ||
- .isInteger(Anything val): check if val is an integer | ||
- .isLetter(Anything val): check if val is letter | ||
- .isNull(Anything val): check if val is null | ||
- .isNumber(Anything val): check if val is Number | ||
- .isObject(Anything val): check if val is Object | ||
- .isString(Anything val): check if val is String | ||
- .isUndefined(Anything val): check if val is Undefined | ||
### Immutable Array & Object | ||
### Array & Object | ||
- .clone(Array|Object|Date o) | ||
- .contains(Array a, String|Object search [, String key]) | ||
- .copies(Array|Object src, Array|Object dest [, Boolean mustMatch[, Array exclude] ]) | ||
- .empty(Array|Object|Element|String o) | ||
- .equals(Anything a, Anything b) | ||
- .hasProperty(Array|Object o, String key) | ||
- .max(Array a) | ||
- .min(Array a) | ||
- .pick(Array a [, Number count]) | ||
- .random([Number min [, Number max]]) | ||
- .sort(Array a [, String order | Object option ]) | ||
- .shuffle(Array a) | ||
- .unique(Array a) | ||
- .first(Array a) | ||
- .last(Array a) | ||
- .getIndex(String|Object element, Array a) | ||
- .getLastIndex(String|Object element, Array a) | ||
- .stabilize(Array | Object val): return immutable version of val | ||
*How to use Bella.sort?* | ||
This library provides a function named "stabilize" that you can pass through it an object or array to get back immutable version. | ||
``` | ||
var a = [1, 5, 19, 6, 4, 11, 7, 22, 40, 3, 8]; | ||
console.log('Array a, default:'); | ||
console.log(a); | ||
Here is an example in Node.js: | ||
console.log('Array a, from lower to higher:'); | ||
var a1 = Bella.sort(a); | ||
console.log(a1); | ||
console.log('Array a, descendant:'); | ||
var a2 = Bella.sort(a, -1); | ||
console.log(a2); | ||
``` | ||
var stabilize = require('bellajs').stabilize; | ||
var players = [ | ||
{ | ||
'name': 'Jerome Nash', | ||
'age': 24 | ||
}, | ||
{ | ||
'name': 'Jackson Valdez', | ||
'age': 21 | ||
}, | ||
{ | ||
'name': 'Benjamin Cole', | ||
'age': 23 | ||
}, | ||
{ | ||
'name': 'Manuel Delgado', | ||
'age': 33 | ||
}, | ||
{ | ||
'name': 'Caleb McKinney', | ||
'age': 28 | ||
} | ||
]; | ||
let user = stabilize({ | ||
name: 'Bob', | ||
age: 17 | ||
}); | ||
console.log('\nList of players as it is:'); | ||
players.forEach(function(item){ | ||
console.log([item.name, item.age].join(' | ')); | ||
}); | ||
// user now is immutable | ||
console.log(user); | ||
console.log('\nSort by age from young to old:'); | ||
var players1 = Bella.sort(players, 'age'); | ||
players1.forEach(function(item){ | ||
console.log([item.name, item.age].join(' | ')); | ||
}); | ||
// access the properties with get() or dot | ||
let name = user.get('name'); // similar to user.name | ||
console.log(name); | ||
console.log('\nAnd then reverse them:'); | ||
var players2 = Bella.sort(players, {age: -1}); | ||
players2.forEach(function(item){ | ||
console.log([item.name, item.age].join(' | ')); | ||
}); | ||
// you can change properties' value with set() method | ||
// it will return a copy of user with new property | ||
``` | ||
let guest = user.set('name', 'Tom'); | ||
console.log(guest.name); // => Tom | ||
// the value of user.name can not be changed | ||
console.log(user.name); // => Bob | ||
// because it's immutable | ||
user.name = 'Jerry'; | ||
console.log(user.name); // => Bob | ||
``` | ||
#### Stabilize an object | ||
``` | ||
stabilize(Object o); | ||
``` | ||
Return an immutable object that has 2 methods "set()" and "get()". | ||
Because the returned object is standard object, so you can still use the built-in methods in [Object.prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype) as normal. | ||
##### .get(key) | ||
Return value of specified property. | ||
##### .set(key, value) | ||
Return an new immutable object with new property. | ||
Setter also accepts an object to allow to define many properties at the same time: | ||
``` | ||
let car = stabilize({ | ||
name: '', | ||
speed: 1000, | ||
color: 'black' | ||
}); | ||
let tesla = car.set({ | ||
name: 'Tesla', | ||
price: 40000 | ||
}); | ||
console.log(tesla); | ||
``` | ||
*tesla* now is a new object with the following properties: | ||
``` | ||
{ | ||
name: 'Tesla', | ||
speed: 1000, | ||
color: 'black', | ||
price: 40000 | ||
} | ||
``` | ||
#### Stabilize an array | ||
``` | ||
stabilize(Array a); | ||
``` | ||
Return an immutable array that has the new methods: *unique*, *first*, *last*, *insert*, *append*, *remove*, *isort* and *ireverse*. All these methods return the immutable data. | ||
Because the returned array is standard array, so you can still use the built-in methods in [Array.prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) as normal. | ||
##### .unique(key) | ||
Return a new array with no duplicate elements. | ||
``` | ||
let a = stabilize([1, 2, 2, 3, 2, 4]); | ||
let b = a.unique(); | ||
console.log(b); // => [ 1, 2, 3, 4 ] | ||
``` | ||
##### .min() | ||
Return the smallest value from an array of numbers. | ||
``` | ||
let a = stabilize([1, 2, 2, 3, 8, 5, 2, 4]); | ||
let b = a.min(); | ||
console.log(b); // => 1 | ||
``` | ||
##### .max() | ||
Return the biggest value from an array of numbers. | ||
``` | ||
let a = stabilize([1, 2, 2, 3, 8, 5, 2, 4]); | ||
let b = a.max(); | ||
console.log(b); // => 1 | ||
``` | ||
##### .first() | ||
Return the first element from array. | ||
``` | ||
let a = stabilize([1, 2, 2, 3, 2, 4]); | ||
let b = a.first(); | ||
console.log(b); // => 1 | ||
``` | ||
##### .last() | ||
Return the last element from array. | ||
``` | ||
let a = stabilize([1, 2, 2, 3, 2, 4]); | ||
let b = a.last(); | ||
console.log(b); // => 4 | ||
``` | ||
##### .pick(count) | ||
Extract *count* elements from array in randomly order. | ||
``` | ||
let a = stabilize([1, 2, 2, 3, 2, 4]); | ||
let b = a.pick(3); | ||
console.log(b); // output an array of 3 random elements | ||
``` | ||
Without *count*, this method returns a random element. | ||
##### .insert(at, element1, element2, ...elementN) | ||
Return a new array with new elements inserted at the position specified by first parameter. | ||
``` | ||
let a = stabilize([1, 2, 3, 4]); | ||
let b = a.insert(2, 'a'); | ||
console.log(b); // => [ 1, 2, 'a', 3, 4 ] | ||
``` | ||
##### .append(element1, element2, ...elementN) | ||
Return a new array with new elements added at the end. | ||
``` | ||
let a = stabilize([1, 2, 3, 4]); | ||
let b = a.append(5, 6, 7); | ||
console.log(b); // => [ 1, 2, 3, 4, 5, 6, 7 ] | ||
``` | ||
##### .remove(start, count) | ||
Return a new array with *count* elements deleted beginning at *start*: | ||
``` | ||
let a = stabilize([1, 2, 3, 4, 5, 6]); | ||
let b = a.remove(3, 2); // remove 2 items from index 3, means 4 and 5 | ||
console.log(b); // => [ 1, 2, 3, 6 ] | ||
``` | ||
##### .isort(compareFunction) | ||
Return a new array sorted by *compareFunction*. | ||
This method does the same thing as [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort), but immutable. | ||
``` | ||
let users = stabilize([ | ||
{ | ||
name: "Bob", | ||
age: 28 | ||
}, | ||
{ | ||
name: "Anne", | ||
age: 21 | ||
}, | ||
{ | ||
name: "Jim", | ||
age: 33 | ||
}, | ||
{ | ||
name: "Kate", | ||
age: 17 | ||
} | ||
]); | ||
let sortedUsers = users.isort((a, b) => { | ||
let ag = a.age; | ||
let bg = b.age; | ||
if (ag === bg) { | ||
return 0; | ||
} | ||
return ag < bg ? -1 : 1; | ||
}); | ||
console.log(sortedUsers); | ||
``` | ||
Output: | ||
``` | ||
[ { name: 'Kate', age: 17 }, | ||
{ name: 'Anne', age: 21 }, | ||
{ name: 'Bob', age: 28 }, | ||
{ name: 'Jim', age: 33 } ] | ||
``` | ||
##### .ireverse() | ||
This method does the same thing as [Array.reverse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse), but immutable. | ||
For example now you can reverse the above *sortedUsers* array: | ||
``` | ||
let reversedUsers = sortedUsers.ireverse(); | ||
console.log(reversedUsers); | ||
``` | ||
Output: | ||
``` | ||
[ { name: 'Jim', age: 33 }, | ||
{ name: 'Bob', age: 28 }, | ||
{ name: 'Anne', age: 21 }, | ||
{ name: 'Kate', age: 17 } ] | ||
``` | ||
##### .shuffle() | ||
Return a clone of given array with shuffled elements. | ||
``` | ||
let shuffledUsers = sortedUsers.shuffled(); | ||
console.log(shuffledUsers); | ||
``` | ||
### String manipulation | ||
@@ -150,4 +346,2 @@ - .createId(Number length [, String prefix]) | ||
- .trim(String s) | ||
- .strtolower(String s) | ||
- .strtoupper(String s) | ||
- .ucfirst(String s) | ||
@@ -163,3 +357,2 @@ - .ucwords(String s) | ||
- .repeat(String s, Number times) | ||
- .warn(String message) | ||
- .replaceAll(String s, String|Array search, String|Array replace) | ||
@@ -166,0 +359,0 @@ - .md5(String s) |
508
src/main.js
@@ -10,3 +10,3 @@ /** | ||
} else { | ||
let root = window || {}; | ||
let root = window || global || {}; | ||
if (root.define && root.define.amd) { | ||
@@ -22,55 +22,22 @@ root.define([], factory); | ||
const ENV = typeof module !== 'undefined' && module.exports ? 'node' : 'browser'; | ||
const MAX_NUMBER = 9007199254740991; | ||
var tof = (v) => { | ||
let ots = Object.prototype.toString; | ||
let s = typeof v; | ||
if (s === 'object') { | ||
if (v) { | ||
if (ots.call(v).indexOf('HTML') !== -1 && ots.call(v).indexOf('Element') !== -1) { | ||
return 'element'; | ||
} | ||
if (v instanceof Array || | ||
( | ||
!(v instanceof Object) && | ||
ots.call(v) === '[object Array]' || | ||
typeof v.length === 'number' && typeof v.splice !== 'undefined' && | ||
typeof v.propertyIsEnumerable !== 'undefined' && !v.propertyIsEnumerable('splice') | ||
) | ||
) { | ||
return 'array'; | ||
} | ||
if (!(v instanceof Object) && | ||
(ots.call(v) === '[object Function]' || | ||
typeof v.call !== 'undefined' && | ||
typeof v.propertyIsEnumerable !== 'undefined' && | ||
!v.propertyIsEnumerable('call') | ||
) | ||
) { | ||
return 'function'; | ||
} | ||
} | ||
return 'object'; | ||
} else if (s === 'function' && typeof v.call === 'undefined') { | ||
return 'object'; | ||
} | ||
return s; | ||
}; | ||
const UNDEF = undefined; // eslint-disable-line no-undefined | ||
var isDef = (val) => { | ||
return tof(val) !== 'undefined'; | ||
}; | ||
const ENV = typeof module !== UNDEF && module.exports ? 'node' : 'browser'; | ||
var isNull = (val) => { | ||
return tof(val) === null || val === null; | ||
return val === null; | ||
}; | ||
var isUndefined = (val) => { | ||
return typeof val === UNDEF; | ||
}; | ||
var isString = (val) => { | ||
return !isNull(val) && tof(val) === 'string'; | ||
return !isNull(val) && typeof val === 'string'; | ||
}; | ||
var isNumber = (val) => { | ||
return val !== '' && !isNull(val) && isDef(val) && !isNaN(val) && tof(val) === 'number'; | ||
return val !== '' && !isNull(val) && !isUndefined(val) && !isNaN(val) && typeof val === 'number'; | ||
}; | ||
@@ -87,7 +54,7 @@ | ||
var isArray = (val) => { | ||
return !isNull(val) && tof(val) === 'array'; | ||
return !isNull(val) && Array.isArray(val); | ||
}; | ||
var isObject = (val) => { | ||
return !isNull(val) && tof(val) === 'object'; | ||
return val !== null && typeof val === 'object' && isArray(val) === false; | ||
}; | ||
@@ -100,3 +67,3 @@ | ||
var isFunction = (val) => { | ||
return !isNull(val) && tof(val) === 'function'; | ||
return typeof val === 'function'; | ||
}; | ||
@@ -108,3 +75,5 @@ | ||
} | ||
return !isNull(val) && tof(val) === 'element'; | ||
let ots = Object.prototype.toString; | ||
let scall = ots.call(val); | ||
return typeof val === 'object' && scall.includes('HTML') && scall.includes('Element'); | ||
}; | ||
@@ -128,3 +97,3 @@ | ||
var isEmpty = (val) => { | ||
return !isDef(val) || isNull(val) || | ||
return !val || isUndefined(val) || isNull(val) || | ||
isString(val) && val === '' || | ||
@@ -139,7 +108,3 @@ isArray(val) && JSON.stringify(val) === '[]' || | ||
} | ||
let r = true; | ||
if (!isDef(ob[k])) { | ||
r = k in ob; | ||
} | ||
return r; | ||
return Object.prototype.hasOwnProperty.call(ob, k); | ||
}; | ||
@@ -263,13 +228,8 @@ | ||
} | ||
return s.replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&'); | ||
return s.replace(/"/g, '"') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/&/g, '&'); | ||
}; | ||
var strtolower = (s) => { | ||
return isString(s) ? s.toLowerCase() : ''; | ||
}; | ||
var strtoupper = (s) => { | ||
return isString(s) ? s.toUpperCase() : ''; | ||
}; | ||
var ucfirst = (s) => { | ||
@@ -298,14 +258,10 @@ if (!isString(s)) { | ||
var leftPad = (s, size, spad) => { | ||
var leftPad = (s, size = 2, pad = '0') => { | ||
let o = String(s); | ||
let z = size || 2; | ||
let g = spad || '0'; | ||
return o.length >= z ? o : new Array(z - o.length + 1).join(g) + o; | ||
return o.length >= size ? o : new Array(size - o.length + 1).join(pad) + o; | ||
}; | ||
var rightPad = (s, size, spad) => { | ||
var rightPad = (s, size = 2, pad = '0') => { | ||
let o = String(s); | ||
let z = size || 2; | ||
let g = spad || '0'; | ||
return o.length >= z ? o : o + new Array(z - o.length + 1).join(g); | ||
return o.length >= size ? o : o + new Array(size - o.length + 1).join(pad); | ||
}; | ||
@@ -321,6 +277,4 @@ | ||
let a = []; | ||
while (a.length < m) { | ||
a.push(s); | ||
} | ||
return a.join(''); | ||
a.length = m; | ||
return a.fill(s, 0, m).join(''); | ||
}; | ||
@@ -332,3 +286,3 @@ | ||
var createId = (leng, prefix) => { | ||
var createId = (leng, prefix = '') => { | ||
let a = []; | ||
@@ -340,5 +294,4 @@ while (a.length < 10) { | ||
let t = r.length; | ||
let px = prefix || ''; | ||
let ln = Math.max(leng || 32, px.length); | ||
let s = px; | ||
let ln = Math.max(leng || 32, prefix.length); | ||
let s = prefix; | ||
while (s.length < ln) { | ||
@@ -367,198 +320,181 @@ let k = Math.floor(Math.random() * t); | ||
let range = max - min + 1; | ||
let rd = Math.floor(Math.random() * range) + offset; | ||
return rd; | ||
return Math.floor(Math.random() * range) + offset; | ||
}; | ||
var max = (a) => { | ||
return isArray(a) ? Math.max.apply({}, a) : a; | ||
}; | ||
var stabilize = (() => { | ||
var min = (a) => { | ||
return isArray(a) ? Math.min.apply({}, a) : a; | ||
}; | ||
var astabilize = (data = []) => { | ||
var empty = (a) => { | ||
if (isArray(a)) { | ||
for (let i = a.length - 1; i >= 0; i--) { | ||
a[i] = null; | ||
delete a[i]; | ||
} | ||
a.length = 0; | ||
} else if (isObject(a)) { | ||
for (let k in a) { | ||
if (hasProperty(a, k)) { | ||
a[k] = null; | ||
delete a[k]; | ||
let a = [...data]; | ||
let unique = () => { | ||
let arr = [...a]; | ||
let r = []; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (r.indexOf(arr[i]) === -1) { | ||
r.push(arr[i]); | ||
} | ||
} | ||
} | ||
} else if (isString(a)) { | ||
a = ''; | ||
} else if (isElement(a)) { | ||
a.innerHTML = ''; | ||
} | ||
return a; | ||
}; | ||
return stabilize(r); | ||
}; | ||
var unique = (a) => { | ||
if (isArray(a)) { | ||
let r = []; | ||
for (let i = 0; i < a.length; i++) { | ||
if (r.indexOf(a[i]) === -1) { | ||
r.push(a[i]); | ||
let min = () => { | ||
return Math.min.apply({}, a); | ||
}; | ||
let max = () => { | ||
return Math.max.apply({}, a); | ||
}; | ||
let first = () => { | ||
let r = [...a][0]; | ||
return stabilize(r); | ||
}; | ||
let last = () => { | ||
let r = [...a][a.length - 1]; | ||
return stabilize(r); | ||
}; | ||
let insert = (at = 0, ...items) => { | ||
let r = [...a]; | ||
let p0 = r.slice(0, at); | ||
let p1 = r.slice(at, r.length); | ||
return stabilize([].concat(p0, ...items, p1)); | ||
}; | ||
let append = (...items) => { | ||
return insert(a.length, items); | ||
}; | ||
let remove = (start = 0, count = 0) => { | ||
let r = [...a.slice(0, start), ...a.slice(start + count)]; | ||
return stabilize(r); | ||
}; | ||
let isort = (fn) => { | ||
let r = [...a].sort(fn); | ||
return stabilize(r); | ||
}; | ||
let ireverse = () => { | ||
let r = [...a].reverse(); | ||
return stabilize(r); | ||
}; | ||
let shuffle = () => { | ||
return isort(() => { | ||
return Math.random() - 0.5; | ||
}); | ||
}; | ||
let pick = (count = 1) => { | ||
let b = a.shuffle(); | ||
let c = Math.max(Math.min(count, b.length), 1); | ||
if (c >= b.length) { | ||
return b; | ||
} | ||
} | ||
return r; | ||
} | ||
return a || []; | ||
}; | ||
var contains = (a, el, key) => { | ||
if (isArray(a)) { | ||
for (let i = 0; i < a.length; i++) { | ||
var val = a[i]; | ||
if (key && val[key] === el[key] || val === el) { | ||
return true; | ||
if (c === 1) { | ||
let ri = random(0, b.length - 1); | ||
return b[ri]; | ||
} | ||
} | ||
} else if (isObject(a) && isString(el)) { | ||
return hasProperty(a, el); | ||
} else if (isString(a) && isString(el)) { | ||
return a.includes(el); | ||
} | ||
return false; | ||
}; | ||
var first = (a) => { | ||
return a[0]; | ||
}; | ||
let d = []; | ||
while (d.length < c) { | ||
let i = random(0, b.length - 1); | ||
d.push(b[i]); | ||
b = b.splice(i, 1); | ||
} | ||
return d; | ||
}; | ||
var last = (a) => { | ||
return a[a.length - 1]; | ||
}; | ||
let addMethods = (met) => { | ||
Object.defineProperty(a, met[0], { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: met[1] | ||
}); | ||
}; | ||
var getIndex = (arr, item) => { | ||
let r = -1; | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] === item) { | ||
r = i; | ||
break; | ||
} | ||
} | ||
return r; | ||
}; | ||
[ | ||
['min', min], | ||
['max', max], | ||
['unique', unique], | ||
['first', first], | ||
['last', last], | ||
['pick', pick], | ||
['insert', insert], | ||
['append', append], | ||
['remove', remove], | ||
['isort', isort], | ||
['ireverse', ireverse], | ||
['shuffle', shuffle] | ||
].map(addMethods); | ||
var getLastIndex = (arr, item) => { | ||
let r = -1; | ||
for (let i = arr.length - 1; i >= 0; i--) { | ||
if (arr[i] === item) { | ||
r = i; | ||
break; | ||
} | ||
} | ||
return r; | ||
}; | ||
return a; | ||
}; | ||
var clone = (obj) => { | ||
if (!(obj instanceof Object)) { | ||
return obj; | ||
} | ||
let output; | ||
let Constructor = obj.constructor; | ||
switch (Constructor) { | ||
case RegExp: | ||
output = new Constructor(obj); | ||
break; | ||
case Date: | ||
output = new Constructor(obj.getTime()); | ||
break; | ||
default: | ||
output = new Constructor(); | ||
} | ||
for (let prop in obj) { // eslint-disable-line guard-for-in | ||
output[prop] = clone(obj[prop]); | ||
} | ||
return output; | ||
}; | ||
var ostabilize = (data = {}) => { | ||
let o = Object.create({}); | ||
var copies = (source, dest, matched, excepts) => { | ||
let mt = matched || false; | ||
let ex = excepts || []; | ||
for (let k in source) { | ||
if (ex.length > 0 && contains(ex, k)) { | ||
continue; // eslint-disable-line no-continue | ||
} | ||
if (!mt || mt && dest.hasOwnProperty(k)) { | ||
let oa = source[k]; | ||
let ob = dest[k]; | ||
if (isObject(ob) && isObject(oa) || isArray(ob) && isArray(oa)) { | ||
dest[k] = copies(oa, dest[k], mt, ex); | ||
} else { | ||
dest[k] = clone(oa); | ||
let config = { | ||
enumerable: true, | ||
configurable: false, | ||
writable: false, | ||
value: 'undefined' | ||
}; | ||
let setProp = (key) => { | ||
let c = Object.assign({}, config); | ||
c.value = data[key]; | ||
Object.defineProperty(o, key, c); | ||
}; | ||
Object.keys(data).map(setProp); | ||
Object.defineProperty(o, 'get', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: (k) => { | ||
return o[k]; | ||
} | ||
} | ||
} | ||
return dest; | ||
}; | ||
}); | ||
var sort = (arr, opts) => { | ||
let a = []; | ||
let one = {}; | ||
let o = opts || 1; | ||
if (isArray(arr) && arr.length > 0) { | ||
a = clone(arr); | ||
one = a[0]; | ||
if (o === 1 || o === -1) { | ||
a.sort((m, n) => { | ||
return m > n ? o : m < n ? -1 * o : 0; // eslint-disable-line no-nested-ternary | ||
}); | ||
} else if (isString(o) && hasProperty(one, o)) { | ||
a.sort((m, n) => { | ||
return m[o] > n[o] ? 1 : m[o] < n[o] ? -1 : 0; // eslint-disable-line no-nested-ternary | ||
}); | ||
} else if (isObject(o)) { | ||
for (let key in o) { | ||
if (hasProperty(one, key)) { | ||
let order = o[key] === -1 ? -1 : 1; | ||
/*eslint-disable*/ | ||
a.sort((m, n) => { | ||
return (m[key] > n[key]) ? order : (m[key] < n[key] ? (-1 * order) : 0); | ||
Object.defineProperty(o, 'set', { | ||
enumerable: false, | ||
configurable: false, | ||
writable: false, | ||
value: (key, value = false) => { | ||
let a = Object.assign({}, o); | ||
let _set = (k, v) => { | ||
a[k] = v; | ||
}; | ||
if (isObject(key)) { | ||
Object.keys(key).forEach((k) => { | ||
_set(k, key[k]); | ||
}); | ||
/*eslint-enable*/ | ||
} else { | ||
_set(key, value); | ||
} | ||
return stabilize(a); | ||
} | ||
} | ||
} | ||
return a; | ||
}; | ||
}); | ||
var shuffle = (arr) => { | ||
let a = clone(arr); | ||
a.sort(() => { | ||
return Math.random() - 0.5; | ||
}); | ||
return a; | ||
}; | ||
return o; | ||
}; | ||
var pick = (arr, count) => { | ||
let c = count ? Math.min(count, arr.length) : 1; | ||
if (c < 1) { | ||
c = 1; | ||
} | ||
let a = shuffle(arr); | ||
if (c >= arr.length) { | ||
return a; | ||
} | ||
if (c === 1) { | ||
let ri = random(0, arr.length - 1); | ||
return a[ri]; | ||
} | ||
let b = []; | ||
while (b.length < c) { | ||
let i = random(0, a.length - 1); | ||
b.push(a[i]); | ||
a.splice(i, 1); | ||
} | ||
return b; | ||
}; | ||
return (data) => { | ||
if (isArray(data)) { | ||
return astabilize(data); | ||
} | ||
if (isObject(data)) { | ||
return ostabilize(data); | ||
} | ||
return data; | ||
}; | ||
})(); | ||
@@ -631,8 +567,13 @@ /*eslint-disable*/ | ||
}; | ||
let updateS = (ai, key) => { | ||
s = replaceAll(s, ai, key); | ||
}; | ||
for (let key in map) { | ||
if (hasProperty(map, key)) { | ||
let a = map[key].split('|'); | ||
for (let i = 0; i < a.length; i++) { | ||
s = replaceAll(s, a[i], key); | ||
} | ||
a.forEach((item) => { | ||
return updateS(item, key); | ||
}); | ||
} | ||
@@ -648,3 +589,3 @@ } | ||
let d = delimiter || '-'; | ||
x = strtolower(x); | ||
x = x.toLowerCase(); | ||
x = trim(x); | ||
@@ -721,8 +662,12 @@ x = x.replace(/\W+/g, ' '); | ||
var format = (output, timestamp) => { | ||
let meridiem = false; | ||
let d, f; | ||
var format = (output, input = time()) => { | ||
let vchar = /\.*\\?([a-z])/gi; | ||
let input = timestamp ? new Date(timestamp).getTime() : time(); | ||
let d = isDate(input) ? input : new Date(input); | ||
if (!isDate(d)) { | ||
return 'Invalid input!'; | ||
} | ||
if (!output || !isString(output)) { | ||
@@ -732,5 +677,3 @@ output = pattern; | ||
if (output.match(/(\.*)a{1}(\.*)*/i)) { | ||
meridiem = true; | ||
} | ||
let meridiem = output.match(/(\.*)a{1}(\.*)*/i); | ||
@@ -757,19 +700,4 @@ let wn = weeks; | ||
var _term = (t, s) => { | ||
return f[t] ? f[t]() : s; | ||
}; | ||
d = input instanceof Date ? input : new Date(input); | ||
if (isNaN(d.getTime())) { | ||
let reg = /^(\d+-\d+-\d+)\s(\d+:\d+:\d+)$/i; | ||
if (reg.test(input)) { | ||
d = new Date(input.replace(' ', 'T')); | ||
} else { | ||
return input + ' !'; | ||
} | ||
} | ||
/*eslint-disable */ | ||
f = { | ||
let f = { | ||
Y() { | ||
@@ -840,2 +768,7 @@ return d.getFullYear(); | ||
/*eslint-enable */ | ||
var _term = (t, s) => { | ||
return f[t] ? f[t]() : s; | ||
}; | ||
return output.replace(vchar, _term); | ||
@@ -905,3 +838,3 @@ }; | ||
id: createId(), | ||
isDef, | ||
isUndefined, | ||
isNull, | ||
@@ -930,4 +863,2 @@ isString, | ||
unescapeHTML, | ||
strtolower, | ||
strtoupper, | ||
ucfirst, | ||
@@ -945,16 +876,3 @@ ucwords, | ||
random, | ||
min, | ||
max, | ||
unique, | ||
contains, | ||
first, | ||
last, | ||
getIndex, | ||
getLastIndex, | ||
sort, | ||
shuffle, | ||
pick, | ||
empty, | ||
copies, | ||
clone, | ||
stabilize, | ||
now, | ||
@@ -961,0 +879,0 @@ time, |
@@ -66,3 +66,3 @@ /** | ||
let r4 = bella.date.format('D, M d, Y h:i:s A', 'something'); | ||
let e4 = 'NaN !'; | ||
let e4 = 'Invalid input!'; | ||
assert.deepEqual(r4, e4, `Invalid string input must return ${e4}`); | ||
@@ -74,2 +74,6 @@ | ||
let r6 = bella.date.format('D, M d, Y h:i:s A', '2016-04-18 13:25:XYZ'); | ||
let e6 = 'Invalid input!'; | ||
assert.deepEqual(r6, e6, `With '2016-04-18 13:25:XYZ' must return ${e6}`); | ||
assert.end(); | ||
@@ -76,0 +80,0 @@ }); |
@@ -268,2 +268,3 @@ /** | ||
[ | ||
() => {}, | ||
function x() {}, | ||
@@ -436,5 +437,3 @@ new Function() | ||
'label', | ||
'color', | ||
'__proto__', | ||
'toString' | ||
'color' | ||
]; | ||
@@ -449,3 +448,5 @@ for (let i = 0; i < props.length; i++) { | ||
'year', | ||
'prototype' | ||
'prototype', | ||
'__proto__', | ||
'toString' | ||
]; | ||
@@ -452,0 +453,0 @@ for (let i = 0; i < fails.length; i++) { |
@@ -118,2 +118,4 @@ /** | ||
assert.deepEquals(a1, e1, `bella.stripTags('${x}') must return ${e1}`); | ||
assert.deepEquals(bella.stripTags(1238), '', `bella.stripTags(1238) must return empty string`); | ||
assert.end(); | ||
@@ -144,25 +146,2 @@ }); | ||
// strtolower | ||
test('Testing .strtolower(String s) method', (assert) => { | ||
let x = 'HElLo wOrLd'; | ||
let a1 = bella.strtolower(x); | ||
let e1 = 'hello world'; | ||
assert.deepEquals(a1, e1, `bella.strtolower('${x}') must return ${e1}`); | ||
assert.deepEquals(bella.strtolower({}), '', 'bella.strtolower({}) must return empty string'); | ||
assert.end(); | ||
}); | ||
// strtoupper | ||
test('Testing .strtoupper(String s) method', (assert) => { | ||
let x = 'HElLo wOrLd'; | ||
let a1 = bella.strtoupper(x); | ||
let e1 = 'HELLO WORLD'; | ||
assert.deepEquals(a1, e1, `bella.strtoupper('${x}') must return ${e1}`); | ||
assert.deepEquals(bella.strtoupper({}), '', 'bella.strtoupper({}) must return empty string'); | ||
assert.end(); | ||
}); | ||
// ucfirst | ||
@@ -169,0 +148,0 @@ test('Testing .ucfirst(String s) method', (assert) => { |
@@ -8,3 +8,3 @@ var fs = require('fs'); | ||
var dirs = ['', 'detection', 'utils', 'string', 'date', 'scheduler']; | ||
var dirs = ['', 'detection', 'utils', 'stabilitize', 'string', 'date', 'scheduler']; | ||
@@ -11,0 +11,0 @@ dirs.forEach((dir) => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
202472
26
474
2343