@andrewcaires/utils.js 0.2.16 → 0.2.17

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.16
2
+ * @andrewcaires/utils.js v0.2.17
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"==l(e),r=e=>"boolean"==l(e),s=e=>"null"!=l(e),i=e=>"number"==l(e),n=e=>"object"==l(e),a=e=>"string"==l(e),o=e=>void 0===e,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 h=(e,t,r)=>Math.max(t,Math.min(r,e)),u=e=>("0"+e).slice(-2),p=e=>parseInt(e,16)||0,c=(e,t)=>{return(r=p(e)+t,h(r,0,255)).toString(16);var r},v=e=>Math.random().toString(16).slice(e),x=()=>v(-3),m=()=>v(-4),d=()=>m()+m(),g=()=>d()+d(),f=e=>("0"+e.toString()).slice(-2),y={"%y":e=>f(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>f(e.getMonth()+1),"%d":e=>f(e.getDate()),"%H":e=>f(e.getHours()),"%M":e=>f(e.getMinutes()),"%S":e=>f(e.getSeconds())};const _=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},b=(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},k="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",w=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=k.indexOf(e[i]),a=k.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:k[(r?n-a+k.length:n+a)%k.length]}return s},C=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),S=e=>/^[\d]$/.test(e),j=e=>/^[A-Za-z]$/.test(e),O=e=>/^[^\w]$/.test(e),A=e=>C(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),I=A,M=e=>C(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),E=e=>e.split("").reverse().join(""),T=e=>(e.maskIndex--,""),F={"#":({value:e})=>e,0:e=>S(e.value)?e.value:T(e),9:e=>S(e.value)?e.value:T(e),A:e=>j(e.value)?e.value:T(e),Z:e=>j(e.value)?e.value:T(e),L:e=>j(e.value)?e.value.toLowerCase():T(e),U:e=>j(e.value)?e.value.toUpperCase():T(e),S:e=>O(e.value)?e.value:T(e)},L=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:i}=e;e.key=t[r],e.value=s[i];const{key:n,value:a}=e;return o(n)||o(a)?"":(e=>F[e.key]?F[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+L(e)},H=(e,t)=>L({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),U=H,q=(e,t)=>E(U(E(e),E(t))),D=36e5,N=24*D,B={ms:1,s:1e3,m:6e4,h:D,d:N,w:6048e5,y:315576e5},Y=["y","d","h","m","s","ms"],$=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function P(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{l(s.next(e))}catch(e){n(e)}}function o(e){try{l(s.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,o)}l((s=s.apply(e,t||[])).next())}))}class R{constructor(){this.name="",this._error="",this._filter=[],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}filter(...e){return this._filter=e,this.unshift((e=>{const t=-1==this._filter.indexOf(e);return{break:!t,value:t?e:void 0}}))}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 P(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 P(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};if(t.break)return{value:t.value}}return{value:t.value}}))}}class z extends R{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 J extends R{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",!0,"true"].indexOf(e):e})))}}class V extends R{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 W extends R{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(n(e)){const t=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:o}=n.validate(e[i]);a&&t.push(a),s(o)&&(r[i]=o)})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{if(!n(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?$(t,e):e}}))}}class Z extends R{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:A(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:H(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:M(e)})))}}class K{static array(e){return(new z).items(e)}static boolean(){return new J}static number(){return new V}static object(e){return(new W).keys(e)}static string(){return new Z}test(e,...t){return!a(K.validate(e,...t).error)}testAsync(e,...t){return P(this,void 0,void 0,(function*(){return!a((yield K.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 P(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=K,exports.ValidationBase=R,exports.allowedObject=$,exports.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},exports.camelCase=I,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=h,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=>y[t]?y[t](e):t.slice(1))),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)=>$(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,r,s)=>t(e)?_(e,r,s):n(e)?b(e,r,s):null,exports.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>P(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),exports.forEachIndex=_,exports.forEachKey=b,exports.forEachSeries=(e,t,r)=>P(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),exports.hash=e=>(e=w(k,e,!1),console.log(e),{generate:t=>w(t,e,!1),reverse:t=>w(t,e,!0)}),exports.hexToRgb=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map((e=>p(e))))||[]},exports.isArray=t,exports.isBoolean=r,exports.isCharacter=e=>/^[\w]$/.test(e),exports.isDef=s,exports.isDigit=S,exports.isFloat=e=>i(e)&&!!(e%1),exports.isFunction=e=>"function"==l(e),exports.isInteger=e=>i(e)&&!(e%1),exports.isLetter=j,exports.isNull=e=>null===e,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isSymbol=O,exports.isUndefined=o,exports.lerp=(e,t,r)=>e+(t-e)*h(r,0,1),exports.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.lowerCamelCase=A,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=H,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),q(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=q,exports.maskTest=(e,t)=>e.length==U(e,t).length,exports.msToString=e=>{const t=[];return Y.forEach((r=>{const s=B[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},exports.noop=()=>{},exports.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*B[r[2]]||0;return s},exports.parseSize=e=>{if(i(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},exports.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),exports.rgbToHex=(e,t,r)=>"#"+u(e.toString(16))+u(t.toString(16))+u(r.toString(16)),exports.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>u(c(e,t)))),exports.shuffleArray=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},exports.simpleID=()=>x()+m(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=E,exports.toArray=e=>t(e)?e:s(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=l,exports.uniqueID=()=>g()+g()+g()+g(),exports.upperCamelCase=M,exports.uuidv4=()=>d()+"-"+m()+"-4"+x()+"-"+m()+"-"+(d()+m());
7
+ "use strict";const e={},t=e=>"array"==l(e),r=e=>"boolean"==l(e),s=e=>"null"!=l(e),i=e=>"number"==l(e),n=e=>"object"==l(e),a=e=>"string"==l(e),o=e=>void 0===e,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 h=(e,t,r)=>Math.max(t,Math.min(r,e)),u=e=>("0"+e).slice(-2),p=e=>parseInt(e,16)||0,c=(e,t)=>{return(r=p(e)+t,h(r,0,255)).toString(16);var r},v=e=>Math.random().toString(16).slice(e),x=()=>v(-3),m=()=>v(-4),d=()=>m()+m(),g=()=>d()+d(),f=e=>("0"+e.toString()).slice(-2),y={"%y":e=>f(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>f(e.getMonth()+1),"%d":e=>f(e.getDate()),"%H":e=>f(e.getHours()),"%M":e=>f(e.getMinutes()),"%S":e=>f(e.getSeconds())};const _=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},b=(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},k="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",w=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=k.indexOf(e[i]),a=k.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:k[(r?n-a+k.length:n+a)%k.length]}return s},C=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),S=e=>/^[\d]$/.test(e),j=e=>/^[A-Za-z]$/.test(e),O=e=>/^[^\w]$/.test(e),A=e=>C(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),I=A,M=e=>C(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),E=e=>e.split("").reverse().join(""),T=e=>(e.maskIndex--,""),F={"#":({value:e})=>e,0:e=>S(e.value)?e.value:T(e),9:e=>S(e.value)?e.value:T(e),A:e=>j(e.value)?e.value:T(e),Z:e=>j(e.value)?e.value:T(e),L:e=>j(e.value)?e.value.toLowerCase():T(e),U:e=>j(e.value)?e.value.toUpperCase():T(e),S:e=>O(e.value)?e.value:T(e)},L=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:i}=e;e.key=t[r],e.value=s[i];const{key:n,value:a}=e;return o(n)||o(a)?"":(e=>F[e.key]?F[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+L(e)},H=(e,t)=>L({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),U=H,q=(e,t)=>E(U(E(e),E(t))),D=36e5,N=24*D,B={ms:1,s:1e3,m:6e4,h:D,d:N,w:6048e5,y:315576e5},Y=["y","d","h","m","s","ms"],$=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function P(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{l(s.next(e))}catch(e){n(e)}}function o(e){try{l(s.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,o)}l((s=s.apply(e,t||[])).next())}))}class R{constructor(){this.name="",this._error="",this._filter=[],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}filter(...e){return this._filter=e,this.unshift((e=>{const t=-1==this._filter.indexOf(e);return{break:!t,value:t?e:void 0}}))}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 P(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 P(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};if(t.break)return{value:t.value}}return{value:t.value}}))}}class z extends R{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 J extends R{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",!0,"true"].indexOf(e):e})))}}class V extends R{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 W extends R{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(n(e)){const t=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:o}=n.validate(e[i]);a&&t.push(a),s(o)&&(r[i]=o)})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{if(!n(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?$(t,e):e}}))}}class Z extends R{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:A(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:H(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:M(e)})))}}class K{static array(e){return(new z).items(e)}static boolean(){return new J}static number(){return new V}static object(e){return(new W).keys(e)}static string(){return new Z}test(e,...t){return!a(K.validate(e,...t).error)}testAsync(e,...t){return P(this,void 0,void 0,(function*(){return!a((yield K.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 P(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=K,exports.ValidationBase=R,exports.allowedObject=$,exports.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},exports.camelCase=I,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=h,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=>y[t]?y[t](e):t.slice(1))),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)=>$(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,r,s)=>t(e)?_(e,r,s):n(e)?b(e,r,s):null,exports.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>P(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),exports.forEachIndex=_,exports.forEachKey=b,exports.forEachSeries=(e,t,r)=>P(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),exports.hash=e=>(e=w(k,e,!1),{generate:t=>w(t,e,!1),reverse:t=>w(t,e,!0)}),exports.hexToRgb=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map((e=>p(e))))||[]},exports.isArray=t,exports.isBoolean=r,exports.isCharacter=e=>/^[\w]$/.test(e),exports.isDef=s,exports.isDigit=S,exports.isFloat=e=>i(e)&&!!(e%1),exports.isFunction=e=>"function"==l(e),exports.isInteger=e=>i(e)&&!(e%1),exports.isLetter=j,exports.isNull=e=>null===e,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isSymbol=O,exports.isUndefined=o,exports.lerp=(e,t,r)=>e+(t-e)*h(r,0,1),exports.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.lowerCamelCase=A,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=H,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),q(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=q,exports.maskTest=(e,t)=>e.length==U(e,t).length,exports.msToString=e=>{const t=[];return Y.forEach((r=>{const s=B[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},exports.noop=()=>{},exports.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*B[r[2]]||0;return s},exports.parseSize=e=>{if(i(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},exports.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),exports.rgbToHex=(e,t,r)=>"#"+u(e.toString(16))+u(t.toString(16))+u(r.toString(16)),exports.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>u(c(e,t)))),exports.shuffleArray=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},exports.simpleID=()=>x()+m(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=E,exports.toArray=e=>t(e)?e:s(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=l,exports.uniqueID=()=>g()+g()+g()+g(),exports.upperCamelCase=M,exports.uuidv4=()=>d()+"-"+m()+"-4"+x()+"-"+m()+"-"+(d()+m());
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.16
2
+ * @andrewcaires/utils.js v0.2.17
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2023 Andrew Caires
5
5
  * @license: MIT
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.16
2
+ * @andrewcaires/utils.js v0.2.17
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),l=t=>n(t)&&!!(t%1),u=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=>{for(let e=t.length-1;e>0;e--){const r=Math.floor(Math.random()*(e+1));[t[e],t[r]]=[t[r],t[e]]}return t},g=t=>e(t)?t:s(t)?[t]:[],f=t=>"true"===t||"1"===t,y=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},_=(t,e,r)=>Math.max(e,Math.min(r,t)),x=(t,e,r)=>t+(e-t)*_(r,0,1),k=t=>("0"+t).slice(-2),w=t=>parseInt(t,16)||0,b=(t,e)=>{return(r=w(t)+e,_(r,0,255)).toString(16);var r},S=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>w(t))))||[]},j=(t,e,r)=>"#"+k(t.toString(16))+k(e.toString(16))+k(r.toString(16)),C=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>k(b(t,e)))),O=t=>Math.random().toString(16).slice(t),A=()=>O(-3),I=()=>O(-4),M=()=>I()+I(),F=()=>M()+M(),L=()=>A()+I(),U=()=>F()+F()+F()+F(),q=()=>M()+"-"+I()+"-4"+A()+"-"+I()+"-"+(M()+I()),E=t=>("0"+t.toString()).slice(-2),Y={"%y":t=>E(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>E(t.getMonth()+1),"%d":t=>E(t.getDate()),"%H":t=>E(t.getHours()),"%M":t=>E(t.getMinutes()),"%S":t=>E(t.getSeconds())},$=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>Y[e]?Y[e](t):e.slice(1))),H=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class N{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 P=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},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},J=(t,r,s)=>e(t)?P(t,r,s):a(t)?T(t,r,s):null,W=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},Z=(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},z=()=>{},B="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",D=(t,e,r)=>{let s="";for(let i=0;i<t.length;i++){const n=B.indexOf(t[i]),a=B.indexOf(e[i%e.length]);s+=-1===n||-1===a?t[i]:B[(r?n-a+B.length:n+a)%B.length]}return s},G=t=>(t=D(B,t,!1),console.log(t),{generate:e=>D(e,t,!1),reverse:e=>D(e,t,!0)}),K=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},Q=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),R=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),V=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),X=t=>/^[\w]$/.test(t),tt=t=>/^[\d]$/.test(t),et=t=>/^[A-Za-z]$/.test(t),rt=t=>/^[^\w]$/.test(t),st=t=>V(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),it=st,nt=t=>V(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),at=t=>t.split("").reverse().join(""),ht=t=>(t.maskIndex--,""),lt={"#":({value:t})=>t,0:t=>tt(t.value)?t.value:ht(t),9:t=>tt(t.value)?t.value:ht(t),A:t=>et(t.value)?t.value:ht(t),Z:t=>et(t.value)?t.value:ht(t),L:t=>et(t.value)?t.value.toLowerCase():ht(t),U:t=>et(t.value)?t.value.toUpperCase():ht(t),S:t=>rt(t.value)?t.value:ht(t)},ut=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:r,text:s,textIndex:i}=t;t.key=e[r],t.value=s[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>lt[t.key]?lt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+ut(t)},ot=(t,e)=>ut({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),ct=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),mt(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),vt=ot,pt=(t,e)=>t.length==vt(t,e).length,mt=(t,e)=>at(vt(at(t),at(e))),dt=36e5,gt=24*dt,ft={ms:1,s:1e3,m:6e4,h:dt,d:gt,w:6048e5,y:315576e5},yt=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=e.exec(t));)r.index===e.lastIndex&&e.lastIndex++,s+=parseFloat(r[1])*ft[r[2]]||0;return s},_t=["y","d","h","m","s","ms"],xt=t=>{const e=[];return _t.forEach((r=>{const s=ft[r],i=(t-t%s)/s;i&&(t-=i*s,e.push(i.toString()+r))})),e.join(" ")},kt=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),wt=(t,e)=>kt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function bt(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{l(s.next(t))}catch(t){n(t)}}function h(t){try{l(s.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}l((s=s.apply(t,e||[])).next())}))}const St=(t,e,r)=>bt(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),jt=(t,e,r)=>bt(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),Ct=t=>new Promise((e=>setTimeout(e,t))),Ot=t=>{if(n(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"};class At{constructor(){this.name="",this._error="",this._filter=[],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}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}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 bt(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 bt(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};if(e.break)return{value:e.value}}return{value:e.value}}))}}class It 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 Mt 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",!0,"true"].indexOf(t):t})))}}class Ft 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 Lt extends At{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(a(t)){const e=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:h}=n.validate(t[i]);a&&e.push(a),s(h)&&(r[i]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!a(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?kt(e,t):t}}))}}class Ut 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:st(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:ot(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:nt(t)})))}}class qt{static array(t){return(new It).items(t)}static boolean(){return new Mt}static number(){return new Ft}static object(t){return(new Lt).keys(t)}static string(){return new Ut}test(t,...e){return!h(qt.validate(t,...e).error)}testAsync(t,...e){return bt(this,void 0,void 0,(function*(){return!h((yield qt.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 bt(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{N as EventEmitter,qt as Validation,At as ValidationBase,kt as allowedObject,y as cache,it as camelCase,p as chunkArray,_ as clamp,K as cutText,$ as dateFormat,H as debounce,Q as decodeHTML,wt as deniedObject,J as each,R as encodeHTML,St as forEachAsync,P as forEachIndex,T as forEachKey,jt as forEachSeries,G as hash,S as hexToRgb,e as isArray,r as isBoolean,X as isCharacter,s as isDef,tt as isDigit,l as isFloat,i as isFunction,u as isInteger,et as isLetter,o as isNull,n as isNumber,a as isObject,h as isString,rt as isSymbol,c as isUndefined,x as lerp,W as loop,st as lowerCamelCase,Z as map,ot as mask,ct as maskMoney,mt as maskReverse,pt as maskTest,xt as msToString,z as noop,yt as parseMs,Ot as parseSize,m as rangeArray,j as rgbToHex,C as shadeColor,d as shuffleArray,L as simpleID,Ct as sleep,at as stringReverse,g as toArray,f as toBool,v as type,U as uniqueID,nt as upperCamelCase,q 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),l=t=>n(t)&&!!(t%1),u=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=>{for(let e=t.length-1;e>0;e--){const r=Math.floor(Math.random()*(e+1));[t[e],t[r]]=[t[r],t[e]]}return t},g=t=>e(t)?t:s(t)?[t]:[],f=t=>"true"===t||"1"===t,y=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},_=(t,e,r)=>Math.max(e,Math.min(r,t)),x=(t,e,r)=>t+(e-t)*_(r,0,1),k=t=>("0"+t).slice(-2),w=t=>parseInt(t,16)||0,b=(t,e)=>{return(r=w(t)+e,_(r,0,255)).toString(16);var r},S=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>w(t))))||[]},j=(t,e,r)=>"#"+k(t.toString(16))+k(e.toString(16))+k(r.toString(16)),C=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>k(b(t,e)))),O=t=>Math.random().toString(16).slice(t),A=()=>O(-3),I=()=>O(-4),M=()=>I()+I(),F=()=>M()+M(),L=()=>A()+I(),U=()=>F()+F()+F()+F(),q=()=>M()+"-"+I()+"-4"+A()+"-"+I()+"-"+(M()+I()),E=t=>("0"+t.toString()).slice(-2),Y={"%y":t=>E(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>E(t.getMonth()+1),"%d":t=>E(t.getDate()),"%H":t=>E(t.getHours()),"%M":t=>E(t.getMinutes()),"%S":t=>E(t.getSeconds())},$=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>Y[e]?Y[e](t):e.slice(1))),H=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class N{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 P=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},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},J=(t,r,s)=>e(t)?P(t,r,s):a(t)?T(t,r,s):null,W=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},Z=(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},z=()=>{},B="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",D=(t,e,r)=>{let s="";for(let i=0;i<t.length;i++){const n=B.indexOf(t[i]),a=B.indexOf(e[i%e.length]);s+=-1===n||-1===a?t[i]:B[(r?n-a+B.length:n+a)%B.length]}return s},G=t=>(t=D(B,t,!1),{generate:e=>D(e,t,!1),reverse:e=>D(e,t,!0)}),K=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},Q=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),R=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),V=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),X=t=>/^[\w]$/.test(t),tt=t=>/^[\d]$/.test(t),et=t=>/^[A-Za-z]$/.test(t),rt=t=>/^[^\w]$/.test(t),st=t=>V(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),it=st,nt=t=>V(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),at=t=>t.split("").reverse().join(""),ht=t=>(t.maskIndex--,""),lt={"#":({value:t})=>t,0:t=>tt(t.value)?t.value:ht(t),9:t=>tt(t.value)?t.value:ht(t),A:t=>et(t.value)?t.value:ht(t),Z:t=>et(t.value)?t.value:ht(t),L:t=>et(t.value)?t.value.toLowerCase():ht(t),U:t=>et(t.value)?t.value.toUpperCase():ht(t),S:t=>rt(t.value)?t.value:ht(t)},ut=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:r,text:s,textIndex:i}=t;t.key=e[r],t.value=s[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>lt[t.key]?lt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+ut(t)},ot=(t,e)=>ut({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),ct=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),mt(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),vt=ot,pt=(t,e)=>t.length==vt(t,e).length,mt=(t,e)=>at(vt(at(t),at(e))),dt=36e5,gt=24*dt,ft={ms:1,s:1e3,m:6e4,h:dt,d:gt,w:6048e5,y:315576e5},yt=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=e.exec(t));)r.index===e.lastIndex&&e.lastIndex++,s+=parseFloat(r[1])*ft[r[2]]||0;return s},_t=["y","d","h","m","s","ms"],xt=t=>{const e=[];return _t.forEach((r=>{const s=ft[r],i=(t-t%s)/s;i&&(t-=i*s,e.push(i.toString()+r))})),e.join(" ")},kt=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),wt=(t,e)=>kt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function bt(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{l(s.next(t))}catch(t){n(t)}}function h(t){try{l(s.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}l((s=s.apply(t,e||[])).next())}))}const St=(t,e,r)=>bt(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),jt=(t,e,r)=>bt(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),Ct=t=>new Promise((e=>setTimeout(e,t))),Ot=t=>{if(n(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"};class At{constructor(){this.name="",this._error="",this._filter=[],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}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}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 bt(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 bt(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};if(e.break)return{value:e.value}}return{value:e.value}}))}}class It 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 Mt 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",!0,"true"].indexOf(t):t})))}}class Ft 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 Lt extends At{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(a(t)){const e=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:h}=n.validate(t[i]);a&&e.push(a),s(h)&&(r[i]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!a(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?kt(e,t):t}}))}}class Ut 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:st(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:ot(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:nt(t)})))}}class qt{static array(t){return(new It).items(t)}static boolean(){return new Mt}static number(){return new Ft}static object(t){return(new Lt).keys(t)}static string(){return new Ut}test(t,...e){return!h(qt.validate(t,...e).error)}testAsync(t,...e){return bt(this,void 0,void 0,(function*(){return!h((yield qt.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 bt(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{N as EventEmitter,qt as Validation,At as ValidationBase,kt as allowedObject,y as cache,it as camelCase,p as chunkArray,_ as clamp,K as cutText,$ as dateFormat,H as debounce,Q as decodeHTML,wt as deniedObject,J as each,R as encodeHTML,St as forEachAsync,P as forEachIndex,T as forEachKey,jt as forEachSeries,G as hash,S as hexToRgb,e as isArray,r as isBoolean,X as isCharacter,s as isDef,tt as isDigit,l as isFloat,i as isFunction,u as isInteger,et as isLetter,o as isNull,n as isNumber,a as isObject,h as isString,rt as isSymbol,c as isUndefined,x as lerp,W as loop,st as lowerCamelCase,Z as map,ot as mask,ct as maskMoney,mt as maskReverse,pt as maskTest,xt as msToString,z as noop,yt as parseMs,Ot as parseSize,m as rangeArray,j as rgbToHex,C as shadeColor,d as shuffleArray,L as simpleID,Ct as sleep,at as stringReverse,g as toArray,f as toBool,v as type,U as uniqueID,nt as upperCamelCase,q as uuidv4};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.16
2
+ * @andrewcaires/utils.js v0.2.17
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"==u(e),s=e=>"boolean"==u(e),i=e=>"null"!=u(e),n=e=>"number"==u(e),a=e=>"object"==u(e),l=e=>"string"==u(e),h=e=>void 0===e,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=(e,t,r)=>Math.max(t,Math.min(r,e)),c=e=>("0"+e).slice(-2),v=e=>parseInt(e,16)||0,p=(e,t)=>{return(r=v(e)+t,o(r,0,255)).toString(16);var r},m=e=>Math.random().toString(16).slice(e),d=()=>m(-3),g=()=>m(-4),f=()=>g()+g(),y=()=>f()+f(),_=e=>("0"+e.toString()).slice(-2),x={"%y":e=>_(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>_(e.getMonth()+1),"%d":e=>_(e.getDate()),"%H":e=>_(e.getHours()),"%M":e=>_(e.getMinutes()),"%S":e=>_(e.getSeconds())};const b=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},k=(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},w="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",S=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=w.indexOf(e[i]),a=w.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:w[(r?n-a+w.length:n+a)%w.length]}return s},C=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),j=e=>/^[\d]$/.test(e),O=e=>/^[A-Za-z]$/.test(e),A=e=>/^[^\w]$/.test(e),I=e=>C(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),M=I,E=e=>C(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),T=e=>e.split("").reverse().join(""),F=e=>(e.maskIndex--,""),L={"#":({value:e})=>e,0:e=>j(e.value)?e.value:F(e),9:e=>j(e.value)?e.value:F(e),A:e=>O(e.value)?e.value:F(e),Z:e=>O(e.value)?e.value:F(e),L:e=>O(e.value)?e.value.toLowerCase():F(e),U:e=>O(e.value)?e.value.toUpperCase():F(e),S:e=>A(e.value)?e.value:F(e)},U=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:i}=e;e.key=t[r],e.value=s[i];const{key:n,value:a}=e;return h(n)||h(a)?"":(e=>L[e.key]?L[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+U(e)},H=(e,t)=>U({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),q=H,D=(e,t)=>T(q(T(e),T(t))),N=36e5,B=24*N,Y={ms:1,s:1e3,m:6e4,h:N,d:B,w:6048e5,y:315576e5},$=["y","d","h","m","s","ms"],J=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function P(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 l(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,l)}h((s=s.apply(e,t||[])).next())}))}class R{constructor(){this.name="",this._error="",this._filter=[],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}filter(...e){return this._filter=e,this.unshift((e=>{const t=-1==this._filter.indexOf(e);return{break:!t,value:t?e:void 0}}))}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!l(this.validate(e).error)}testAsync(e){return P(this,void 0,void 0,(function*(){return!l((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 P(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};if(t.break)return{value:t.value}}return{value:t.value}}))}}class z extends R{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 V extends R{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",!0,"true"].indexOf(e):e})))}}class W extends R{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 Z extends R{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(a(e)){const t=[],r={};return Object.keys(this._keys).forEach((s=>{const n=this._keys[s];n.name=s;const{error:a,value:l}=n.validate(e[s]);a&&t.push(a),i(l)&&(r[s]=l)})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{if(!a(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?J(t,e):e}}))}}class K extends R{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>l(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:I(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:H(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:E(e)})))}}class G{static array(e){return(new z).items(e)}static boolean(){return new V}static number(){return new W}static object(e){return(new Z).keys(e)}static string(){return new K}test(e,...t){return!l(G.validate(e,...t).error)}testAsync(e,...t){return P(this,void 0,void 0,(function*(){return!l((yield G.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 P(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=G,e.ValidationBase=R,e.allowedObject=J,e.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},e.camelCase=M,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=>x[t]?x[t](e):t.slice(1))),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)=>J(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,s)=>r(e)?b(e,t,s):a(e)?k(e,t,s):null,e.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>P(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),e.forEachIndex=b,e.forEachKey=k,e.forEachSeries=(e,t,r)=>P(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),e.hash=e=>(e=S(w,e,!1),console.log(e),{generate:t=>S(t,e,!1),reverse:t=>S(t,e,!0)}),e.hexToRgb=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map((e=>v(e))))||[]},e.isArray=r,e.isBoolean=s,e.isCharacter=e=>/^[\w]$/.test(e),e.isDef=i,e.isDigit=j,e.isFloat=e=>n(e)&&!!(e%1),e.isFunction=e=>"function"==u(e),e.isInteger=e=>n(e)&&!(e%1),e.isLetter=O,e.isNull=e=>null===e,e.isNumber=n,e.isObject=a,e.isString=l,e.isSymbol=A,e.isUndefined=h,e.lerp=(e,t,r)=>e+(t-e)*o(r,0,1),e.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.lowerCamelCase=I,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=H,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),D(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=D,e.maskTest=(e,t)=>e.length==q(e,t).length,e.msToString=e=>{const t=[];return $.forEach((r=>{const s=Y[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},e.noop=()=>{},e.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*Y[r[2]]||0;return s},e.parseSize=e=>{if(n(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},e.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),e.rgbToHex=(e,t,r)=>"#"+c(e.toString(16))+c(t.toString(16))+c(r.toString(16)),e.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>c(p(e,t)))),e.shuffleArray=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},e.simpleID=()=>d()+g(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=T,e.toArray=e=>r(e)?e:i(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=u,e.uniqueID=()=>y()+y()+y()+y(),e.upperCamelCase=E,e.uuidv4=()=>f()+"-"+g()+"-4"+d()+"-"+g()+"-"+(f()+g()),e}({});
7
+ var UtilsJS=function(e){"use strict";const t={},r=e=>"array"==u(e),s=e=>"boolean"==u(e),i=e=>"null"!=u(e),n=e=>"number"==u(e),a=e=>"object"==u(e),l=e=>"string"==u(e),h=e=>void 0===e,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=(e,t,r)=>Math.max(t,Math.min(r,e)),c=e=>("0"+e).slice(-2),v=e=>parseInt(e,16)||0,p=(e,t)=>{return(r=v(e)+t,o(r,0,255)).toString(16);var r},m=e=>Math.random().toString(16).slice(e),d=()=>m(-3),g=()=>m(-4),f=()=>g()+g(),y=()=>f()+f(),_=e=>("0"+e.toString()).slice(-2),x={"%y":e=>_(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>_(e.getMonth()+1),"%d":e=>_(e.getDate()),"%H":e=>_(e.getHours()),"%M":e=>_(e.getMinutes()),"%S":e=>_(e.getSeconds())};const b=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},k=(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},w="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",S=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=w.indexOf(e[i]),a=w.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:w[(r?n-a+w.length:n+a)%w.length]}return s},C=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),j=e=>/^[\d]$/.test(e),O=e=>/^[A-Za-z]$/.test(e),A=e=>/^[^\w]$/.test(e),I=e=>C(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),M=I,E=e=>C(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),T=e=>e.split("").reverse().join(""),F=e=>(e.maskIndex--,""),L={"#":({value:e})=>e,0:e=>j(e.value)?e.value:F(e),9:e=>j(e.value)?e.value:F(e),A:e=>O(e.value)?e.value:F(e),Z:e=>O(e.value)?e.value:F(e),L:e=>O(e.value)?e.value.toLowerCase():F(e),U:e=>O(e.value)?e.value.toUpperCase():F(e),S:e=>A(e.value)?e.value:F(e)},U=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:i}=e;e.key=t[r],e.value=s[i];const{key:n,value:a}=e;return h(n)||h(a)?"":(e=>L[e.key]?L[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+U(e)},H=(e,t)=>U({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),q=H,D=(e,t)=>T(q(T(e),T(t))),N=36e5,B=24*N,Y={ms:1,s:1e3,m:6e4,h:N,d:B,w:6048e5,y:315576e5},$=["y","d","h","m","s","ms"],J=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function P(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 l(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,l)}h((s=s.apply(e,t||[])).next())}))}class R{constructor(){this.name="",this._error="",this._filter=[],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}filter(...e){return this._filter=e,this.unshift((e=>{const t=-1==this._filter.indexOf(e);return{break:!t,value:t?e:void 0}}))}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!l(this.validate(e).error)}testAsync(e){return P(this,void 0,void 0,(function*(){return!l((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 P(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};if(t.break)return{value:t.value}}return{value:t.value}}))}}class z extends R{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 V extends R{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",!0,"true"].indexOf(e):e})))}}class W extends R{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 Z extends R{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(a(e)){const t=[],r={};return Object.keys(this._keys).forEach((s=>{const n=this._keys[s];n.name=s;const{error:a,value:l}=n.validate(e[s]);a&&t.push(a),i(l)&&(r[s]=l)})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{if(!a(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?J(t,e):e}}))}}class K extends R{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>l(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:I(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:H(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:E(e)})))}}class G{static array(e){return(new z).items(e)}static boolean(){return new V}static number(){return new W}static object(e){return(new Z).keys(e)}static string(){return new K}test(e,...t){return!l(G.validate(e,...t).error)}testAsync(e,...t){return P(this,void 0,void 0,(function*(){return!l((yield G.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 P(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=G,e.ValidationBase=R,e.allowedObject=J,e.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},e.camelCase=M,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=>x[t]?x[t](e):t.slice(1))),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)=>J(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,s)=>r(e)?b(e,t,s):a(e)?k(e,t,s):null,e.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>P(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),e.forEachIndex=b,e.forEachKey=k,e.forEachSeries=(e,t,r)=>P(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),e.hash=e=>(e=S(w,e,!1),{generate:t=>S(t,e,!1),reverse:t=>S(t,e,!0)}),e.hexToRgb=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map((e=>v(e))))||[]},e.isArray=r,e.isBoolean=s,e.isCharacter=e=>/^[\w]$/.test(e),e.isDef=i,e.isDigit=j,e.isFloat=e=>n(e)&&!!(e%1),e.isFunction=e=>"function"==u(e),e.isInteger=e=>n(e)&&!(e%1),e.isLetter=O,e.isNull=e=>null===e,e.isNumber=n,e.isObject=a,e.isString=l,e.isSymbol=A,e.isUndefined=h,e.lerp=(e,t,r)=>e+(t-e)*o(r,0,1),e.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.lowerCamelCase=I,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=H,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),D(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=D,e.maskTest=(e,t)=>e.length==q(e,t).length,e.msToString=e=>{const t=[];return $.forEach((r=>{const s=Y[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},e.noop=()=>{},e.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*Y[r[2]]||0;return s},e.parseSize=e=>{if(n(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},e.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),e.rgbToHex=(e,t,r)=>"#"+c(e.toString(16))+c(t.toString(16))+c(r.toString(16)),e.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>c(p(e,t)))),e.shuffleArray=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},e.simpleID=()=>d()+g(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=T,e.toArray=e=>r(e)?e:i(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=u,e.uniqueID=()=>y()+y()+y()+y(),e.upperCamelCase=E,e.uuidv4=()=>f()+"-"+g()+"-4"+d()+"-"+g()+"-"+(f()+g()),e}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/utils.js",
3
- "version": "0.2.16",
3
+ "version": "0.2.17",
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",