@andrewcaires/utils.js 0.2.30 → 0.3.0

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.30
2
+ * @andrewcaires/utils.js v0.3.0
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2024 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- "use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),l=t=>"object"==i(t),u=t=>l(t)&&t.constructor===Object,p=t=>"string"==i(t),c=t=>void 0===t,m=t=>("0"+t).slice(-2),v=t=>parseInt(t,16)||0,d=(e,s)=>{return(r=v(e)+s,t(r,0,255)).toString(16);var r},x=t=>Math.random().toString(16).slice(t),g=()=>x(-3),f=()=>x(-4),y=()=>f()+f(),_=()=>y()+y(),b=t=>("0"+t.toString()).slice(-2),w={"%y":t=>b(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>b(t.getMonth()+1),"%d":t=>b(t.getDate()),"%H":t=>b(t.getHours()),"%M":t=>b(t.getMinutes()),"%S":t=>b(t.getSeconds())},k=36e5,S=24*k,C={ms:1,s:1e3,m:6e4,h:k,d:S,w:7*S,y:315576e5},j=["y","d","h","m","s","ms"];function O(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{o(r.next(t))}catch(t){n(t)}}function h(t){try{o(r.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}o((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const A=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},I=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},M=()=>{},E="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",L=(t,e,s)=>{let r="";for(let i=0;i<t.length;i++){const n=E.indexOf(t[i]),a=E.indexOf(e[i%e.length]);r+=-1===n||-1===a?t[i]:E[(s?n-a+62:n+a)%62]}return r},T=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),N=t=>T(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),U=N,F=/&(amp|apos|lt|gt|nbsp|quot);/g,q={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},D=t=>/^[\d]$/.test(t),H=t=>/^[A-Za-z]$/.test(t),P=t=>/^[^\w]$/.test(t),B=t=>s(t.split("")).join("");class R{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new R).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new R).lower()}make(t){let s="";this.hasSpace&&(s+=this._space),this.hasNumbers&&(s+=this._numbers),this.hasSymbols&&(s+=this._symbols),this.hasUpper&&!this.hasChars&&(s+=this._upper),this.hasLower&&!this.hasChars&&(s+=this._lower),this.hasChars&&(s+=this._chars);let r="";if(t&&s.length){s=B(s);for(let i=0;i<t;i++)r+=s.charAt(e(0,s.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new R).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new R).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new R).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new R).upper()}}const Y=t=>t.split("").reverse().join(""),z=t=>T(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Z=t=>(t.maskIndex--,""),$={"#":({value:t})=>t,0:t=>D(t.value)?t.value:Z(t),9:t=>D(t.value)?t.value:Z(t),A:t=>H(t.value)?t.value:Z(t),Z:t=>H(t.value)?t.value:Z(t),L:t=>H(t.value)?t.value.toLowerCase():Z(t),U:t=>H(t.value)?t.value.toUpperCase():Z(t),S:t=>P(t.value)?t.value:Z(t)},J=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>$[t.key]?$[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+J(t)},V=(t,e)=>J({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),W=V,K=(t,e)=>Y(W(Y(t),Y(e))),G=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Q=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(u(t)&&u(s))for(const e in s)u(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Q(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Q(t,...e)};class X{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=h(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=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return O(this,void 0,void 0,(function*(){return!p((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 O(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 tt extends X{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(n(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}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 et extends X{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:h(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class st extends X{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>o(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:h(t)?parseFloat(t):t})))}}class rt extends X{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?G(e,t):t}}))}}class it extends X{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}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:N(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:V(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:l(t)?JSON.stringify(t):h(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 nt{static array(t){return(new tt).items(t)}static boolean(){return new et}static number(){return new st}static object(t){return(new rt).keys(t)}static string(){return new it}test(t,...e){return!p(nt.validate(t,...e).error)}testAsync(t,...e){return O(this,void 0,void 0,(function*(){return!p((yield nt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return O(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},exports.Parse=class extends nt{},exports.RandomString=R,exports.Validation=nt,exports.ValidationBase=X,exports.allowed=G,exports.cache=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},exports.camelCase=U,exports.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},exports.clamp=t,exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>w[e]?w[e](t):e.slice(1))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(F,((t,e)=>q[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>G(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?A(t,e,s):l(t)?I(t,e,s):null,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>O(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(M)})),exports.forEachIndex=A,exports.forEachKey=I,exports.forEachSeries=(t,e,s)=>O(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(M)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hash=t=>(t=L(E,t,!1),{generate:e=>L(e,t,!1),reverse:e=>L(e,t,!0)}),exports.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>v(t))))||[]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=D,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=H,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=u,exports.isString=p,exports.isSymbol=P,exports.isUndefined=c,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>O(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=N,exports.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},exports.mask=V,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),K(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=K,exports.maskTest=(t,e)=>t.length==W(t,e).length,exports.merge=Q,exports.msToString=t=>{const e=[];return j.forEach((s=>{const r=C[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=M,exports.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*C[s[2]]||0;return r},exports.parseSize=t=>{if(o(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"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.randomElement=t=>t.length?t[e(0,t.length-1)]:null,exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=(t,e,s)=>"#"+m(t.toString(16))+m(e.toString(16))+m(s.toString(16)),exports.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>m(d(t,e)))),exports.shuffleArray=s,exports.shuffleString=B,exports.simpleID=()=>g()+f(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=Y,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>_()+_()+_()+_(),exports.upperCamelCase=z,exports.uuidv4=()=>y()+"-"+f()+"-4"+g()+"-"+f()+"-"+(y()+f());
7
+ "use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),u=t=>"object"==i(t),l=t=>u(t)&&t.constructor===Object,p=t=>"string"==i(t),c=t=>void 0===t,m=t=>("0"+t).slice(-2),d=t=>parseInt(t,16)||0,v=(e,s)=>{return(r=d(e)+s,t(r,0,255)).toString(16);var r};class x{constructor(t){this.key=this.shuffle(x.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[x.dictionary.indexOf(t[s]),x.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return x.dictionary[(s?t-e+x.dictionary.length:t+e)%x.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}x.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const y=t=>Math.random().toString(16).slice(t),f=()=>y(-3),g=()=>y(-4),_=()=>g()+g(),b=()=>_()+_(),w=t=>("0"+t.toString()).slice(-2),k={"%y":t=>w(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>w(t.getMonth()+1),"%d":t=>w(t.getDate()),"%H":t=>w(t.getHours()),"%M":t=>w(t.getMinutes()),"%S":t=>w(t.getSeconds())},S=36e5,C=24*S,j={ms:1,s:1e3,m:6e4,h:S,d:C,w:7*C,y:315576e5},O=["y","d","h","m","s","ms"];function A(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{o(r.next(t))}catch(t){n(t)}}function h(t){try{o(r.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}o((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const I=()=>{},M=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),L=t=>M(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),E=L,T=/&(amp|apos|lt|gt|nbsp|quot);/g,N={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},U=t=>/^[\d]$/.test(t),F=t=>/^[A-Za-z]$/.test(t),q=t=>/^[^\w]$/.test(t),H=t=>s(t.split("")).join("");class D{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new D).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new D).lower()}make(t){let s="";this.hasSpace&&(s+=this._space),this.hasNumbers&&(s+=this._numbers),this.hasSymbols&&(s+=this._symbols),this.hasUpper&&!this.hasChars&&(s+=this._upper),this.hasLower&&!this.hasChars&&(s+=this._lower),this.hasChars&&(s+=this._chars);let r="";if(t&&s.length){s=H(s);for(let i=0;i<t;i++)r+=s.charAt(e(0,s.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new D).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new D).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new D).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new D).upper()}}const B=t=>t.split("").reverse().join(""),P=t=>M(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),R=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),Y={"#":t=>t.value,0:t=>R(t,U),9:t=>R(t,U),A:t=>R(t,F),Z:t=>R(t,F),L:t=>R(t,F).toLowerCase(),U:t=>R(t,F).toUpperCase(),S:t=>R(t,q)},z=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>Y[t.key]?Y[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+z(t)},Z=(t,e)=>z({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),$=(t,e)=>B(Z(B(t),B(e))),J=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),V=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(l(t)&&l(s))for(const e in s)l(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=V(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return V(t,...e)};class W{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=h(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=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return A(this,void 0,void 0,(function*(){return!p((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 A(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 G extends W{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(n(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}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 K extends W{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:h(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Q extends W{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>o(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:h(t)?parseFloat(t):t})))}}class X extends W{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(u(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!u(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?J(e,t):t}}))}}class tt extends W{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}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:L(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:Z(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:u(t)?JSON.stringify(t):h(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:P(t)})))}}class et{static array(t){return(new G).items(t)}static boolean(){return new K}static number(){return new Q}static object(t){return(new X).keys(t)}static string(){return new tt}test(t,...e){return!p(et.validate(t,...e).error)}testAsync(t,...e){return A(this,void 0,void 0,(function*(){return!p((yield et.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return A(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},exports.Hash=x,exports.RandomString=D,exports.Validation=et,exports.ValidationBase=W,exports.allowed=J,exports.cache=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},exports.camelCase=E,exports.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},exports.clamp=t,exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>k[e]?k[e](t):e.slice(1))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(T,((t,e)=>N[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>J(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>A(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(I)})),exports.forEachSeries=(t,e,s)=>A(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(I)})),exports.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>d(t))))||[]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=U,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=F,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=u,exports.isPlainObject=l,exports.isString=p,exports.isSymbol=q,exports.isUndefined=c,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>A(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=L,exports.mask=Z,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),$(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=$,exports.merge=V,exports.msToString=t=>{const e=[];return O.forEach((s=>{const r=j[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=I,exports.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*j[s[2]]||0;return r},exports.parseSize=t=>{if(o(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"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.randomElement=t=>t.length?t[e(0,t.length-1)]:null,exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=(t,e,s)=>"#"+m(t.toString(16))+m(e.toString(16))+m(s.toString(16)),exports.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>m(v(t,e)))),exports.shuffleArray=s,exports.shuffleString=H,exports.simpleID=()=>f()+g(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=B,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>b()+b()+b()+b(),exports.upperCamelCase=P,exports.uuidv4=()=>_()+"-"+g()+"-4"+f()+"-"+g()+"-"+(_()+g());
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.30
2
+ * @andrewcaires/utils.js v0.3.0
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2024 Andrew Caires
5
5
  * @license: MIT
