From 35cafb4bac02e0d9edb4507bb1dc12e1523d24fc Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Tue, 26 Apr 2016 11:34:52 +0700 Subject: [PATCH 1/2] Up to v5.1.1 - Add repeat(), warn() methods - Improve clone() method to avoid reference - Mark copies() method as "is deprecated". --- README.md | 7 + dist/bella.min.js | 56 +++---- package.json | 2 +- src/bella.js | 317 +++++++++++++++++++----------------- test/config.js | 2 + test/manual/test.js | 37 +++++ test/specs/string/string.js | 9 + test/specs/utils/utils.js | 20 ++- 8 files changed, 268 insertions(+), 182 deletions(-) create mode 100644 test/manual/test.js diff --git a/README.md b/README.md index 54e60be..d01fea3 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ BellaJS is a lightweight library with several helpful utils. It supports both No # Changes +#### v5.1.1 - Apr 26, 2016 +- Add repeat(), warn() methods +- Improve clone() method to avoid reference +- Mark copies() method as "is deprecated". + #### v5.0.0 - Apr 18, 2016 - Refactor scheduler's features - Rewrite in ES6 syntax @@ -149,6 +154,8 @@ Assuming there is a file bella.min.js located at "/public/js/lib/", the followin - .truncate(String s, Number limit) - .leftPad(String s, Number limit, String pad) - .rightPad(String s, Number limit, String pad) + - .repeat(String s, Number times) + - .warn(String message) - .replaceAll(String s, String|Array search, String|Array replace) - .md5(String s) diff --git a/dist/bella.min.js b/dist/bella.min.js index 1e83ed1..a6b3060 100644 --- a/dist/bella.min.js +++ b/dist/bella.min.js @@ -1,28 +1,28 @@ -(function(){var H="undefined"!==typeof module&&module.exports?"node":"browser",e={ENV:H},w=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},F=function(a){return"undefined"!==w(a)},y=function(a){return null===w(a)||null===a},k=function(a){return!y(a)&&"string"===w(a)},z=function(a){return""!==a&&!y(a)&&F(a)&&!isNaN(a)&&"number"===w(a)},n=function(a){return!y(a)&&"array"===w(a)},p=function(a){return!y(a)&& -"object"===w(a)},C=function(a){return a instanceof Date&&!isNaN(a.valueOf())},O=function(a){return a&&"node"===H&&a._root?!0:!y(a)&&"element"===w(a)},I=function(a){return!F(a)||y(a)||k(a)&&""===a||n(a)&&"[]"===JSON.stringify(a)||p(a)&&"{}"===JSON.stringify(a)},q=function(a,b){if(!a||!b)return!1;var c=!0;F(a[b])||(c=b in a);return c},J=function(a,b){var c=!0;if(I(a)&&I(b))return!0;if(C(a)&&C(b))return a.getTime()===b.getTime();if(z(a)&&z(b)||k(a)&&k(b))return a===b;if(n(a)&&n(b)){if(a.length!==b.length)return!1; -if(0c.length;)c.push(Math.random().toString(36).slice(2));for(var c=c.join(""),d=c.length,f=b||"",g=Math.max(a||32,f.length);f.length< -g;)f+=c.charAt(Math.floor(Math.random()*d))||"";return f},L=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},P=function(a,b,c){if(n(a))for(var d=0;db?f:ab[f]?1:a[f]c[a.key]?a.order:b[a.key]c&&(c=1);if(c>=a.length)return a;if(1===c)return c=L(0,a.length-1),a[c];for(var d=[],f=B(a);d.lengthb;)a[b]=0|4294967296*Math.abs(Math.sin(++b));return function(b){var d,f,g,l,e=[];b=unescape(encodeURI(b));for(var h=b.length,t=[d=1732584193,f=-271733879,~d,~f],x=0;x<=h;)e[x>>2]|=(b.charCodeAt(x)||128)<<8*(x++%4);e[b=16*(h+8>>6)+14]=8*h;for(x=0;xl;)h=[g=h[3],(d=h[1]|0)+((g=h[0]+[d&(f=h[2])|~d&g,g&d|~g&f,d^f^g,f^(d|~g)][h=l>>4]+(a[l]+(e[[l,5*l+1,3*l+5,7*l][h]%16+x]|0)))<<(h=[7,12,17,22,5,9,14, -20,4,11,16,23,6,10,15,21][4*h+l++%4])|g>>>32-h),d,f];for(l=4;l;)t[--l]=t[l]+h[l]}for(b="";32>l;)b+=(t[l>>3]>>4*(1^l++&7)&15).toString(16);return b}}();var D=function(a,b){if(!k(a))return"";var c=a?a.replace(/^[\s\xa0]+|[\s\xa0]+$/g,""):a||"";return c&&b?c.replace(/\r?\n|\r/g," ").replace(/\s\s+|\r/g," "):c},S=function(a){return k(a)?a.toLowerCase():""},T=function(a){if(!k(a))return"";if(1===a.length)return a.toUpperCase();a=a.toLowerCase();return a.charAt(0).toUpperCase()+a.slice(1)},U=function(a, -b,c){a=String(a);b=b||2;return a.length>=b?a:Array(b-a.length+1).join(c||"0")+a},E=function(a,b,c){z(a)&&(a=String(a));if(!a||!k(a))return"";z(b)&&(b=String(b));z(c)&&(c=String(c));if(k(b)&&k(c))a=a.split(b).join(c);else if(n(b)&&k(c))b.forEach(function(b){a=E(a,b,c)});else if(n(b)&&n(c)&&b.length===c.length){var d=b.length;if(0/gi," "))&&(a=D(a.replace(/\s\s+/g," ")));return a};e.escapeHTML=function(a){return k(a)?a.replace(/&/g, -"&").replace(//g,">").replace(/"/g,"""):""};e.unescapeHTML=function(a){return k(a)?a.replace(/"/g,'"').replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"):""};e.strtolower=S;e.strtoupper=function(a){return k(a)?a.toUpperCase():""};e.ucfirst=T;e.ucwords=function(a){if(!k(a))return"";var b=[];a.split(" ").forEach(function(a){b.push(T(a))});return b.join(" ")};e.leftPad=U;e.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")};e.replaceAll=E;e.stripAccent=V;e.createAlias=function(a,b){a=String(a);var c=V(a);if(c)var d=b||"-",c=S(c),c=D(c),c=c.replace(/\W+/g," "),c=c.replace(/\s+/g," "),c=c.replace(/\s/g,d);return c};e.template=function(a,b){var c=[],d=function(a,b,l){l&&c.push(l);l=[];for(var e in b)if(q(b,e)){var h=b[e];if(p(h)||n(h))l.push({key:e,data:h});else if(k(h)){var h=E(h,["{","}"],["{","}"]),t=c.concat([e]),t=new RegExp("{"+t.join(".")+"}","gi");a=a.replace(t,h)}}0a?"+":"-",U(Math.abs(a/60),2)].join("")}(),d=function(f,d){var e=!1,r,h;d=d?(new Date(d)).getTime():A(); -f&&k(f)||(f="D, M d, Y h:i:s A");f.match(/(\.*)a{1}(\.*)*/i)&&(e=!0);var t=function(a){return String(10>a?"0"+a:a)};r=d instanceof Date?d:new Date(d);if(isNaN(r.getTime()))if(/^(\d+-\d+-\d+)\s(\d+:\d+:\d+)$/i.test(d))r=new Date(d.replace(" ","T"));else return d+" !";h={Y:function(){return r.getFullYear()},y:function(){return(h.Y()+"").slice(-2)},F:function(){return b[h.n()-1]},M:function(){return(h.F()+"").slice(0,3)},m:function(){return t(h.n())},n:function(){return r.getMonth()+1},S:function(){var a= -h.j()+" ",a=a.charAt(a.length-2);return"1"===a?"st":"2"===a?"nd":"3"===a?"rd":"th"},j:function(){return r.getDate()},d:function(){return t(h.j())},t:function(){return(new Date(h.Y(),h.n(),0)).getDate()},w:function(){return r.getDay()},l:function(){return a[h.w()]},D:function(){return(h.l()+"").slice(0,3)},G:function(){return r.getHours()},g:function(){return h.G()%12||12},h:function(){return t(e?h.g():h.G())},i:function(){return t(r.getMinutes())},s:function(){return t(r.getSeconds())},a:function(){return 11< -h.G()?"pm":"am"},A:function(){return h.a().toUpperCase()},O:function(){return c}};return f.replace(/\.*\\?([a-z])/gi,function(a,b){return h[a]?h[a]():b})};e.now=N;e.time=A;e.date={utc:function(a){return(new Date(a||N())).toUTCString()},local:function(a){return d("D, j M Y h:i:s O",a)},strtotime:function(a){return(new Date(a)).getTime()},format:d,relativize:function(a){var b=a instanceof Date?a:new Date(a);a=new Date-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(ap)c=new Date(c),c.setDate(c.getDate()+e+7-c.getDay()%7),q=c;q.setHours(f);q.setMinutes(d);q.setSeconds(n);return q.getTime()-g}if(c=a.match(/^(\*|\d+)\s+(\*|\d+)\s+(\*|\d+)\s+(\*|\d+)\s+(\*|\d+)\s+(\d+)$/i)){var g=c,c="*"=== -g[1]?"*":parseInt(g[1],10),e="*"===g[2]?"*":parseInt(g[2],10),f="*"===g[3]?"*":parseInt(g[3],10),d="*"===g[4]?"*":parseInt(g[4],10),n="*"===g[5]?"*":parseInt(g[5],10),v="*"===g[6]?"*":parseInt(g[6],10),u=new Date,m=u.getFullYear();if("*"!==c&&cm&&"*"===n&&(v.setMinutes(w+1), -m=v.getTime(),m-=u);0>m&&"*"===d&&(v.setHours(q+1),m=v.getTime(),m-=u);0>m&&"*"===f&&(v.setDate(p+1),m=v.getTime(),m-=u);0>m&&"*"===e&&(v.setMonth(k),m=v.getTime(),m-=u);0>m&&"*"===c&&(v.setFullYear(g+1),m=v.getTime(),m-=u);c=m}return c}return-1},d=function(b){b.fn();var c=b.id;if(!b.repeat)return a["delete"](c);var d=A();b.lastTick=d;a.set(c,b);return!0},f=function(){if(0f?a["delete"](d):0===f?(b.delay=0,g.push(b)): -(b.delay=f,a.set(d,b),f<=e&&(e=f,d=[],d=g.concat(b),g=d.filter(function(a){return a.delay<=e})))});b&&clearTimeout(b);g.length&&(b=setTimeout(function(){g.map(d);setTimeout(f,1)},e))}},g=function(b,c,d){d=d?0:1;var e=A(),g=K(32);a.set(g,{id:g,fn:c,time:b,repeat:d,createdAt:e,lastTick:e,delay:0});f()};e.scheduler={yearly:function(a,b){g("* "+a,b)},monthly:function(a,b){g("* * "+a,b)},daily:function(a,b){g("* * * "+a,b)},hourly:function(a,b){return g("* * * * "+a,b)},every:function(a,b){return g(a, -b)},once:function(a,b){return g(a,b,1)}}})();if("node"===H)module.exports=e;else{var G=window||{};G.define&&G.define.amd&&G.define(function(){return e});G.Bella=e}})(); \ No newline at end of file +(function(){var G="undefined"!==typeof module&&module.exports?"node":"browser",e={ENV:G},w=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},E=function(a){return"undefined"!==w(a)},y=function(a){return null===w(a)||null===a},k=function(a){return!y(a)&&"string"===w(a)},z=function(a){return""!==a&&!y(a)&&E(a)&&!isNaN(a)&&"number"===w(a)},n=function(a){return!y(a)&&"array"===w(a)},r=function(a){return!y(a)&& +"object"===w(a)},H=function(a){return a instanceof Date&&!isNaN(a.valueOf())},P=function(a){return a&&"node"===G&&a._root?!0:!y(a)&&"element"===w(a)},I=function(a){return!E(a)||y(a)||k(a)&&""===a||n(a)&&"[]"===JSON.stringify(a)||r(a)&&"{}"===JSON.stringify(a)},p=function(a,b){if(!a||!b)return!1;var c=!0;E(a[b])||(c=b in a);return c},J=function(a,b){var c=!0;if(I(a)&&I(b))return!0;if(H(a)&&H(b))return a.getTime()===b.getTime();if(z(a)&&z(b)||k(a)&&k(b))return a===b;if(n(a)&&n(b)){if(a.length!==b.length)return!1; +if(0=b?a:Array(b-a.length+1).join(c||"0")+a},K=function(a,b){for(var c=[];c.length/gi," "))&&(a=C(a.replace(/\s\s+/g," ")));return a}; +e.escapeHTML=function(a){return k(a)?a.replace(/&/g,"&").replace(//g,">").replace(/"/g,"""):""};e.unescapeHTML=function(a){return k(a)?a.replace(/"/g,'"').replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"):""};e.strtolower=Q;e.strtoupper=function(a){return k(a)?a.toUpperCase():""};e.ucfirst=R;e.ucwords=function(a){if(!k(a))return"";var b=[];a.split(" ").forEach(function(a){b.push(R(a))});return b.join(" ")};e.leftPad=S;e.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")};e.repeat=K;e.warn=T;var L=function(a,b){for(var c=[];10>c.length;)c.push(Math.random().toString(36).slice(2));for(var c=c.join(""),d=c.length,f=b||"",g=Math.max(a||32,f.length);f.lengtha)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},U=function(a,b,c){if(n(a))for(var d= +0;db?f:ab[f]?1:a[f]c[a.key]?a.order:b[a.key]c&&(c=1);if(c>=a.length)return a;if(1===c)return c=M(0,a.length-1),a[c];for(var d=[],f=B(a);d.lengthb;)a[b]=0|4294967296*Math.abs(Math.sin(++b));return function(b){var d,f,g,m,e=[];b=unescape(encodeURI(b));for(var h=b.length,t=[d=1732584193, +f=-271733879,~d,~f],x=0;x<=h;)e[x>>2]|=(b.charCodeAt(x)||128)<<8*(x++%4);e[b=16*(h+8>>6)+14]=8*h;for(x=0;xm;)h=[g=h[3],(d=h[1]|0)+((g=h[0]+[d&(f=h[2])|~d&g,g&d|~g&f,d^f^g,f^(d|~g)][h=m>>4]+(a[m]+(e[[m,5*m+1,3*m+5,7*m][h]%16+x]|0)))<<(h=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*h+m++%4])|g>>>32-h),d,f];for(m=4;m;)t[--m]=t[m]+h[m]}for(b="";32>m;)b+=(t[m>>3]>>4*(1^m++&7)&15).toString(16);return b}}();var D=function(a,b,c){z(a)&&(a=String(a));if(!a||!k(a))return"";z(b)&& +(b=String(b));z(c)&&(c=String(c));if(k(b)&&k(c))a=a.split(b).join(c);else if(n(b)&&k(c))b.forEach(function(b){a=D(a,b,c)});else if(n(b)&&n(c)&&b.length===c.length){var d=b.length;if(0a?"+":"-",S(Math.abs(a/60),2)].join("")}(),d=function(f,d){var e=!1,q,h;d=d?(new Date(d)).getTime():A();f&&k(f)||(f="D, M d, Y h:i:s A");f.match(/(\.*)a{1}(\.*)*/i)&&(e=!0);var t=function(a){return String(10>a?"0"+a:a)};q=d instanceof Date?d:new Date(d);if(isNaN(q.getTime()))if(/^(\d+-\d+-\d+)\s(\d+:\d+:\d+)$/i.test(d))q= +new Date(d.replace(" ","T"));else return d+" !";h={Y:function(){return q.getFullYear()},y:function(){return(h.Y()+"").slice(-2)},F:function(){return b[h.n()-1]},M:function(){return(h.F()+"").slice(0,3)},m:function(){return t(h.n())},n:function(){return q.getMonth()+1},S:function(){var a=h.j()+" ",a=a.charAt(a.length-2);return"1"===a?"st":"2"===a?"nd":"3"===a?"rd":"th"},j:function(){return q.getDate()},d:function(){return t(h.j())},t:function(){return(new Date(h.Y(),h.n(),0)).getDate()},w:function(){return q.getDay()}, +l:function(){return a[h.w()]},D:function(){return(h.l()+"").slice(0,3)},G:function(){return q.getHours()},g:function(){return h.G()%12||12},h:function(){return t(e?h.g():h.G())},i:function(){return t(q.getMinutes())},s:function(){return t(q.getSeconds())},a:function(){return 11r)c=new Date(c),c.setDate(c.getDate()+e+7-c.getDay()%7),p=c;p.setHours(f);p.setMinutes(d);p.setSeconds(n);return p.getTime()-g}if(c=a.match(/^(\*|\d+)\s+(\*|\d+)\s+(\*|\d+)\s+(\*|\d+)\s+(\*|\d+)\s+(\d+)$/i)){var g=c,c="*"===g[1]?"*":parseInt(g[1],10),e="*"===g[2]?"*":parseInt(g[2],10),f="*"===g[3]?"*":parseInt(g[3],10),d="*"===g[4]?"*":parseInt(g[4],10),n="*"===g[5]?"*":parseInt(g[5],10),v="*"===g[6]?"*":parseInt(g[6],10),u=new Date,l=u.getFullYear(); +if("*"!==c&&cl&&"*"===n&&(v.setMinutes(w+1),l=v.getTime(),l-=u);0>l&&"*"===d&&(v.setHours(p+1),l=v.getTime(),l-=u);0>l&&"*"===f&&(v.setDate(r+1),l=v.getTime(),l-=u);0>l&&"*"===e&&(v.setMonth(k),l=v.getTime(),l-=u);0>l&&"*"===c&&(v.setFullYear(g+1),l=v.getTime(),l-=u);c=l}return c}return-1}, +d=function(b){b.fn();var c=b.id;if(!b.repeat)return a["delete"](c);var d=A();b.lastTick=d;a.set(c,b);return!0},f=function(){if(0f?a["delete"](d):0===f?(b.delay=0,g.push(b)):(b.delay=f,a.set(d,b),f<=e&&(e=f,d=[],d=g.concat(b),g=d.filter(function(a){return a.delay<=e})))});b&&clearTimeout(b);g.length&&(b=setTimeout(function(){g.map(d);setTimeout(f,1)},e))}},g=function(b,c,d){d=d?0:1;var e=A(),g=L(32);a.set(g,{id:g, +fn:c,time:b,repeat:d,createdAt:e,lastTick:e,delay:0});f()};e.scheduler={yearly:function(a,b){g("* "+a,b)},monthly:function(a,b){g("* * "+a,b)},daily:function(a,b){g("* * * "+a,b)},hourly:function(a,b){return g("* * * * "+a,b)},every:function(a,b){return g(a,b)},once:function(a,b){return g(a,b,1)}}})();if("node"===G)module.exports=e;else{var F=window||{};F.define&&F.define.amd&&F.define(function(){return e});F.Bella=e}})(); \ No newline at end of file diff --git a/package.json b/package.json index 37f91a1..4aad30b 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "5.1.0", + "version": "5.1.1", "name": "bellajs", "description": "A useful helper for any javascript program", "homepage": "http://techpush.net", diff --git a/src/bella.js b/src/bella.js index d23e26b..fa525cf 100755 --- a/src/bella.js +++ b/src/bella.js @@ -180,6 +180,151 @@ B.hasProperty = hasProperty; B.equals = equals; + var encode = (s) => { + return isString(s) ? encodeURIComponent(s) : ''; + }; + + var decode = (s) => { + return isString(s) ? decodeURIComponent(s.replace(/\+/g, ' ')) : ''; + }; + + var trim = (s, all) => { + if (!isString(s)) { + return ''; + } + let x = s ? s.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '') : s || ''; + if (x && all) { + return x.replace(/\r?\n|\r/g, ' ').replace(/\s\s+|\r/g, ' '); + } + return x; + }; + + var truncate = (s, l) => { + s = trim(s); + if (!s) { + return s; + } + let t = l || 140; + if (s.length <= t) { + return s; + } + let x = s.substring(0, t); + let a = x.split(' '), b = a.length, r = ''; + if (b > 1) { + a.pop(); + r += a.join(' '); + if (r.length < s.length) { + r += '...'; + } + } else { + x = x.substring(0, t - 3); + r = x + '...'; + } + return r; + }; + + var stripTags = (s) => { + if (!isString(s)) { + return ''; + } + let r = s.replace(/<.*?>/gi, ' '); + if (r) { + r = trim(r.replace(/\s\s+/g, ' ')); + } + return r; + }; + + var escapeHTML = (s) => { + if (!isString(s)) { + return ''; + } + return s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); + }; + + var unescapeHTML = (s) => { + if (!isString(s)) { + return ''; + } + 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) => { + if (!isString(s)) { + return ''; + } + if (s.length === 1) { + return s.toUpperCase(); + } + s = s.toLowerCase(); + return s.charAt(0).toUpperCase() + s.slice(1); + }; + + var ucwords = (s) => { + if (!isString(s)) { + return ''; + } + let c = s.split(' '), a = []; + c.forEach((w) => { + a.push(ucfirst(w)); + }); + return a.join(' '); + }; + + var leftPad = (s, size, spad) => { + 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; + }; + + var rightPad = (s, size, spad) => { + 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); + }; + + var repeat = (s, m) => { + let a = []; + while (a.length < m) { + a.push(s); + } + return a.join(''); + }; + + var warn = (msg) => { + let txt = ' WARNING: ' + msg; + let t = txt.length + 5; + let c = '\x1b[33m%s\x1b[0m '; + console.warn(c, repeat('*', t)); // eslint-disable-line no-console + console.warn(c, txt); // eslint-disable-line no-console + console.warn(c, repeat('*', t)); // eslint-disable-line no-console + }; + + B.encode = encode; + B.decode = decode; + B.trim = trim; + B.truncate = truncate; + B.stripTags = stripTags; + B.escapeHTML = escapeHTML; + B.unescapeHTML = unescapeHTML; + B.strtolower = strtolower; + B.strtoupper = strtoupper; + B.ucfirst = ucfirst; + B.ucwords = ucwords; + B.leftPad = leftPad; + B.rightPad = rightPad; + B.repeat = repeat; + B.warn = warn; + var createId = (leng, prefix) => { let rn = () => { return Math.random().toString(36).slice(2); @@ -287,7 +432,7 @@ return a[a.length - 1]; }; - var getIndex = (item, arr) => { + var getIndex = (arr, item) => { let r = -1; for (let i = 0; i < arr.length; i++) { if (arr[i] === item) { @@ -298,7 +443,7 @@ return r; }; - var getLastIndex = (item, arr) => { + var getLastIndex = (arr, item) => { let r = -1; for (let i = arr.length - 1; i >= 0; i--) { if (arr[i] === item) { @@ -310,37 +455,30 @@ }; var clone = (obj) => { - if (!isObject(obj) && !isArray(obj) && !isDate(obj)) { + if (!(obj instanceof Object)) { return obj; } - if (isDate(obj)) { - let copy1 = new Date(); - copy1.setTime(obj.getTime()); - return copy1; - } - if (isArray(obj)) { - let copy2 = [], arr = obj.slice(0); - for (let i = 0, len = arr.length; i < len; ++i) { - copy2[i] = clone(arr[i]); - } - return copy2; + 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(); } - if (isObject(obj)) { - let copy = {}; - for (let attr in obj) { - if (attr === 'clone') { - continue; - } - if (obj.hasOwnProperty(attr)) { - copy[attr] = clone(obj[attr]); - } - } - return copy; + for (let prop in obj) { // eslint-disable-line guard-for-in + output[prop] = clone(obj[prop]); } - return obj; + return output; }; + var copies = (source, dest, matched, excepts) => { + warn('.copies() method is deprecated. It will be removed since May 15, 2016.'); let mt = matched || false; let ex = excepts || []; for (let k in source) { @@ -353,7 +491,7 @@ if (isObject(ob) && isObject(oa) || isArray(ob) && isArray(oa)) { dest[k] = copies(oa, dest[k], mt, ex); } else { - dest[k] = oa; + dest[k] = clone(oa); } } } @@ -471,118 +609,6 @@ B.md5 = function() {for(var m=[],l=0;64>l;)m[l]=0|4294967296*Math.abs(Math.sin(++l));return function(c) {var e,g,f,a,h=[];c=unescape(encodeURI(c));for(var b=c.length,k=[e=1732584193,g=-271733879,~e,~g],d=0;d<=b;)h[d>>2]|=(c.charCodeAt(d)||128)<<8*(d++%4);h[c=16*(b+8>>6)+14]=8*b;for(d=0;da;)b=[f=b[3],(e=b[1]|0)+((f=b[0]+[e&(g=b[2])|~e&f,f&e|~f&g,e^g^f,g^(e|~f)][b=a>>4]+(m[a]+(h[[a,5*a+1,3*a+5,7*a][b]%16+d]|0)))<<(b=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*b+a++%4])|f>>>32-b),e,g];for(a=4;a;)k[--a]=k[a]+b[a]}for(c="";32>a;)c+=(k[a>>3]>>4*(1^a++&7)&15).toString(16);return c}}(); /*eslint-enable*/ - var encode = (s) => { - return isString(s) ? encodeURIComponent(s) : ''; - }; - - var decode = (s) => { - return isString(s) ? decodeURIComponent(s.replace(/\+/g, ' ')) : ''; - }; - - var trim = (s, all) => { - if (!isString(s)) { - return ''; - } - let x = s ? s.replace(/^[\s\xa0]+|[\s\xa0]+$/g, '') : s || ''; - if (x && all) { - return x.replace(/\r?\n|\r/g, ' ').replace(/\s\s+|\r/g, ' '); - } - return x; - }; - - var truncate = (s, l) => { - s = trim(s); - if (!s) { - return s; - } - let t = l || 140; - if (s.length <= t) { - return s; - } - let x = s.substring(0, t); - let a = x.split(' '), b = a.length, r = ''; - if (b > 1) { - a.pop(); - r += a.join(' '); - if (r.length < s.length) { - r += '...'; - } - } else { - x = x.substring(0, t - 3); - r = x + '...'; - } - return r; - }; - - var stripTags = (s) => { - if (!isString(s)) { - return ''; - } - let r = s.replace(/<.*?>/gi, ' '); - if (r) { - r = trim(r.replace(/\s\s+/g, ' ')); - } - return r; - }; - - var escapeHTML = (s) => { - if (!isString(s)) { - return ''; - } - return s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); - }; - - var unescapeHTML = (s) => { - if (!isString(s)) { - return ''; - } - 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) => { - if (!isString(s)) { - return ''; - } - if (s.length === 1) { - return s.toUpperCase(); - } - s = s.toLowerCase(); - return s.charAt(0).toUpperCase() + s.slice(1); - }; - - var ucwords = (s) => { - if (!isString(s)) { - return ''; - } - let c = s.split(' '), a = []; - c.forEach((w) => { - a.push(ucfirst(w)); - }); - return a.join(' '); - }; - - var leftPad = (s, size, spad) => { - 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; - }; - - var rightPad = (s, size, spad) => { - 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); - }; - var replaceAll = (s, a, b) => { if (isNumber(s)) { s = String(s); @@ -704,19 +730,6 @@ return tpl; }; - B.encode = encode; - B.decode = decode; - B.trim = trim; - B.truncate = truncate; - B.stripTags = stripTags; - B.escapeHTML = escapeHTML; - B.unescapeHTML = unescapeHTML; - B.strtolower = strtolower; - B.strtoupper = strtoupper; - B.ucfirst = ucfirst; - B.ucwords = ucwords; - B.leftPad = leftPad; - B.rightPad = rightPad; B.replaceAll = replaceAll; B.stripAccent = stripAccent; B.createAlias = createAlias; diff --git a/test/config.js b/test/config.js index 6055ac0..d912994 100644 --- a/test/config.js +++ b/test/config.js @@ -1,6 +1,8 @@ var bella = require('../dist/bella.min'); +// var bella = require('../src/bella'); + module.exports = { bella: bella }; diff --git a/test/manual/test.js b/test/manual/test.js new file mode 100644 index 0000000..dfb9797 --- /dev/null +++ b/test/manual/test.js @@ -0,0 +1,37 @@ +/** + * Testing + * @ndaidong + */ + +'use strict'; + +/* eslint no-undefined: 0*/ +/* eslint no-array-constructor: 0*/ +/* eslint no-new-func: 0*/ + + +var bella = require('../../src/bella'); + +let a = { + name: 'x', + age: 10, + reg: [ 9, 8, 7, 2] +}; + +let b = bella.copies(a, {}); + +console.log('Original:'); +console.log('a'); +console.log(a); +console.log('b'); +console.log(b); + +console.log('Changing...'); +a.age = 15; +b.name = 'y'; +b.reg.push(111); +console.log('After changing:'); +console.log('a'); +console.log(a); +console.log('b'); +console.log(b); diff --git a/test/specs/string/string.js b/test/specs/string/string.js index abe0119..38aac03 100644 --- a/test/specs/string/string.js +++ b/test/specs/string/string.js @@ -14,6 +14,15 @@ var test = require('tape'); var config = require('../../config'); var bella = config.bella; +// repeat +test('Testing .repeat(String s, Number times) method', (assert) => { + let x = 'hi'; + let a = bella.repeat(x, 5); + let e = 'hihihihihi'; + assert.deepEquals(a, e, `bella.repeat(x) must return ${e}`); + assert.end(); +}); + // encode test('Testing .encode(String s) method', (assert) => { let x = 'Hello world'; diff --git a/test/specs/utils/utils.js b/test/specs/utils/utils.js index 999882e..bc9ae76 100644 --- a/test/specs/utils/utils.js +++ b/test/specs/utils/utils.js @@ -113,7 +113,8 @@ test('Testing .clone(Object target) method', (assert) => { }, birthday: new Date(), a: 0, - clone: false + clone: false, + reg: /^\w+@\s([a-z])$/gi }; @@ -122,6 +123,7 @@ test('Testing .clone(Object target) method', (assert) => { assert.ok(bella.hasProperty(r, 'IQ'), 'Result must have IQ'); assert.ok(bella.hasProperty(r, 'epouse'), 'Result must have epouse'); assert.ok(bella.hasProperty(r, 'birthday'), 'Result must have birthday'); + assert.ok(bella.hasProperty(r, 'reg'), 'Result must have reg'); assert.error(bella.clone(), 'Clone nothing must return nothing'); assert.end(); }); @@ -304,6 +306,22 @@ test('Testing .last(Array a) method', (assert) => { assert.end(); }); +// first +test('Testing .getIndex(Array a, Find) method', (assert) => { + let a = [ 9, 77, 123, 51, 876, 124, 12, 51, 99, 46 ]; + let e = bella.getIndex(a, 51); + assert.deepEquals(e, 3, 'Index of 51 must be 3'); + assert.end(); +}); + +// first +test('Testing .getLastIndex(Array a, Find) method', (assert) => { + let a = [ 9, 77, 123, 51, 876, 124, 12, 51, 99, 46 ]; + let e = bella.getLastIndex(a, 51); + assert.deepEquals(e, 7, 'Last index of 51 must be 7'); + assert.end(); +}); + // sort test('Testing .sort(Array a) method', (assert) => { let a1 = [ From 42d7b579b8a351afd7989e1f352086eff6037138 Mon Sep 17 00:00:00 2001 From: Dong Nguyen Date: Tue, 26 Apr 2016 11:37:52 +0700 Subject: [PATCH 2/2] Update travis config --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2c26e2e..31e5963 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - - "5.10" + - "5.11.0" - "5.9" - "5.0" - "4.2"