@andrewcaires/utils.js 0.2.7 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -148,7 +148,7 @@ type([]); // => 'array'
148
148
  type(true); // => 'boolean'
149
149
  type(false); // => 'boolean'
150
150
 
151
- type(frunction() {}); // => 'function'
151
+ type(function() {}); // => 'function'
152
152
 
153
153
  type(1); // => 'number'
154
154
  type(1.7); // => 'number'
package/dist/index.cjs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.7
2
+ * @andrewcaires/utils.js v0.2.9
3
3
  * JavaScript utility library for web and nodejs development
4
- * (c) 2022 Andrew Caires
4
+ * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- "use strict";const t={},e=t=>"array"==u(t),r=t=>"boolean"==u(t),s=t=>"null"!=u(t),i=t=>"number"==u(t),n=t=>"object"==u(t),a=t=>"string"==u(t),h=t=>void 0===t,u=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const o=(t,e,r)=>Math.max(e,Math.min(r,t)),l=(t,e)=>{return(r=parseInt(t,16)+e,o(r,0,255)).toString(16);var r},p=t=>Math.random().toString(16).slice(t),c=()=>p(-3),v=()=>p(-4),m=()=>v()+v(),d=()=>m()+m();const x=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},f=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length&&(s=i[n],!1!==e.call(r,t[s],s,t));n++);return t},g=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),_=t=>g(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),y=_,b=t=>g(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),w=t=>t.split("").reverse().join(""),k=t=>/^[A-Za-z]$/.test(t),C=t=>(t.mask--,""),j=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return h(s)||h(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:C(r);case"A":case"Z":return k(e)?e:C(r);case"L":return k(e)?e.toLowerCase():C(r);case"U":return k(e)?e.toUpperCase():C(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:C(r);default:return t!=e&&r.text--,t}})(s,i,r)+j(t,e,r)},S=(t,e)=>j(t,e,{mask:-1,text:-1}),A=S,O=(t,e)=>w(A(w(t),w(e))),E=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{});function M(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}class F{constructor(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}empty(t){return this.value=t,this.unshift((t=>{const e=s(t);return{break:!e,value:e?t:this.value}}))}error(t){return this._error=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>s(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!a(this.validate(t).error)}testAsync(t){return M(this,void 0,void 0,(function*(){return!a((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return M(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++)if(e=yield this.custom[t](e.value),e.error)return{error:e.error};return{value:e.value}}))}}class I extends F{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(e(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:r,value:s}=this._items.validate(t[e]);if(r)return this.invalid("["+this._type+"]< "+r+"["+e+"] >");t[e]=s}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class U extends F{constructor(){super(),this._type="boolean",this.push((t=>r(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:s(t)?-1!=[1,"1","true"].indexOf(t):t})))}}class q extends F{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>i(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:s(t)?parseFloat(t):t})))}}class L extends F{constructor(){super(),this._type="object",this.push((t=>{if(n(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),s(a)&&(t[r]=a)}})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class T extends F{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:_(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:S(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:n(t)?JSON.stringify(t):s(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:b(t)})))}}class B{static array(t){return(new I).items(t)}static boolean(){return new U}static number(){return new q}static object(t){return(new L).keys(t)}static string(){return new T}test(t,...e){return!a(B.validate(t,...e).error)}testAsync(t,...e){return M(this,void 0,void 0,(function*(){return!a((yield B.validateAsync(t,...e)).error)}))}static validate(t,...e){let r={value:t};for(let t=0;t<e.length;t++)if(r=e[t].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(t,...e){return M(this,void 0,void 0,(function*(){let r={value:t};for(let t=0;t<e.length;t++)if(r=yield e[t].validateAsync(r.value),r.error)return{error:r.error};return r}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const r=this.events.get(t);return r?r.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const r=s=>{e(s),this.off(t,r)};return this.on(t,r),{dispose:()=>this.off(t,r)}}off(t,e){const r=this.events.get(t);if(e){if(r){const t=r.indexOf(e);t>=0&&r.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const r=this.events.get(t);r&&r.forEach((t=>t(e)))}},exports.Validation=B,exports.ValidationBase=F,exports.allowedObject=E,exports.camelCase=y,exports.chunkArray=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},exports.clamp=o,exports.cutText=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),exports.debounce=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}},exports.deniedObject=(t,e)=>E(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,r,s)=>e(t)?x(t,r,s):n(t)?f(t,r,s):null,exports.forEachAsync=(t,e,r)=>M(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),exports.forEachIndex=x,exports.forEachKey=f,exports.forEachSeries=(t,e,r)=>M(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),exports.isArray=e,exports.isBoolean=r,exports.isDef=s,exports.isFloat=t=>i(t)&&!!(t%1),exports.isFunction=t=>"function"==u(t),exports.isInteger=t=>i(t)&&!(t%1),exports.isNull=t=>null===t,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isUndefined=h,exports.loop=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},exports.lowerCamelCase=_,exports.map=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length;n++)s=i[n],t[s]=e.call(r,t[s],s,t);return t},exports.mask=S,exports.maskMoney=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),O(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),exports.maskReverse=O,exports.maskTest=(t,e)=>t.length==A(t,e).length,exports.noop=()=>{},exports.parseSize=t=>{if(i(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")},exports.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+l(t,e)).slice(-2))),exports.simpleID=()=>c()+v(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=w,exports.toArray=t=>e(t)?t:s(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=u,exports.uniqueID=()=>d()+d()+d()+d(),exports.upperCamelCase=b,exports.uuidv4=()=>m()+"-"+v()+"-4"+c()+"-"+v()+"-"+(m()+v());
7
+ "use strict";const e={},t=e=>"array"==o(e),r=e=>"boolean"==o(e),s=e=>"null"!=o(e),i=e=>"number"==o(e),n=e=>"object"==o(e),a=e=>"string"==o(e),h=e=>void 0===e,o=t=>null==t?"null":e[e.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>e["[object "+t+"]"]=t.toLowerCase()));const u=(e,t,r)=>Math.max(t,Math.min(r,e)),l=(e,t)=>{return(r=parseInt(e,16)+t,u(r,0,255)).toString(16);var r},p=e=>Math.random().toString(16).slice(e),c=()=>p(-3),v=()=>p(-4),m=()=>v()+v(),d=()=>m()+m();const x=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},f=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length&&(s=i[n],!1!==t.call(r,e[s],s,e));n++);return e},g=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),_=e=>g(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),y=_,b=e=>g(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),w=e=>e.split("").reverse().join(""),k=e=>/^[A-Za-z]$/.test(e),C=e=>(e.mask--,""),j=(e,t,r)=>{r.mask++,r.text++;const s=e[r.mask],i=t[r.text];return h(s)||h(i)?"":((e,t,r)=>{switch(e.toUpperCase()){case"#":return t;case"0":case"9":return(e=>/^[0-9]$/.test(e))(t)?t:C(r);case"A":case"Z":return k(t)?t:C(r);case"L":return k(t)?t.toLowerCase():C(r);case"U":return k(t)?t.toUpperCase():C(r);case"S":return(e=>/^[^A-Za-z0-9]$/.test(e))(t)?t:C(r);default:return e!=t&&r.text--,e}})(s,i,r)+j(e,t,r)},A=(e,t)=>j(e,t,{mask:-1,text:-1}),S=A,O=(e,t)=>w(S(w(e),w(t))),M=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function E(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{o(s.next(e))}catch(e){n(e)}}function h(e){try{o(s.throw(e))}catch(e){n(e)}}function o(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,h)}o((s=s.apply(e,t||[])).next())}))}class F{constructor(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}empty(e){return this.value=e,this.unshift((e=>{const t=s(e);return{break:!t,value:t?e:this.value}}))}error(e){return this._error=e,this}invalid(e){return{error:this._error?this._error:this.name?this.name+":"+e:e}}parse(){return this.unshift((e=>({value:e})))}push(e){return this.rules.push(e),this}required(){return this.unshift((e=>s(e)?{value:e}:this.invalid("["+this._type+"].required")))}test(e){return!a(this.validate(e).error)}testAsync(e){return E(this,void 0,void 0,(function*(){return!a((yield this.validateAsync(e)).error)}))}unshift(e){return this.rules.unshift(e),this}validate(e){if(this.custom.length)return this.invalid("[async]");let t={value:e};for(let e=0;e<this.rules.length;e++){if(t=this.rules[e](t.value),t.error)return{error:t.error};if(t.break)return{value:t.value}}return{value:t.value}}validateAsync(e){return E(this,void 0,void 0,(function*(){let t=this.validate(e);if(t.error)return{error:t.error};for(let e=0;e<this.custom.length;e++)if(t=yield this.custom[e](t.value),t.error)return{error:t.error};return{value:t.value}}))}}class I extends F{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((e=>{if(t(e)){if(this._items){this._items.name="";for(let t=0;t<e.length;t++){const{error:r,value:s}=this._items.validate(e[t]);if(r)return this.invalid("["+this._type+"]< "+r+"["+t+"] >");e[t]=s}}return{value:e}}return this.invalid("["+this._type+"]")}))}between(e,t){return this.min(e),this.max(t)}items(e){return this._items=e,this}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}}class L extends F{constructor(){super(),this._type="boolean",this.push((e=>r(e)?{value:e}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((e=>({value:s(e)?-1!=[1,"1","true"].indexOf(e):e})))}}class T extends F{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((e=>i(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}max(e){return this._max=e,this.push((e=>e<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:s(e)?parseFloat(e):e})))}}class U extends F{constructor(){super(),this._type="object",this.push((e=>{if(n(e)){if(this._keys){const t=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(e[r]);n&&t.push(n),s(a)&&(e[r]=a)}})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:e}}return{value:e}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e,this}}class q extends F{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>a(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}email(){return this.push((e=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(e)?{value:e}:this.invalid("["+this._type+"].email")))}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((e=>({value:e.toLowerCase()})))}lowerCamelCase(){return this.push((e=>({value:_(e)})))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(e){return this.push((t=>({value:A(e,t)})))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:n(e)?JSON.stringify(e):s(e)?e.toString():e})))}required(){return this.push((e=>e.length?{value:e}:this.invalid("["+this._type+"].required")))}trim(){return this.push((e=>({value:e.trim()})))}upperCase(){return this.push((e=>({value:e.toUpperCase()})))}upperCamelCase(){return this.push((e=>({value:b(e)})))}}class H{static array(e){return(new I).items(e)}static boolean(){return new L}static number(){return new T}static object(e){return(new U).keys(e)}static string(){return new q}test(e,...t){return!a(H.validate(e,...t).error)}testAsync(e,...t){return E(this,void 0,void 0,(function*(){return!a((yield H.validateAsync(e,...t)).error)}))}static validate(e,...t){let r={value:e};for(let e=0;e<t.length;e++)if(r=t[e].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(e,...t){return E(this,void 0,void 0,(function*(){let r={value:e};for(let e=0;e<t.length;e++)if(r=yield t[e].validateAsync(r.value),r.error)return{error:r.error};return r}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(e,t){const r=this.events.get(e);return r?r.push(t):this.events.set(e,[t]),{dispose:()=>this.off(e,t)}}once(e,t){const r=s=>{t(s),this.off(e,r)};return this.on(e,r),{dispose:()=>this.off(e,r)}}off(e,t){const r=this.events.get(e);if(t){if(r){const e=r.indexOf(t);e>=0&&r.splice(e,1)}}else this.events.set(e,[])}emit(e,t){const r=this.events.get(e);r&&r.forEach((e=>e(t)))}},exports.Validation=H,exports.ValidationBase=F,exports.allowedObject=M,exports.camelCase=y,exports.chunkArray=(e,t)=>{const r=[];for(let s=0;s<e.length;s+=t)r.push(e.slice(s,s+t));return r},exports.clamp=u,exports.cutText=(e,t)=>{if(e.length>t){const r=e.substring(0,t).split(" ");return r.pop(),r.join(" ")+"..."}return e},exports.dateFormat=(e,t)=>t.replace(/%[yYmdHMS]/g,(t=>{let r;switch(t){case"%y":r=e.getFullYear();break;case"%Y":return e.getFullYear().toString();case"%m":r=e.getMonth()+1;break;case"%d":r=e.getDate();break;case"%H":r=e.getHours();break;case"%M":r=e.getMinutes();break;case"%S":r=e.getSeconds();break;default:return t.slice(1)}return("0"+r.toString()).slice(-2)})),exports.debounce=(e,t)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>e(...s)),t)}},exports.decodeHTML=e=>e.replace(/&#(\d+);/g,((e,t)=>String.fromCharCode(t))),exports.deniedObject=(e,t)=>M(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,r,s)=>t(e)?x(e,r,s):n(e)?f(e,r,s):null,exports.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>E(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),exports.forEachIndex=x,exports.forEachKey=f,exports.forEachSeries=(e,t,r)=>E(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),exports.isArray=t,exports.isBoolean=r,exports.isDef=s,exports.isFloat=e=>i(e)&&!!(e%1),exports.isFunction=e=>"function"==o(e),exports.isInteger=e=>i(e)&&!(e%1),exports.isNull=e=>null===e,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isUndefined=h,exports.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.lowerCamelCase=_,exports.map=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length;n++)s=i[n],e[s]=t.call(r,e[s],s,e);return e},exports.mask=A,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),O(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=O,exports.maskTest=(e,t)=>e.length==S(e,t).length,exports.noop=()=>{},exports.parseSize=e=>{if(i(e))return e+"px";const[,t,r,s]=(e=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e)||["","0","",""];return(r.length?parseFloat(t+"."+r):parseInt(t))+(s||"px")},exports.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>("0"+l(e,t)).slice(-2))),exports.simpleID=()=>c()+v(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=w,exports.toArray=e=>t(e)?e:s(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=o,exports.uniqueID=()=>d()+d()+d()+d(),exports.upperCamelCase=b,exports.uuidv4=()=>m()+"-"+v()+"-4"+c()+"-"+v()+"-"+(m()+v());
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.7
2
+ * @andrewcaires/utils.js v0.2.9
3
3
  * JavaScript utility library for web and nodejs development
4
- * (c) 2022 Andrew Caires
4
+ * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
7
  declare const chunkArray: <T>(array: T[], length: number) => T[][];
@@ -78,6 +78,8 @@ declare const sleep: (ms: number) => Promise<void>;
78
78
  declare const parseSize: (size: number | string) => string;
79
79
 
80
80
  declare const cutText: (text: string, length: number) => string;
81
+ declare const decodeHTML: (text: string) => string;
82
+ declare const encodeHTML: (text: string) => string;
81
83
  declare const lowerCamelCase: (text: string) => string;
82
84
  declare const camelCase: (text: string) => string;
83
85
  declare const upperCamelCase: (text: string) => string;
@@ -187,4 +189,4 @@ declare class Validation {
187
189
  static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<ValidationResult>;
188
190
  }
189
191
 
190
- export { EventCallback, EventDisposable, EventEmitter, EventList, TypeArray, TypeArrayAny, TypeArrayFunction, TypeArrayNumber, TypeArrayString, TypeCallback, TypeCallbackArray, TypeCallbackFunction, TypeCallbackMap, TypeCallbackObject, TypeObject, TypeObjectAny, TypeObjectFunction, TypeObjectNumber, TypeObjectString, Validation, ValidationBase, allowedObject, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, deniedObject, each, forEachAsync, forEachIndex, forEachKey, forEachSeries, isArray, isBoolean, isDef, isFloat, isFunction, isInteger, isNull, isNumber, isObject, isString, isUndefined, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, noop, parseSize, shadeColor, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
192
+ export { EventCallback, EventDisposable, EventEmitter, EventList, TypeArray, TypeArrayAny, TypeArrayFunction, TypeArrayNumber, TypeArrayString, TypeCallback, TypeCallbackArray, TypeCallbackFunction, TypeCallbackMap, TypeCallbackObject, TypeObject, TypeObjectAny, TypeObjectFunction, TypeObjectNumber, TypeObjectString, Validation, ValidationBase, allowedObject, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, deniedObject, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, isArray, isBoolean, isDef, isFloat, isFunction, isInteger, isNull, isNumber, isObject, isString, isUndefined, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, noop, parseSize, shadeColor, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.7
2
+ * @andrewcaires/utils.js v0.2.9
3
3
  * JavaScript utility library for web and nodejs development
4
- * (c) 2022 Andrew Caires
4
+ * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- const t={},e=t=>"array"==v(t),r=t=>"boolean"==v(t),s=t=>"null"!=v(t),i=t=>"function"==v(t),n=t=>"number"==v(t),a=t=>"object"==v(t),h=t=>"string"==v(t),u=t=>n(t)&&!!(t%1),l=t=>n(t)&&!(t%1),o=t=>null===t,c=t=>void 0===t,v=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const p=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},m=t=>e(t)?t:s(t)?[t]:[],d=t=>"true"===t||"1"===t,g=(t,e,r)=>Math.max(e,Math.min(r,t)),_=(t,e)=>{return(r=parseInt(t,16)+e,g(r,0,255)).toString(16);var r},f=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+_(t,e)).slice(-2))),y=t=>Math.random().toString(16).slice(t),x=()=>y(-3),b=()=>y(-4),w=()=>b()+b(),k=()=>w()+w(),j=()=>x()+b(),C=()=>k()+k()+k()+k(),S=()=>w()+"-"+b()+"-4"+x()+"-"+b()+"-"+(w()+b()),A=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),O=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class M{constructor(){this.events=new Map}on(t,e){const r=this.events.get(t);return r?r.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const r=s=>{e(s),this.off(t,r)};return this.on(t,r),{dispose:()=>this.off(t,r)}}off(t,e){const r=this.events.get(t);if(e){if(r){const t=r.indexOf(e);t>=0&&r.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const r=this.events.get(t);r&&r.forEach((t=>t(e)))}}const U=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},F=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length&&(s=i[n],!1!==e.call(r,t[s],s,t));n++);return t},L=(t,r,s)=>e(t)?U(t,r,s):a(t)?F(t,r,s):null,q=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},Y=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length;n++)s=i[n],t[s]=e.call(r,t[s],s,t);return t},$=()=>{},E=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},H=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),I=t=>H(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),P=I,T=t=>H(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),Z=t=>t.split("").reverse().join(""),z=t=>/^[A-Za-z]$/.test(t),N=t=>(t.mask--,""),W=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return c(s)||c(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:N(r);case"A":case"Z":return z(e)?e:N(r);case"L":return z(e)?e.toLowerCase():N(r);case"U":return z(e)?e.toUpperCase():N(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:N(r);default:return t!=e&&r.text--,t}})(s,i,r)+W(t,e,r)},B=(t,e)=>W(t,e,{mask:-1,text:-1}),D=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),K(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),J=B,G=(t,e)=>t.length==J(t,e).length,K=(t,e)=>Z(J(Z(t),Z(e))),Q=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),R=(t,e)=>Q(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function V(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}const X=(t,e,r)=>V(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),tt=(t,e,r)=>V(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),et=t=>new Promise((e=>setTimeout(e,t))),rt=t=>{if(n(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")};class st{constructor(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}empty(t){return this.value=t,this.unshift((t=>{const e=s(t);return{break:!e,value:e?t:this.value}}))}error(t){return this._error=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>s(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!h(this.validate(t).error)}testAsync(t){return V(this,void 0,void 0,(function*(){return!h((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return V(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++)if(e=yield this.custom[t](e.value),e.error)return{error:e.error};return{value:e.value}}))}}class it extends st{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(e(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:r,value:s}=this._items.validate(t[e]);if(r)return this.invalid("["+this._type+"]< "+r+"["+e+"] >");t[e]=s}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class nt extends st{constructor(){super(),this._type="boolean",this.push((t=>r(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:s(t)?-1!=[1,"1","true"].indexOf(t):t})))}}class at extends st{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>n(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:s(t)?parseFloat(t):t})))}}class ht extends st{constructor(){super(),this._type="object",this.push((t=>{if(a(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),s(a)&&(t[r]=a)}})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class ut extends st{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:I(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:B(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:a(t)?JSON.stringify(t):s(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:T(t)})))}}class lt{static array(t){return(new it).items(t)}static boolean(){return new nt}static number(){return new at}static object(t){return(new ht).keys(t)}static string(){return new ut}test(t,...e){return!h(lt.validate(t,...e).error)}testAsync(t,...e){return V(this,void 0,void 0,(function*(){return!h((yield lt.validateAsync(t,...e)).error)}))}static validate(t,...e){let r={value:t};for(let t=0;t<e.length;t++)if(r=e[t].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(t,...e){return V(this,void 0,void 0,(function*(){let r={value:t};for(let t=0;t<e.length;t++)if(r=yield e[t].validateAsync(r.value),r.error)return{error:r.error};return r}))}}export{M as EventEmitter,lt as Validation,st as ValidationBase,Q as allowedObject,P as camelCase,p as chunkArray,g as clamp,E as cutText,A as dateFormat,O as debounce,R as deniedObject,L as each,X as forEachAsync,U as forEachIndex,F as forEachKey,tt as forEachSeries,e as isArray,r as isBoolean,s as isDef,u as isFloat,i as isFunction,l as isInteger,o as isNull,n as isNumber,a as isObject,h as isString,c as isUndefined,q as loop,I as lowerCamelCase,Y as map,B as mask,D as maskMoney,K as maskReverse,G as maskTest,$ as noop,rt as parseSize,f as shadeColor,j as simpleID,et as sleep,Z as stringReverse,m as toArray,d as toBool,v as type,C as uniqueID,T as upperCamelCase,S as uuidv4};
7
+ const t={},e=t=>"array"==v(t),r=t=>"boolean"==v(t),s=t=>"null"!=v(t),i=t=>"function"==v(t),n=t=>"number"==v(t),a=t=>"object"==v(t),h=t=>"string"==v(t),u=t=>n(t)&&!!(t%1),l=t=>n(t)&&!(t%1),o=t=>null===t,c=t=>void 0===t,v=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const p=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},m=t=>e(t)?t:s(t)?[t]:[],d=t=>"true"===t||"1"===t,g=(t,e,r)=>Math.max(e,Math.min(r,t)),f=(t,e)=>{return(r=parseInt(t,16)+e,g(r,0,255)).toString(16);var r},_=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+f(t,e)).slice(-2))),y=t=>Math.random().toString(16).slice(t),x=()=>y(-3),b=()=>y(-4),w=()=>b()+b(),k=()=>w()+w(),C=()=>x()+b(),j=()=>k()+k()+k()+k(),S=()=>w()+"-"+b()+"-4"+x()+"-"+b()+"-"+(w()+b()),A=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),O=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class M{constructor(){this.events=new Map}on(t,e){const r=this.events.get(t);return r?r.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const r=s=>{e(s),this.off(t,r)};return this.on(t,r),{dispose:()=>this.off(t,r)}}off(t,e){const r=this.events.get(t);if(e){if(r){const t=r.indexOf(e);t>=0&&r.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const r=this.events.get(t);r&&r.forEach((t=>t(e)))}}const U=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},F=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length&&(s=i[n],!1!==e.call(r,t[s],s,t));n++);return t},L=(t,r,s)=>e(t)?U(t,r,s):a(t)?F(t,r,s):null,q=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},Y=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length;n++)s=i[n],t[s]=e.call(r,t[s],s,t);return t},$=()=>{},E=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},H=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),I=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),P=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),T=t=>P(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),Z=T,z=t=>P(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),N=t=>t.split("").reverse().join(""),W=t=>/^[A-Za-z]$/.test(t),B=t=>(t.mask--,""),D=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return c(s)||c(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:B(r);case"A":case"Z":return W(e)?e:B(r);case"L":return W(e)?e.toLowerCase():B(r);case"U":return W(e)?e.toUpperCase():B(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:B(r);default:return t!=e&&r.text--,t}})(s,i,r)+D(t,e,r)},J=(t,e)=>D(t,e,{mask:-1,text:-1}),G=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),R(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),K=J,Q=(t,e)=>t.length==K(t,e).length,R=(t,e)=>N(K(N(t),N(e))),V=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),X=(t,e)=>V(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function tt(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}const et=(t,e,r)=>tt(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),rt=(t,e,r)=>tt(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),st=t=>new Promise((e=>setTimeout(e,t))),it=t=>{if(n(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")};class nt{constructor(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}empty(t){return this.value=t,this.unshift((t=>{const e=s(t);return{break:!e,value:e?t:this.value}}))}error(t){return this._error=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>s(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!h(this.validate(t).error)}testAsync(t){return tt(this,void 0,void 0,(function*(){return!h((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return tt(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++)if(e=yield this.custom[t](e.value),e.error)return{error:e.error};return{value:e.value}}))}}class at extends nt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(e(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:r,value:s}=this._items.validate(t[e]);if(r)return this.invalid("["+this._type+"]< "+r+"["+e+"] >");t[e]=s}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class ht extends nt{constructor(){super(),this._type="boolean",this.push((t=>r(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:s(t)?-1!=[1,"1","true"].indexOf(t):t})))}}class ut extends nt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>n(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:s(t)?parseFloat(t):t})))}}class lt extends nt{constructor(){super(),this._type="object",this.push((t=>{if(a(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),s(a)&&(t[r]=a)}})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class ot extends nt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:T(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:J(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:a(t)?JSON.stringify(t):s(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:z(t)})))}}class ct{static array(t){return(new at).items(t)}static boolean(){return new ht}static number(){return new ut}static object(t){return(new lt).keys(t)}static string(){return new ot}test(t,...e){return!h(ct.validate(t,...e).error)}testAsync(t,...e){return tt(this,void 0,void 0,(function*(){return!h((yield ct.validateAsync(t,...e)).error)}))}static validate(t,...e){let r={value:t};for(let t=0;t<e.length;t++)if(r=e[t].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(t,...e){return tt(this,void 0,void 0,(function*(){let r={value:t};for(let t=0;t<e.length;t++)if(r=yield e[t].validateAsync(r.value),r.error)return{error:r.error};return r}))}}export{M as EventEmitter,ct as Validation,nt as ValidationBase,V as allowedObject,Z as camelCase,p as chunkArray,g as clamp,E as cutText,A as dateFormat,O as debounce,H as decodeHTML,X as deniedObject,L as each,I as encodeHTML,et as forEachAsync,U as forEachIndex,F as forEachKey,rt as forEachSeries,e as isArray,r as isBoolean,s as isDef,u as isFloat,i as isFunction,l as isInteger,o as isNull,n as isNumber,a as isObject,h as isString,c as isUndefined,q as loop,T as lowerCamelCase,Y as map,J as mask,G as maskMoney,R as maskReverse,Q as maskTest,$ as noop,it as parseSize,_ as shadeColor,C as simpleID,st as sleep,N as stringReverse,m as toArray,d as toBool,v as type,j as uniqueID,z as upperCamelCase,S as uuidv4};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.7
2
+ * @andrewcaires/utils.js v0.2.9
3
3
  * JavaScript utility library for web and nodejs development
4
- * (c) 2022 Andrew Caires
4
+ * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- var UtilsJS=function(t){"use strict";const e={},r=t=>"array"==l(t),s=t=>"boolean"==l(t),i=t=>"null"!=l(t),n=t=>"number"==l(t),a=t=>"object"==l(t),h=t=>"string"==l(t),u=t=>void 0===t,l=t=>null==t?"null":e[e.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>e["[object "+t+"]"]=t.toLowerCase()));const o=(t,e,r)=>Math.max(e,Math.min(r,t)),c=(t,e)=>{return(r=parseInt(t,16)+e,o(r,0,255)).toString(16);var r},p=t=>Math.random().toString(16).slice(t),v=()=>p(-3),m=()=>p(-4),d=()=>m()+m(),f=()=>d()+d();const g=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},_=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length&&(s=i[n],!1!==e.call(r,t[s],s,t));n++);return t},y=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),x=t=>y(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),b=x,w=t=>y(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),k=t=>t.split("").reverse().join(""),C=t=>/^[A-Za-z]$/.test(t),j=t=>(t.mask--,""),S=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return u(s)||u(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:j(r);case"A":case"Z":return C(e)?e:j(r);case"L":return C(e)?e.toLowerCase():j(r);case"U":return C(e)?e.toUpperCase():j(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:j(r);default:return t!=e&&r.text--,t}})(s,i,r)+S(t,e,r)},A=(t,e)=>S(t,e,{mask:-1,text:-1}),O=A,E=(t,e)=>k(O(k(t),k(e))),M=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{});function F(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}class U{constructor(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}empty(t){return this.value=t,this.unshift((t=>{const e=i(t);return{break:!e,value:e?t:this.value}}))}error(t){return this._error=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>i(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!h(this.validate(t).error)}testAsync(t){return F(this,void 0,void 0,(function*(){return!h((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return F(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++)if(e=yield this.custom[t](e.value),e.error)return{error:e.error};return{value:e.value}}))}}class I extends U{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(r(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:r,value:s}=this._items.validate(t[e]);if(r)return this.invalid("["+this._type+"]< "+r+"["+e+"] >");t[e]=s}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class q extends U{constructor(){super(),this._type="boolean",this.push((t=>s(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:i(t)?-1!=[1,"1","true"].indexOf(t):t})))}}class L extends U{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>n(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:i(t)?parseFloat(t):t})))}}class T extends U{constructor(){super(),this._type="object",this.push((t=>{if(a(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const s=this._keys[r];s.name=r;const{error:n,value:a}=s.validate(t[r]);n&&e.push(n),i(a)&&(t[r]=a)}})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class B extends U{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:x(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:A(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:a(t)?JSON.stringify(t):i(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:w(t)})))}}class D{static array(t){return(new I).items(t)}static boolean(){return new q}static number(){return new L}static object(t){return(new T).keys(t)}static string(){return new B}test(t,...e){return!h(D.validate(t,...e).error)}testAsync(t,...e){return F(this,void 0,void 0,(function*(){return!h((yield D.validateAsync(t,...e)).error)}))}static validate(t,...e){let r={value:t};for(let t=0;t<e.length;t++)if(r=e[t].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(t,...e){return F(this,void 0,void 0,(function*(){let r={value:t};for(let t=0;t<e.length;t++)if(r=yield e[t].validateAsync(r.value),r.error)return{error:r.error};return r}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const r=this.events.get(t);return r?r.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const r=s=>{e(s),this.off(t,r)};return this.on(t,r),{dispose:()=>this.off(t,r)}}off(t,e){const r=this.events.get(t);if(e){if(r){const t=r.indexOf(e);t>=0&&r.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const r=this.events.get(t);r&&r.forEach((t=>t(e)))}},t.Validation=D,t.ValidationBase=U,t.allowedObject=M,t.camelCase=b,t.chunkArray=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},t.clamp=o,t.cutText=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),t.debounce=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}},t.deniedObject=(t,e)=>M(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>r(t)?g(t,e,s):a(t)?_(t,e,s):null,t.forEachAsync=(t,e,r)=>F(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),t.forEachIndex=g,t.forEachKey=_,t.forEachSeries=(t,e,r)=>F(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),t.isArray=r,t.isBoolean=s,t.isDef=i,t.isFloat=t=>n(t)&&!!(t%1),t.isFunction=t=>"function"==l(t),t.isInteger=t=>n(t)&&!(t%1),t.isNull=t=>null===t,t.isNumber=n,t.isObject=a,t.isString=h,t.isUndefined=u,t.loop=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},t.lowerCamelCase=x,t.map=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length;n++)s=i[n],t[s]=e.call(r,t[s],s,t);return t},t.mask=A,t.maskMoney=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),E(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),t.maskReverse=E,t.maskTest=(t,e)=>t.length==O(t,e).length,t.noop=()=>{},t.parseSize=t=>{if(n(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")},t.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+c(t,e)).slice(-2))),t.simpleID=()=>v()+m(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=k,t.toArray=t=>r(t)?t:i(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=l,t.uniqueID=()=>f()+f()+f()+f(),t.upperCamelCase=w,t.uuidv4=()=>d()+"-"+m()+"-4"+v()+"-"+m()+"-"+(d()+m()),t}({});
7
+ var UtilsJS=function(e){"use strict";const t={},r=e=>"array"==l(e),s=e=>"boolean"==l(e),i=e=>"null"!=l(e),n=e=>"number"==l(e),a=e=>"object"==l(e),h=e=>"string"==l(e),u=e=>void 0===e,l=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const o=(e,t,r)=>Math.max(t,Math.min(r,e)),c=(e,t)=>{return(r=parseInt(e,16)+t,o(r,0,255)).toString(16);var r},p=e=>Math.random().toString(16).slice(e),v=()=>p(-3),m=()=>p(-4),d=()=>m()+m(),f=()=>d()+d();const g=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},_=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length&&(s=i[n],!1!==t.call(r,e[s],s,e));n++);return e},y=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),x=e=>y(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),b=x,w=e=>y(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),k=e=>e.split("").reverse().join(""),C=e=>/^[A-Za-z]$/.test(e),j=e=>(e.mask--,""),S=(e,t,r)=>{r.mask++,r.text++;const s=e[r.mask],i=t[r.text];return u(s)||u(i)?"":((e,t,r)=>{switch(e.toUpperCase()){case"#":return t;case"0":case"9":return(e=>/^[0-9]$/.test(e))(t)?t:j(r);case"A":case"Z":return C(t)?t:j(r);case"L":return C(t)?t.toLowerCase():j(r);case"U":return C(t)?t.toUpperCase():j(r);case"S":return(e=>/^[^A-Za-z0-9]$/.test(e))(t)?t:j(r);default:return e!=t&&r.text--,e}})(s,i,r)+S(e,t,r)},A=(e,t)=>S(e,t,{mask:-1,text:-1}),O=A,M=(e,t)=>k(O(k(e),k(t))),E=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function F(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{u(s.next(e))}catch(e){n(e)}}function h(e){try{u(s.throw(e))}catch(e){n(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,h)}u((s=s.apply(e,t||[])).next())}))}class U{constructor(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}empty(e){return this.value=e,this.unshift((e=>{const t=i(e);return{break:!t,value:t?e:this.value}}))}error(e){return this._error=e,this}invalid(e){return{error:this._error?this._error:this.name?this.name+":"+e:e}}parse(){return this.unshift((e=>({value:e})))}push(e){return this.rules.push(e),this}required(){return this.unshift((e=>i(e)?{value:e}:this.invalid("["+this._type+"].required")))}test(e){return!h(this.validate(e).error)}testAsync(e){return F(this,void 0,void 0,(function*(){return!h((yield this.validateAsync(e)).error)}))}unshift(e){return this.rules.unshift(e),this}validate(e){if(this.custom.length)return this.invalid("[async]");let t={value:e};for(let e=0;e<this.rules.length;e++){if(t=this.rules[e](t.value),t.error)return{error:t.error};if(t.break)return{value:t.value}}return{value:t.value}}validateAsync(e){return F(this,void 0,void 0,(function*(){let t=this.validate(e);if(t.error)return{error:t.error};for(let e=0;e<this.custom.length;e++)if(t=yield this.custom[e](t.value),t.error)return{error:t.error};return{value:t.value}}))}}class I extends U{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((e=>{if(r(e)){if(this._items){this._items.name="";for(let t=0;t<e.length;t++){const{error:r,value:s}=this._items.validate(e[t]);if(r)return this.invalid("["+this._type+"]< "+r+"["+t+"] >");e[t]=s}}return{value:e}}return this.invalid("["+this._type+"]")}))}between(e,t){return this.min(e),this.max(t)}items(e){return this._items=e,this}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}}class L extends U{constructor(){super(),this._type="boolean",this.push((e=>s(e)?{value:e}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((e=>({value:i(e)?-1!=[1,"1","true"].indexOf(e):e})))}}class T extends U{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((e=>n(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}max(e){return this._max=e,this.push((e=>e<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:i(e)?parseFloat(e):e})))}}class q extends U{constructor(){super(),this._type="object",this.push((e=>{if(a(e)){if(this._keys){const t=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const s=this._keys[r];s.name=r;const{error:n,value:a}=s.validate(e[r]);n&&t.push(n),i(a)&&(e[r]=a)}})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:e}}return{value:e}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e,this}}class H extends U{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>h(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}email(){return this.push((e=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(e)?{value:e}:this.invalid("["+this._type+"].email")))}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((e=>({value:e.toLowerCase()})))}lowerCamelCase(){return this.push((e=>({value:x(e)})))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(e){return this.push((t=>({value:A(e,t)})))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:a(e)?JSON.stringify(e):i(e)?e.toString():e})))}required(){return this.push((e=>e.length?{value:e}:this.invalid("["+this._type+"].required")))}trim(){return this.push((e=>({value:e.trim()})))}upperCase(){return this.push((e=>({value:e.toUpperCase()})))}upperCamelCase(){return this.push((e=>({value:w(e)})))}}class B{static array(e){return(new I).items(e)}static boolean(){return new L}static number(){return new T}static object(e){return(new q).keys(e)}static string(){return new H}test(e,...t){return!h(B.validate(e,...t).error)}testAsync(e,...t){return F(this,void 0,void 0,(function*(){return!h((yield B.validateAsync(e,...t)).error)}))}static validate(e,...t){let r={value:e};for(let e=0;e<t.length;e++)if(r=t[e].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(e,...t){return F(this,void 0,void 0,(function*(){let r={value:e};for(let e=0;e<t.length;e++)if(r=yield t[e].validateAsync(r.value),r.error)return{error:r.error};return r}))}}return e.EventEmitter=class{constructor(){this.events=new Map}on(e,t){const r=this.events.get(e);return r?r.push(t):this.events.set(e,[t]),{dispose:()=>this.off(e,t)}}once(e,t){const r=s=>{t(s),this.off(e,r)};return this.on(e,r),{dispose:()=>this.off(e,r)}}off(e,t){const r=this.events.get(e);if(t){if(r){const e=r.indexOf(t);e>=0&&r.splice(e,1)}}else this.events.set(e,[])}emit(e,t){const r=this.events.get(e);r&&r.forEach((e=>e(t)))}},e.Validation=B,e.ValidationBase=U,e.allowedObject=E,e.camelCase=b,e.chunkArray=(e,t)=>{const r=[];for(let s=0;s<e.length;s+=t)r.push(e.slice(s,s+t));return r},e.clamp=o,e.cutText=(e,t)=>{if(e.length>t){const r=e.substring(0,t).split(" ");return r.pop(),r.join(" ")+"..."}return e},e.dateFormat=(e,t)=>t.replace(/%[yYmdHMS]/g,(t=>{let r;switch(t){case"%y":r=e.getFullYear();break;case"%Y":return e.getFullYear().toString();case"%m":r=e.getMonth()+1;break;case"%d":r=e.getDate();break;case"%H":r=e.getHours();break;case"%M":r=e.getMinutes();break;case"%S":r=e.getSeconds();break;default:return t.slice(1)}return("0"+r.toString()).slice(-2)})),e.debounce=(e,t)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>e(...s)),t)}},e.decodeHTML=e=>e.replace(/&#(\d+);/g,((e,t)=>String.fromCharCode(t))),e.deniedObject=(e,t)=>E(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,s)=>r(e)?g(e,t,s):a(e)?_(e,t,s):null,e.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>F(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),e.forEachIndex=g,e.forEachKey=_,e.forEachSeries=(e,t,r)=>F(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),e.isArray=r,e.isBoolean=s,e.isDef=i,e.isFloat=e=>n(e)&&!!(e%1),e.isFunction=e=>"function"==l(e),e.isInteger=e=>n(e)&&!(e%1),e.isNull=e=>null===e,e.isNumber=n,e.isObject=a,e.isString=h,e.isUndefined=u,e.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.lowerCamelCase=x,e.map=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length;n++)s=i[n],e[s]=t.call(r,e[s],s,e);return e},e.mask=A,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),M(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=M,e.maskTest=(e,t)=>e.length==O(e,t).length,e.noop=()=>{},e.parseSize=e=>{if(n(e))return e+"px";const[,t,r,s]=(e=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e)||["","0","",""];return(r.length?parseFloat(t+"."+r):parseInt(t))+(s||"px")},e.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>("0"+c(e,t)).slice(-2))),e.simpleID=()=>v()+m(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=k,e.toArray=e=>r(e)?e:i(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=l,e.uniqueID=()=>f()+f()+f()+f(),e.upperCamelCase=w,e.uuidv4=()=>d()+"-"+m()+"-4"+v()+"-"+m()+"-"+(d()+m()),e}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/utils.js",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "JavaScript utility library for web and nodejs development",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "unpkg": "./dist/index.min.js",
@@ -43,16 +43,16 @@
43
43
  },
