@andrewcaires/utils.js 0.2.8 → 0.2.10

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/dist/index.cjs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.8
2
+ * @andrewcaires/utils.js v0.2.10
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- "use strict";const e={},t=e=>"array"==h(e),r=e=>"boolean"==h(e),s=e=>"null"!=h(e),i=e=>"number"==h(e),n=e=>"object"==h(e),a=e=>"string"==h(e),o=e=>void 0===e,h=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},c=e=>Math.random().toString(16).slice(e),p=()=>c(-3),v=()=>c(-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},g=(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},f=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),_=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),y=e=>_(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),b=y,w=e=>_(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),k=e=>e.split("").reverse().join("");console.log(f("andrew>"));const C=e=>/^[A-Za-z]$/.test(e),j=e=>(e.mask--,""),A=(e,t,r)=>{r.mask++,r.text++;const s=e[r.mask],i=t[r.text];return o(s)||o(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)+A(e,t,r)},S=(e,t)=>A(e,t,{mask:-1,text:-1}),O=S,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{h(s.next(e))}catch(e){n(e)}}function o(e){try{h(s.throw(e))}catch(e){n(e)}}function h(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,o)}h((s=s.apply(e,t||[])).next())}))}class I{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 F(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 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 L extends I{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 T extends I{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 U extends I{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 q extends I{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 H extends I{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:y(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:S(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:w(e)})))}}class B{static array(e){return(new L).items(e)}static boolean(){return new T}static number(){return new U}static object(e){return(new q).keys(e)}static string(){return new H}test(e,...t){return!a(B.validate(e,...t).error)}testAsync(e,...t){return F(this,void 0,void 0,(function*(){return!a((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}))}}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=B,exports.ValidationBase=I,exports.allowedObject=E,exports.camelCase=b,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)=>E(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,r,s)=>t(e)?x(e,r,s):n(e)?g(e,r,s):null,exports.encodeHTML=f,exports.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))))})),exports.forEachIndex=x,exports.forEachKey=g,exports.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)})),exports.isArray=t,exports.isBoolean=r,exports.isDef=s,exports.isFloat=e=>i(e)&&!!(e%1),exports.isFunction=e=>"function"==h(e),exports.isInteger=e=>i(e)&&!(e%1),exports.isNull=e=>null===e,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isUndefined=o,exports.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.lowerCamelCase=y,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=S,exports.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)),exports.maskReverse=M,exports.maskTest=(e,t)=>e.length==O(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=()=>p()+v(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=k,exports.toArray=e=>t(e)?e:s(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=h,exports.uniqueID=()=>d()+d()+d()+d(),exports.upperCamelCase=w,exports.uuidv4=()=>m()+"-"+v()+"-4"+p()+"-"+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},g=(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},f=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),y=e=>f(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),_=y,b=e=>f(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--,""),A=(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)+A(e,t,r)},j=(e,t)=>A(e,t,{mask:-1,text:-1}),S=j,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:y(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:j(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=_,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)?g(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=g,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=y,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=j,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.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),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,10 +1,11 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.8
2
+ * @andrewcaires/utils.js v0.2.10
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
7
  declare const chunkArray: <T>(array: T[], length: number) => T[][];
8
+ declare const rangeArray: (start: number, end: number, step?: number) => number[];
8
9
  declare const toArray: (value: any) => Array<any>;
9
10
 
10
11
  declare const toBool: (value: string) => boolean;
@@ -189,4 +190,4 @@ declare class Validation {
189
190
  static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<ValidationResult>;
190
191
  }
191
192
 
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 };
193
+ 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, rangeArray, 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.8
2
+ * @andrewcaires/utils.js v0.2.10
3
3
  * JavaScript utility library for web and nodejs development