@@ -61,14 +61,28 @@ declare const isUndefined: (test: any) => test is undefined;
61
61
  declare const type: (test: any) => string;
62
62
 
63
63
  type CacheCallback = (key: any) => TypeObjectAny;
64
+
64
65
  declare const cache: () => CacheCallback;
65
66
 
66
67
  declare const hexToRgb: (color: string) => Array<number>;
67
68
  declare const rgbToHex: (r: number, g: number, b: number) => string;
68
69
  declare const shadeColor: (color: string, percent: number) => string;
69
70
 
71
+ declare class Hash {
72
+ private static readonly dictionary;
73
+ private key;
74
+ constructor(key: string);
75
+ encrypt(text: string): string;
76
+ decrypt(text: string): string;
77
+ private index;
78
+ private result;
79
+ private shuffle;
80
+ }
81
+
70
82
  declare const simpleID: () => string;
83
+
71
84
  declare const uniqueID: () => string;
85
+
72
86
  declare const uuidv4: () => string;
73
87
 
74
88
  declare const dateFormat: (date: Date, format: string) => string;
@@ -108,25 +122,13 @@ interface ILoopCallbacks {
108
122
 
109
123
  declare const loop: (callback: TypeCallbackFunction, ms: number) => ILoopCallbacks;
110
124
 
111
- type EachCallback = TypeCallback<any, string | number, any, any>;
112
- type LoopCallback = (index: number, count: number) => any;
113
- declare const each: (object: any, callback: EachCallback, thisArg?: any) => any;
114
- declare const forEachIndex: <T>(array: Array<T>, callback: TypeCallbackArray<T>, thisArg?: any) => Array<T>;
115
- declare const forEachKey: <T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<T>;
116
- declare const forLoop: (count: number, callback: LoopCallback, thisArg?: any) => number;
117
- declare const map: <T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<T>;
118
125
  declare const noop: () => void;
119
126
 
120
- interface IHash {
121
- generate: (str: string) => string;
122
- reverse: (hash: string) => string;
123
- }
124
- declare const hash: (key: string) => IHash;
125
-
126
127
  declare const mask: (mask: string, text: string) => string;
127
- declare const maskMoney: (mask: string, text: string, decimal: number) => string;
128
- declare const maskTest: (mask: string, text: string) => boolean;
129
- declare const maskReverse: (mask: string, text: string) => string;
128
+
129
+ declare const maskMoney: (value: string, text: string, decimal: number) => string;
130
+
131
+ declare const maskReverse: (value: string, text: string) => string;
130
132
 
131
133
  declare const clamp: (value: number, min: number, max: number) => number;
132
134
 
@@ -146,8 +148,6 @@ declare const forEachSeries: <T>(array: Array<T>, callback: ForEachCallback<T>,
146
148
 
147
149
  declare const sleep: (ms: number) => Promise<void>;
148
150
 
149
- declare const parseSize: (size: number | string) => string;
150
-
151
151
  declare const camelCase: (str: string) => string;
152
152
 
153
153
  declare const cutText: (str: string, length: number) => string;
@@ -166,6 +166,8 @@ declare const isSymbol: (str: string) => boolean;
166
166
 
167
167
  declare const lowerCamelCase: (str: string) => string;
168
168
 
169
+ declare const parseSize: (size: number | string) => string;
170
+
169
171
  declare const randomCase: (str: string) => string;
170
172
 
171
173
  declare class RandomString {
@@ -207,8 +209,8 @@ interface IValidationResult {
207
209
  error?: string;
208
210
  value?: any;
209
211
  }
212
+
210
213
  type ValidationCallback = (value: any) => IValidationResult;
211
- type ValidationCallbackAsync = (value: any) => Promise<IValidationResult>;
212
214
 
213
215
  declare class ValidationBase<T = any> {
214
216
  name: string;
@@ -305,7 +307,6 @@ declare class Validation {
305
307
  static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<IValidationResult>;
306
308
  }
307
309
 
308
- declare class Parse extends Validation {
309
- }
310
+ type ValidationCallbackAsync = (value: any) => Promise<IValidationResult>;
310
311
 
311
- export { type CacheCallback, type EachCallback, type EventCallback, EventEmitter, type EventList, type ForEachCallback, type IDebounceCallbacks, type IEventDisposable, type IHash, type ILoopCallbacks, type IValidationResult, type LoopCallback, Parse, RandomString, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeObject, type TypeObjectAny, type TypeObjectFunction, type TypeObjectNumber, type TypeObjectString, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hash, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
312
+ export { type CacheCallback, type EventCallback, EventEmitter, type EventList, type ForEachCallback, Hash, type IDebounceCallbacks, type IEventDisposable, type ILoopCallbacks, type IValidationResult, RandomString, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeObject, type TypeObjectAny, type TypeObjectFunction, type TypeObjectNumber, type TypeObjectString, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, denied, encodeHTML, forEachAsync, forEachSeries, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, mask, maskMoney, maskReverse, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, shadeColor, shuffleArray, shuffleString, 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.30
2
+ * @andrewcaires/utils.js v0.3.0
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2024 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- const t=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,s,r)=>t+(s-t)*e(r,0,1),r=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),i=t=>t.length?t[r(0,t.length-1)]:null,n=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),h=t=>{for(let e=t.length-1;e>0;e--){const s=r(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},a={},u=t=>null==t?"null":a[a.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>a["[object "+t+"]"]=t.toLowerCase()));const l=t=>"array"==u(t),o=t=>"boolean"==u(t),c=t=>"null"!=u(t),p=t=>"number"==u(t),v=t=>p(t)&&!!(t%1),m=t=>"function"==u(t),d=t=>p(t)&&!(t%1),g=t=>null===t,f=t=>"object"==u(t),y=t=>f(t)&&t.constructor===Object,_=t=>"string"==u(t),x=t=>void 0===t,b=t=>l(t)?t:c(t)?[t]:[],w=t=>"true"===t||"1"===t,k=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},S=t=>("0"+t).slice(-2),j=t=>parseInt(t,16)||0,C=(t,s)=>{return(r=j(t)+s,e(r,0,255)).toString(16);var r},O=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>j(t))))||[]},A=(t,e,s)=>"#"+S(t.toString(16))+S(e.toString(16))+S(s.toString(16)),I=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>S(C(t,e)))),M=t=>Math.random().toString(16).slice(t),L=()=>M(-3),U=()=>M(-4),N=()=>U()+U(),q=()=>N()+N(),E=()=>L()+U(),F=()=>q()+q()+q()+q(),T=()=>N()+"-"+U()+"-4"+L()+"-"+U()+"-"+(N()+U()),Y=t=>("0"+t.toString()).slice(-2),H={"%y":t=>Y(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>Y(t.getMonth()+1),"%d":t=>Y(t.getDate()),"%H":t=>Y(t.getHours()),"%M":t=>Y(t.getMinutes()),"%S":t=>Y(t.getSeconds())},P=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>H[e]?H[e](t):e.slice(1))),Z=36e5,$=24*Z,z={ms:1,s:1e3,m:6e4,h:Z,d:$,w:7*$,y:315576e5},D=["y","d","h","m","s","ms"],J=t=>{const e=[];return D.forEach((s=>{const r=z[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},W=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*z[s[2]]||0;return r},B=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class G{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}}function K(t,e,s,r){return new(s||(s=Promise))((function(i,n){function h(t){try{u(r.next(t))}catch(t){n(t)}}function a(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const Q=(t,e)=>{let s=!1;const r=()=>K(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},R=(t,e,s)=>l(t)?V(t,e,s):f(t)?X(t,e,s):null,V=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},X=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},tt=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},et=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},st=()=>{},rt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",it=(t,e,s)=>{let r="";for(let i=0;i<t.length;i++){const n=rt.indexOf(t[i]),h=rt.indexOf(e[i%e.length]);r+=-1===n||-1===h?t[i]:rt[(s?n-h+62:n+h)%62]}return r},nt=t=>(t=it(rt,t,!1),{generate:e=>it(e,t,!1),reverse:e=>it(e,t,!0)}),ht=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),at=t=>ht(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),ut=at,lt=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},ot=/&(amp|apos|lt|gt|nbsp|quot);/g,ct={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},pt=t=>t.replace(ot,((t,e)=>ct[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),vt=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),mt=t=>/^[\w]$/.test(t),dt=t=>/^[\d]$/.test(t),gt=t=>/^[A-Za-z]$/.test(t),ft=t=>/^[^\w]$/.test(t),yt=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),_t=t=>h(t.split("")).join("");class xt{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new xt).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new xt).lower()}make(t){let e="";this.hasSpace&&(e+=this._space),this.hasNumbers&&(e+=this._numbers),this.hasSymbols&&(e+=this._symbols),this.hasUpper&&!this.hasChars&&(e+=this._upper),this.hasLower&&!this.hasChars&&(e+=this._lower),this.hasChars&&(e+=this._chars);let s="";if(t&&e.length){e=_t(e);for(let i=0;i<t;i++)s+=e.charAt(r(0,e.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new xt).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new xt).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new xt).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new xt).upper()}}const bt=t=>t.split("").reverse().join(""),wt=t=>ht(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),kt=t=>(t.maskIndex--,""),St={"#":({value:t})=>t,0:t=>dt(t.value)?t.value:kt(t),9:t=>dt(t.value)?t.value:kt(t),A:t=>gt(t.value)?t.value:kt(t),Z:t=>gt(t.value)?t.value:kt(t),L:t=>gt(t.value)?t.value.toLowerCase():kt(t),U:t=>gt(t.value)?t.value.toUpperCase():kt(t),S:t=>ft(t.value)?t.value:kt(t)},jt=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:h}=t;return x(n)||x(h)?"":(t=>St[t.key]?St[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+jt(t)},Ct=(t,e)=>jt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),Ot=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),Mt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),At=Ct,It=(t,e)=>t.length==At(t,e).length,Mt=(t,e)=>bt(At(bt(t),bt(e))),Lt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Ut=(t,e)=>Lt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Nt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(y(t)&&y(s))for(const e in s)y(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Nt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Nt(t,...e)},qt=(t,e,s)=>K(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(st)})),Et=(t,e,s)=>K(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(st)})),Ft=t=>new Promise((e=>setTimeout(e,t))),Tt=t=>{if(p(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 Yt{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=c(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=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!_(this.validate(t).error)}testAsync(t){return K(this,void 0,void 0,(function*(){return!_((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 K(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 Ht extends Yt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(l(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}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 Pt extends Yt{constructor(){super(),this._type="boolean",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:c(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Zt extends Yt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>p(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:c(t)?parseFloat(t):t})))}}class $t extends Yt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(f(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),c(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!f(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Lt(e,t):t}}))}}class zt extends Yt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>_(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}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:at(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:Ct(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:f(t)?JSON.stringify(t):c(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:wt(t)})))}}class Dt{static array(t){return(new Ht).items(t)}static boolean(){return new Pt}static number(){return new Zt}static object(t){return(new $t).keys(t)}static string(){return new zt}test(t,...e){return!_(Dt.validate(t,...e).error)}testAsync(t,...e){return K(this,void 0,void 0,(function*(){return!_((yield Dt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return K(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}class Jt extends Dt{}export{G as EventEmitter,Jt as Parse,xt as RandomString,Dt as Validation,Yt as ValidationBase,Lt as allowed,k as cache,ut as camelCase,t as chunkArray,e as clamp,lt as cutText,P as dateFormat,B as debounce,pt as decodeHTML,Ut as denied,R as each,vt as encodeHTML,qt as forEachAsync,V as forEachIndex,X as forEachKey,Et as forEachSeries,tt as forLoop,nt as hash,O as hexToRgb,l as isArray,o as isBoolean,mt as isCharacter,c as isDef,dt as isDigit,v as isFloat,m as isFunction,d as isInteger,gt as isLetter,g as isNull,p as isNumber,f as isObject,y as isPlainObject,_ as isString,ft as isSymbol,x as isUndefined,s as lerp,Q as loop,at as lowerCamelCase,et as map,Ct as mask,Ot as maskMoney,Mt as maskReverse,It as maskTest,Nt as merge,J as msToString,st as noop,W as parseMs,Tt as parseSize,r as random,yt as randomCase,i as randomElement,n as rangeArray,A as rgbToHex,I as shadeColor,h as shuffleArray,_t as shuffleString,E as simpleID,Ft as sleep,bt as stringReverse,b as toArray,w as toBool,u as type,F as uniqueID,wt as upperCamelCase,T as uuidv4};
7
+ const t=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,s,r)=>t+(s-t)*e(r,0,1),r=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),i=t=>t.length?t[r(0,t.length-1)]:null,n=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),h=t=>{for(let e=t.length-1;e>0;e--){const s=r(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},a={},u=t=>null==t?"null":a[a.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>a["[object "+t+"]"]=t.toLowerCase()));const l=t=>"array"==u(t),o=t=>"boolean"==u(t),c=t=>"null"!=u(t),p=t=>"number"==u(t),m=t=>p(t)&&!!(t%1),v=t=>"function"==u(t),d=t=>p(t)&&!(t%1),f=t=>null===t,y=t=>"object"==u(t),g=t=>y(t)&&t.constructor===Object,_=t=>"string"==u(t),x=t=>void 0===t,b=t=>l(t)?t:c(t)?[t]:[],w=t=>"true"===t||"1"===t,k=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},S=t=>("0"+t).slice(-2),C=t=>parseInt(t,16)||0,j=(t,s)=>{return(r=C(t)+s,e(r,0,255)).toString(16);var r},O=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>C(t))))||[]},A=(t,e,s)=>"#"+S(t.toString(16))+S(e.toString(16))+S(s.toString(16)),I=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>S(j(t,e))));class M{constructor(t){this.key=this.shuffle(M.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[M.dictionary.indexOf(t[s]),M.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return M.dictionary[(s?t-e+M.dictionary.length:t+e)%M.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,h]=this.index(t,e,i);r+=-1===n||-1===h?t[i]:this.result(n,h,s)}return r}}M.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const L=t=>Math.random().toString(16).slice(t),U=()=>L(-3),N=()=>L(-4),q=()=>N()+N(),E=()=>q()+q(),F=()=>U()+N(),T=()=>E()+E()+E()+E(),Y=()=>q()+"-"+N()+"-4"+U()+"-"+N()+"-"+(q()+N()),H=t=>("0"+t.toString()).slice(-2),P={"%y":t=>H(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>H(t.getMonth()+1),"%d":t=>H(t.getDate()),"%H":t=>H(t.getHours()),"%M":t=>H(t.getMinutes()),"%S":t=>H(t.getSeconds())},Z=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>P[e]?P[e](t):e.slice(1))),$=36e5,z=24*$,D={ms:1,s:1e3,m:6e4,h:$,d:z,w:7*z,y:315576e5},J=["y","d","h","m","s","ms"],W=t=>{const e=[];return J.forEach((s=>{const r=D[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},B=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*D[s[2]]||0;return r},G=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class K{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}}function Q(t,e,s,r){return new(s||(s=Promise))((function(i,n){function h(t){try{u(r.next(t))}catch(t){n(t)}}function a(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const R=(t,e)=>{let s=!1;const r=()=>Q(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},V=()=>{},X=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),tt=t=>X(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),et=tt,st=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},rt=/&(amp|apos|lt|gt|nbsp|quot);/g,it={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},nt=t=>t.replace(rt,((t,e)=>it[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),ht=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),at=t=>/^[\w]$/.test(t),ut=t=>/^[\d]$/.test(t),lt=t=>/^[A-Za-z]$/.test(t),ot=t=>/^[^\w]$/.test(t),ct=t=>{if(p(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"},pt=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),mt=t=>h(t.split("")).join("");class vt{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new vt).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new vt).lower()}make(t){let e="";this.hasSpace&&(e+=this._space),this.hasNumbers&&(e+=this._numbers),this.hasSymbols&&(e+=this._symbols),this.hasUpper&&!this.hasChars&&(e+=this._upper),this.hasLower&&!this.hasChars&&(e+=this._lower),this.hasChars&&(e+=this._chars);let s="";if(t&&e.length){e=mt(e);for(let i=0;i<t;i++)s+=e.charAt(r(0,e.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new vt).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new vt).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new vt).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new vt).upper()}}const dt=t=>t.split("").reverse().join(""),ft=t=>X(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),yt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),gt={"#":t=>t.value,0:t=>yt(t,ut),9:t=>yt(t,ut),A:t=>yt(t,lt),Z:t=>yt(t,lt),L:t=>yt(t,lt).toLowerCase(),U:t=>yt(t,lt).toUpperCase(),S:t=>yt(t,ot)},_t=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:h}=t;return x(n)||x(h)?"":(t=>gt[t.key]?gt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+_t(t)},xt=(t,e)=>_t({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),bt=(t,e)=>dt(xt(dt(t),dt(e))),wt=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),bt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),kt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),St=(t,e)=>kt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Ct=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(g(t)&&g(s))for(const e in s)g(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Ct(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Ct(t,...e)},jt=(t,e,s)=>Q(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(V)})),Ot=(t,e,s)=>Q(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(V)})),At=t=>new Promise((e=>setTimeout(e,t)));class It{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=c(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=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!_(this.validate(t).error)}testAsync(t){return Q(this,void 0,void 0,(function*(){return!_((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 Q(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 Mt extends It{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(l(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}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 Lt extends It{constructor(){super(),this._type="boolean",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:c(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Ut extends It{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>p(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:c(t)?parseFloat(t):t})))}}class Nt extends It{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(y(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),c(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!y(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 qt extends It{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>_(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}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:tt(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:xt(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:y(t)?JSON.stringify(t):c(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:ft(t)})))}}class Et{static array(t){return(new Mt).items(t)}static boolean(){return new Lt}static number(){return new Ut}static object(t){return(new Nt).keys(t)}static string(){return new qt}test(t,...e){return!_(Et.validate(t,...e).error)}testAsync(t,...e){return Q(this,void 0,void 0,(function*(){return!_((yield Et.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return Q(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}export{K as EventEmitter,M as Hash,vt as RandomString,Et as Validation,It as ValidationBase,kt as allowed,k as cache,et as camelCase,t as chunkArray,e as clamp,st as cutText,Z as dateFormat,G as debounce,nt as decodeHTML,St as denied,ht as encodeHTML,jt as forEachAsync,Ot as forEachSeries,O as hexToRgb,l as isArray,o as isBoolean,at as isCharacter,c as isDef,ut as isDigit,m as isFloat,v as isFunction,d as isInteger,lt as isLetter,f as isNull,p as isNumber,y as isObject,g as isPlainObject,_ as isString,ot as isSymbol,x as isUndefined,s as lerp,R as loop,tt as lowerCamelCase,xt as mask,wt as maskMoney,bt as maskReverse,Ct as merge,W as msToString,V as noop,B as parseMs,ct as parseSize,r as random,pt as randomCase,i as randomElement,n as rangeArray,A as rgbToHex,I as shadeColor,h as shuffleArray,mt as shuffleString,F as simpleID,At as sleep,dt as stringReverse,b as toArray,w as toBool,u as type,T as uniqueID,ft as upperCamelCase,Y as uuidv4};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.30
2
+ * @andrewcaires/utils.js v0.3.0
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2024 Andrew Caires
5
5
  * @license: MIT
6
6
  */
7
- var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),l=t=>"null"!=n(t),u=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=t=>"string"==n(t),m=t=>void 0===t,v=t=>("0"+t).slice(-2),d=t=>parseInt(t,16)||0,g=(t,s)=>{return(r=d(t)+s,e(r,0,255)).toString(16);var r},f=t=>Math.random().toString(16).slice(t),y=()=>f(-3),_=()=>f(-4),x=()=>_()+_(),b=()=>x()+x(),w=t=>("0"+t.toString()).slice(-2),k={"%y":t=>w(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>w(t.getMonth()+1),"%d":t=>w(t.getDate()),"%H":t=>w(t.getHours()),"%M":t=>w(t.getMinutes()),"%S":t=>w(t.getSeconds())},S=36e5,C=24*S,j={ms:1,s:1e3,m:6e4,h:S,d:C,w:7*C,y:315576e5},O=["y","d","h","m","s","ms"];function A(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{l(r.next(t))}catch(t){n(t)}}function h(t){try{l(r.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}l((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const I=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},M=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},E=()=>{},L="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",T=(t,e,s)=>{let r="";for(let i=0;i<t.length;i++){const n=L.indexOf(t[i]),a=L.indexOf(e[i%e.length]);r+=-1===n||-1===a?t[i]:L[(s?n-a+62:n+a)%62]}return r},U=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),N=t=>U(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),F=N,q=/&(amp|apos|lt|gt|nbsp|quot);/g,D={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},H=t=>/^[\d]$/.test(t),P=t=>/^[A-Za-z]$/.test(t),B=t=>/^[^\w]$/.test(t),R=t=>r(t.split("")).join("");class Y{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new Y).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Y).lower()}make(t){let e="";this.hasSpace&&(e+=this._space),this.hasNumbers&&(e+=this._numbers),this.hasSymbols&&(e+=this._symbols),this.hasUpper&&!this.hasChars&&(e+=this._upper),this.hasLower&&!this.hasChars&&(e+=this._lower),this.hasChars&&(e+=this._chars);let r="";if(t&&e.length){e=R(e);for(let i=0;i<t;i++)r+=e.charAt(s(0,e.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new Y).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Y).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Y).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Y).upper()}}const z=t=>t.split("").reverse().join(""),J=t=>U(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Z=t=>(t.maskIndex--,""),$={"#":({value:t})=>t,0:t=>H(t.value)?t.value:Z(t),9:t=>H(t.value)?t.value:Z(t),A:t=>P(t.value)?t.value:Z(t),Z:t=>P(t.value)?t.value:Z(t),L:t=>P(t.value)?t.value.toLowerCase():Z(t),U:t=>P(t.value)?t.value.toUpperCase():Z(t),S:t=>B(t.value)?t.value:Z(t)},V=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return m(n)||m(a)?"":(t=>$[t.key]?$[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+V(t)},W=(t,e)=>V({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),K=W,G=(t,e)=>z(K(z(t),z(e))),Q=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),X=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=X(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return X(t,...e)};class tt{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=l(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=>l(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return A(this,void 0,void 0,(function*(){return!p((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 A(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 et extends tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(a(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}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 st extends tt{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:l(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class rt extends tt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>u(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:l(t)?parseFloat(t):t})))}}class it extends tt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),l(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Q(e,t):t}}))}}class nt extends tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}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:N(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:W(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:o(t)?JSON.stringify(t):l(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:J(t)})))}}class at{static array(t){return(new et).items(t)}static boolean(){return new st}static number(){return new rt}static object(t){return(new it).keys(t)}static string(){return new nt}test(t,...e){return!p(at.validate(t,...e).error)}testAsync(t,...e){return A(this,void 0,void 0,(function*(){return!p((yield at.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return A(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},t.Parse=class extends at{},t.RandomString=Y,t.Validation=at,t.ValidationBase=tt,t.allowed=Q,t.cache=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},t.camelCase=F,t.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},t.clamp=e,t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>k[e]?k[e](t):e.slice(1))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(q,((t,e)=>D[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>Q(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?I(t,e,s):o(t)?M(t,e,s):null,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>A(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(E)})),t.forEachIndex=I,t.forEachKey=M,t.forEachSeries=(t,e,s)=>A(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(E)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hash=t=>(t=T(L,t,!1),{generate:e=>T(e,t,!1),reverse:e=>T(e,t,!0)}),t.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>d(t))))||[]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=l,t.isDigit=H,t.isFloat=t=>u(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>u(t)&&!(t%1),t.isLetter=P,t.isNull=t=>null===t,t.isNumber=u,t.isObject=o,t.isPlainObject=c,t.isString=p,t.isSymbol=B,t.isUndefined=m,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>A(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=N,t.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},t.mask=W,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),G(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=G,t.maskTest=(t,e)=>t.length==K(t,e).length,t.merge=X,t.msToString=t=>{const e=[];return O.forEach((s=>{const r=j[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=E,t.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*j[s[2]]||0;return r},t.parseSize=t=>{if(u(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"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.randomElement=t=>t.length?t[s(0,t.length-1)]:null,t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=(t,e,s)=>"#"+v(t.toString(16))+v(e.toString(16))+v(s.toString(16)),t.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>v(g(t,e)))),t.shuffleArray=r,t.shuffleString=R,t.simpleID=()=>y()+_(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=z,t.toArray=t=>a(t)?t:l(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>b()+b()+b()+b(),t.upperCamelCase=J,t.uuidv4=()=>x()+"-"+_()+"-4"+y()+"-"+_()+"-"+(x()+_()),t}({});
7
+ var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),u=t=>"null"!=n(t),l=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=t=>"string"==n(t),m=t=>void 0===t,d=t=>("0"+t).slice(-2),v=t=>parseInt(t,16)||0,f=(t,s)=>{return(r=v(t)+s,e(r,0,255)).toString(16);var r};class y{constructor(t){this.key=this.shuffle(y.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[y.dictionary.indexOf(t[s]),y.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return y.dictionary[(s?t-e+y.dictionary.length:t+e)%y.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}y.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const g=t=>Math.random().toString(16).slice(t),_=()=>g(-3),x=()=>g(-4),b=()=>x()+x(),w=()=>b()+b(),k=t=>("0"+t.toString()).slice(-2),S={"%y":t=>k(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>k(t.getMonth()+1),"%d":t=>k(t.getDate()),"%H":t=>k(t.getHours()),"%M":t=>k(t.getMinutes()),"%S":t=>k(t.getSeconds())},C=36e5,j=24*C,O={ms:1,s:1e3,m:6e4,h:C,d:j,w:7*j,y:315576e5},A=["y","d","h","m","s","ms"];function I(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{u(r.next(t))}catch(t){n(t)}}function h(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const M=()=>{},L=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),E=t=>L(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),T=E,U=/&(amp|apos|lt|gt|nbsp|quot);/g,N={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},F=t=>/^[\d]$/.test(t),q=t=>/^[A-Za-z]$/.test(t),H=t=>/^[^\w]$/.test(t),D=t=>r(t.split("")).join("");class B{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new B).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new B).lower()}make(t){let e="";this.hasSpace&&(e+=this._space),this.hasNumbers&&(e+=this._numbers),this.hasSymbols&&(e+=this._symbols),this.hasUpper&&!this.hasChars&&(e+=this._upper),this.hasLower&&!this.hasChars&&(e+=this._lower),this.hasChars&&(e+=this._chars);let r="";if(t&&e.length){e=D(e);for(let i=0;i<t;i++)r+=e.charAt(s(0,e.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new B).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new B).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new B).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new B).upper()}}const P=t=>t.split("").reverse().join(""),R=t=>L(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Y=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),z={"#":t=>t.value,0:t=>Y(t,F),9:t=>Y(t,F),A:t=>Y(t,q),Z:t=>Y(t,q),L:t=>Y(t,q).toLowerCase(),U:t=>Y(t,q).toUpperCase(),S:t=>Y(t,H)},J=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return m(n)||m(a)?"":(t=>z[t.key]?z[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+J(t)},Z=(t,e)=>J({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),$=(t,e)=>P(Z(P(t),P(e))),V=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),W=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=W(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return W(t,...e)};class G{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=u(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=>u(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return I(this,void 0,void 0,(function*(){return!p((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 I(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 K extends G{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(a(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class Q extends G{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:u(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class X extends G{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>l(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:u(t)?parseFloat(t):t})))}}class tt extends G{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),u(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?V(e,t):t}}))}}class et extends G{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}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:E(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:Z(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:o(t)?JSON.stringify(t):u(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:R(t)})))}}class st{static array(t){return(new K).items(t)}static boolean(){return new Q}static number(){return new X}static object(t){return(new tt).keys(t)}static string(){return new et}test(t,...e){return!p(st.validate(t,...e).error)}testAsync(t,...e){return I(this,void 0,void 0,(function*(){return!p((yield st.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return I(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},t.Hash=y,t.RandomString=B,t.Validation=st,t.ValidationBase=G,t.allowed=V,t.cache=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},t.camelCase=T,t.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},t.clamp=e,t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>S[e]?S[e](t):e.slice(1))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(U,((t,e)=>N[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>V(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>I(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(M)})),t.forEachSeries=(t,e,s)=>I(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(M)})),t.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>v(t))))||[]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=u,t.isDigit=F,t.isFloat=t=>l(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>l(t)&&!(t%1),t.isLetter=q,t.isNull=t=>null===t,t.isNumber=l,t.isObject=o,t.isPlainObject=c,t.isString=p,t.isSymbol=H,t.isUndefined=m,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>I(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=E,t.mask=Z,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),$(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=$,t.merge=W,t.msToString=t=>{const e=[];return A.forEach((s=>{const r=O[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=M,t.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*O[s[2]]||0;return r},t.parseSize=t=>{if(l(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"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.randomElement=t=>t.length?t[s(0,t.length-1)]:null,t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=(t,e,s)=>"#"+d(t.toString(16))+d(e.toString(16))+d(s.toString(16)),t.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>d(f(t,e)))),t.shuffleArray=r,t.shuffleString=D,t.simpleID=()=>_()+x(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=P,t.toArray=t=>a(t)?t:u(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>w()+w()+w()+w(),t.upperCamelCase=R,t.uuidv4=()=>b()+"-"+x()+"-4"+_()+"-"+x()+"-"+(b()+x()),t}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/utils.js",
3
- "version": "0.2.30",
3
+ "version": "0.3.0",
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",
@@ -20,9 +20,7 @@
20
20
  "dist"
21
21
  ],
22
22
  "scripts": {
23
- "build": "rollup --bundleConfigAsCjs -c ./rollup.config.js",
24
- "dev": "ts-node-dev --respawn --transpile-only ./src/index.ts",
25
- "test": "jest --config ./jest.config.ts"
23
+ "build": "rollup --bundleConfigAsCjs -c ./rollup.config.js"
26
24
  },
27
25
  "repository": {
28
26
  "type": "git",
@@ -44,20 +42,14 @@
44
42
  },
45
43
  "homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
46
44
  "devDependencies": {
47
- "@jest/globals": "^29.7.0",
48
- "@rollup/plugin-commonjs": "^25.0.8",
45
+ "@rollup/plugin-commonjs": "^26.0.1",
49
46
  "@rollup/plugin-terser": "^0.4.4",
50
47
  "@rollup/plugin-typescript": "^11.1.6",
51
- "@types/jest": "^29.5.12",
52
- "@types/node": "^20.13.0",
53
- "@typescript-eslint/eslint-plugin": "^7.11.0",
54
- "@typescript-eslint/parser": "^7.11.0",
55
- "eslint": "^8.57.0",
56
- "jest": "^29.7.0",
48
+ "@types/node": "^20.14.2",
49
+ "eslint": "^9.5.0",
57
50
  "rollup": "^4.18.0",
58
51
  "rollup-plugin-dts": "^6.1.1",
59
- "ts-jest": "^29.1.4",
60
- "ts-node-dev": "^2.0.0",
61
- "tslib": "^2.6.2"
52
+ "tslib": "^2.6.3",
53
+ "typescript-eslint": "^8.0.0-alpha.30"
62
54
  }
63
- }
55
+ }