44
44
  "homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
45
45
  "devDependencies": {
46
- "@rollup/plugin-commonjs": "^23.0.4",
47
- "@rollup/plugin-terser": "^0.2.0",
48
- "@rollup/plugin-typescript": "^10.0.1",
49
- "@types/node": "^18.11.13",
50
- "@typescript-eslint/eslint-plugin": "^5.46.0",
51
- "@typescript-eslint/parser": "^5.46.0",
52
- "eslint": "^8.29.0",
53
- "rollup": "^3.7.3",
54
- "rollup-plugin-dts": "^5.0.0",
46
+ "@rollup/plugin-commonjs": "^24.0.1",
47
+ "@rollup/plugin-terser": "^0.4.0",
48
+ "@rollup/plugin-typescript": "^11.0.0",
49
+ "@types/node": "^18.14.2",
50
+ "@typescript-eslint/eslint-plugin": "^5.54.0",
51
+ "@typescript-eslint/parser": "^5.54.0",
52
+ "eslint": "^8.35.0",
53
+ "rollup": "^3.17.3",
54
+ "rollup-plugin-dts": "^5.2.0",
55
55
  "ts-node-dev": "^2.0.0",
56
- "tslib": "^2.4.1"
56
+ "tslib": "^2.5.0"
57
57
  }
58
58
  }