4
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)),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("");console.log(I("andrew>"));const 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};
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,r=1)=>Array.from({length:(e-t)/r+1},((e,s)=>t+s*r)),d=t=>e(t)?t:s(t)?[t]:[],g=t=>"true"===t||"1"===t,f=(t,e,r)=>Math.max(e,Math.min(r,t)),_=(t,e)=>{return(r=parseInt(t,16)+e,f(r,0,255)).toString(16);var r},y=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+_(t,e)).slice(-2))),x=t=>Math.random().toString(16).slice(t),b=()=>x(-3),w=()=>x(-4),k=()=>w()+w(),C=()=>k()+k(),j=()=>b()+w(),A=()=>C()+C()+C()+C(),S=()=>k()+"-"+w()+"-4"+b()+"-"+w()+"-"+(k()+w()),O=(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)})),M=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class U{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 F=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},L=(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},q=(t,r,s)=>e(t)?F(t,r,s):a(t)?L(t,r,s):null,Y=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},$=(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=()=>{},H=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},I=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),P=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),T=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),Z=t=>T(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),z=Z,N=t=>T(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),W=t=>t.split("").reverse().join(""),B=t=>/^[A-Za-z]$/.test(t),D=t=>(t.mask--,""),J=(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:D(r);case"A":case"Z":return B(e)?e:D(r);case"L":return B(e)?e.toLowerCase():D(r);case"U":return B(e)?e.toUpperCase():D(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:D(r);default:return t!=e&&r.text--,t}})(s,i,r)+J(t,e,r)},G=(t,e)=>J(t,e,{mask:-1,text:-1}),K=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),V(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),Q=G,R=(t,e)=>t.length==Q(t,e).length,V=(t,e)=>W(Q(W(t),W(e))),X=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),tt=(t,e)=>X(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function et(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 rt=(t,e,r)=>et(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),st=(t,e,r)=>et(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),it=t=>new Promise((e=>setTimeout(e,t))),nt=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 at{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 et(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 et(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 ht extends at{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 ut extends at{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 lt extends at{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 ot extends at{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 ct extends at{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:Z(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:G(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:N(t)})))}}class vt{static array(t){return(new ht).items(t)}static boolean(){return new ut}static number(){return new lt}static object(t){return(new ot).keys(t)}static string(){return new ct}test(t,...e){return!h(vt.validate(t,...e).error)}testAsync(t,...e){return et(this,void 0,void 0,(function*(){return!h((yield vt.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 et(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{U as EventEmitter,vt as Validation,at as ValidationBase,X as allowedObject,z as camelCase,p as chunkArray,f as clamp,H as cutText,O as dateFormat,M as debounce,I as decodeHTML,tt as deniedObject,q as each,P as encodeHTML,rt as forEachAsync,F as forEachIndex,L as forEachKey,st 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,Y as loop,Z as lowerCamelCase,$ as map,G as mask,K as maskMoney,V as maskReverse,R as maskTest,E as noop,nt as parseSize,m as rangeArray,y as shadeColor,j as simpleID,it as sleep,W as stringReverse,d as toArray,g as toBool,v as type,A as uniqueID,N as upperCamelCase,S as uuidv4};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.8
2
+ * @andrewcaires/utils.js v0.2.10
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2023 Andrew Caires
5
5
  * @license: MIT
6
6
  */
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=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),x=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),b=e=>x(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),w=b,k=e=>x(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),C=e=>e.split("").reverse().join("");console.log(y("andrew>"));const j=e=>/^[A-Za-z]$/.test(e),S=e=>(e.mask--,""),A=(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:S(r);case"A":case"Z":return j(t)?t:S(r);case"L":return j(t)?t.toLowerCase():S(r);case"U":return j(t)?t.toUpperCase():S(r);case"S":return(e=>/^[^A-Za-z0-9]$/.test(e))(t)?t:S(r);default:return e!=t&&r.text--,e}})(s,i,r)+A(e,t,r)},O=(e,t)=>A(e,t,{mask:-1,text:-1}),M=O,E=(e,t)=>C(M(C(e),C(t))),F=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function U(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 I{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 U(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 U(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 L extends I{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 T extends I{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 q extends I{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 H extends I{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 B extends I{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:b(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:O(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:k(e)})))}}class D{static array(e){return(new L).items(e)}static boolean(){return new T}static number(){return new q}static object(e){return(new H).keys(e)}static string(){return new B}test(e,...t){return!h(D.validate(e,...t).error)}testAsync(e,...t){return U(this,void 0,void 0,(function*(){return!h((yield D.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 U(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=D,e.ValidationBase=I,e.allowedObject=F,e.camelCase=w,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)=>F(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=y,e.forEachAsync=(e,t,r)=>U(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)=>U(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=b,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=O,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),E(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=E,e.maskTest=(e,t)=>e.length==M(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=C,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=k,e.uuidv4=()=>d()+"-"+m()+"-4"+v()+"-"+m()+"-"+(d()+m()),e}({});
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},y=(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},_=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),x=e=>_(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),b=x,w=e=>_(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),A=e=>(e.mask--,""),j=(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:A(r);case"A":case"Z":return C(t)?t:A(r);case"L":return C(t)?t.toLowerCase():A(r);case"U":return C(t)?t.toUpperCase():A(r);case"S":return(e=>/^[^A-Za-z0-9]$/.test(e))(t)?t:A(r);default:return e!=t&&r.text--,e}})(s,i,r)+j(e,t,r)},S=(e,t)=>j(e,t,{mask:-1,text:-1}),O=S,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:S(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)?y(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=y,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=S,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.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),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.8",
3
+ "version": "0.2.10",
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",
@@ -46,11 +46,11 @@
46
46
  "@rollup/plugin-commonjs": "^24.0.1",
47
47
  "@rollup/plugin-terser": "^0.4.0",
48
48
  "@rollup/plugin-typescript": "^11.0.0",
49
- "@types/node": "^18.14.2",
49
+ "@types/node": "^18.14.6",
50
50
  "@typescript-eslint/eslint-plugin": "^5.54.0",
51
51
  "@typescript-eslint/parser": "^5.54.0",
52
52
  "eslint": "^8.35.0",
53
- "rollup": "^3.17.3",
53
+ "rollup": "^3.18.0",
54
54
  "rollup-plugin-dts": "^5.2.0",
55
55
  "ts-node-dev": "^2.0.0",
56
56
  "tslib": "^2.5.0"