@andrewcaires/utils.js 0.3.6 → 0.4.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.3.6
2
+ * @andrewcaires/utils.js v0.4.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),u=t=>"function"==i(t),l=t=>"object"==i(t),p=t=>l(t)&&t.constructor===Object,c=["boolean","number","string"],m=t=>"string"==i(t),d=t=>void 0===t,x=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},v=(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},f=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),g=t=>parseInt(null!=t?t:"0",16)||0,y=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(g))||[]},_=t=>("0"+t.toString(16)).slice(-2),b=(t,e,s)=>"#"+_(t)+_(e)+_(s),w=(e,s)=>{const r=y(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return b(i,n,a)},k=(e,s)=>{const r=y(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return b(i,n,a)},C={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},S=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,j=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1));class O{constructor(t){this.key=this.shuffle(O.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[O.dictionary.indexOf(t[s]),O.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return O.dictionary[(s?t-e+O.dictionary.length:t+e)%O.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}}O.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const M=t=>Math.random().toString(16).slice(t),A=()=>M(-3),I=()=>M(-4),E=()=>I()+I(),T=()=>E()+E(),D=t=>("0"+t.toString()).slice(-2),L={"%y":t=>D(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>D(t.getMonth()+1),"%d":t=>D(t.getDate()),"%H":t=>D(t.getHours()),"%M":t=>D(t.getMinutes()),"%S":t=>D(t.getSeconds())},N=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>L[t]?L[t](e):t.slice(1)))),U=36e5,F=24*U,H={ms:1,s:1e3,m:6e4,h:U,d:F,w:7*F,y:315576e5},q=["y","d","h","m","s","ms"];function R(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 P=()=>{},B=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),Y=t=>B(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),z=Y,Z=/&(amp|apos|lt|gt|nbsp|quot);/g,$={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},J=t=>/^[\d]$/.test(t),V=t=>/^[A-Za-z]$/.test(t),W=t=>/^[^\w]$/.test(t),K=t=>s(t.split("")).join("");class G{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 G).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new G).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=K(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 G).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new G).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new G).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new G).upper()}}const Q=t=>t.split("").reverse().join(""),X=t=>B(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),tt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),et={"#":t=>t.value,0:t=>tt(t,J),9:t=>tt(t,J),A:t=>tt(t,V),Z:t=>tt(t,V),L:t=>tt(t,V).toLowerCase(),U:t=>tt(t,V).toUpperCase(),S:t=>tt(t,W)},st=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 d(n)||d(a)?"":(t=>et[t.key]?et[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+st(t)},rt=(t,e)=>st({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),it=(t,e)=>Q(rt(Q(t),Q(e))),nt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),at=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(p(t)&&p(s))for(const e in s)p(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=at(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return at(t,...e)};class ht{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:u(this._value)?this._value():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}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!m(this.validate(t).error)}testAsync(t){return R(this,void 0,void 0,(function*(){return!m((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 R(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 ot extends ht{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 ut extends ht{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 lt extends ht{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:N(this._format,new Date(t))})))}parse(){return this.unshift((t=>(m(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class pt extends ht{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 ct extends ht{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:a,value:o}=n.validate(t[n.name]);a&&e.push(a),h(o)&&(s[r]=o)})),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?nt(e,t):t}}))}}class mt extends ht{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>m(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:Y(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:rt(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:X(t)})))}}class dt{static array(t){return(new ot).items(t)}static boolean(){return new ut}static date(){return new lt}static number(){return new pt}static object(t){return(new ct).keys(t)}static string(){return new mt}test(t,...e){return!m(dt.validate(t,...e).error)}testAsync(t,...e){return R(this,void 0,void 0,(function*(){return!m((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 R(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=O,exports.RandomString=G,exports.Validation=dt,exports.ValidationBase=ht,exports.allowed=nt,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=z,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.colorPalette=(t,e)=>f(null!=e?e:C,(e=>0==e?t:e>0?k(t,e):w(t,1- -1*e))),exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=N,exports.dateRange=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},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(Z,((t,e)=>$[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>nt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?x(t,e,s):l(t)?v(t,e,s):t,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>R(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(P)})),exports.forEachIndex=x,exports.forEachKey=v,exports.forEachSeries=(t,e,s)=>R(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(P)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hexToRgb=y,exports.hslToRgb=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const a=s-.5*r;return[Math.round(255*(n[0]+a)),Math.round(255*(n[1]+a)),Math.round(255*(n[2]+a))]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=J,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=u,exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=V,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=p,exports.isScalar=t=>-1!=c.indexOf(i(t)),exports.isString=m,exports.isSymbol=W,exports.isUndefined=d,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>R(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=Y,exports.map=f,exports.mask=rt,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),it(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=it,exports.maskTest=(t,e)=>t.length==rt(t,e).length,exports.merge=at,exports.msToString=t=>{const e=[];return q.forEach((s=>{const r=H[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=P,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])*H[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=b,exports.rgbToHsl=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),a=Math.max(r,i,n),h=Math.min(r,i,n),o=a-h,u=(h+a)/2;return[60*S(o,a,r,i,n),j(o,u),u]},exports.shadeColor=w,exports.shuffleArray=s,exports.shuffleString=K,exports.simpleID=()=>A()+I(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=Q,exports.tintColor=k,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>T()+T()+T()+T(),exports.upperCamelCase=X,exports.uuidv4=()=>E()+"-"+I()+"-4"+A()+"-"+I()+"-"+(E()+I());
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","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),o=t=>"null"!=i(t),h=t=>"number"==i(t),u=t=>"function"==typeof t,l=t=>"object"==i(t),p=t=>l(t)&&t.constructor===Object,c=["boolean","number","string"],m=t=>"string"==typeof t,d=t=>void 0===t,x=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},v=(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},f=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),g=t=>parseInt(null!=t?t:"0",16)||0,y=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(g))||[]},_=t=>("0"+t.toString(16)).slice(-2),b=(t,e,s)=>"#"+_(t)+_(e)+_(s),w=(e,s)=>{const r=y(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return b(i,n,a)},k=(e,s)=>{const r=y(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return b(i,n,a)},S={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},C=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,j=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1));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,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}M.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const O=t=>Math.random().toString(16).slice(t),A=()=>O(-3),I=()=>O(-4),E=()=>I()+I(),D=()=>E()+E(),T=t=>("0"+t.toString()).slice(-2),L={"%y":t=>T(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>T(t.getMonth()+1),"%d":t=>T(t.getDate()),"%H":t=>T(t.getHours()),"%M":t=>T(t.getMinutes()),"%S":t=>T(t.getSeconds())},F=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>L[t]?L[t](e):t.slice(1)))),N=36e5,U=24*N,H={ms:1,s:1e3,m:6e4,h:N,d:U,w:7*U,y:315576e5},R=["y","d","h","m","s","ms"];function q(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{h(r.next(t))}catch(t){n(t)}}function o(t){try{h(r.throw(t))}catch(t){n(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,o)}h((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const P=()=>{},B=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),Y=t=>B(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),z=Y,Z=/&(amp|apos|lt|gt|nbsp|quot);/g,$={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},J=t=>/^[\d]$/.test(t),V=t=>/^[A-Za-z]$/.test(t),W=t=>/^[^\w]$/.test(t),K=t=>s(t.split("")).join("");class G{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 G).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new G).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=K(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 G).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new G).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new G).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new G).upper()}}const Q=t=>t.split("").reverse().join(""),X=t=>B(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),tt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),et={"#":t=>t.value,0:t=>tt(t,J),9:t=>tt(t,J),A:t=>tt(t,V),Z:t=>tt(t,V),L:t=>tt(t,V).toLowerCase(),U:t=>tt(t,V).toUpperCase(),S:t=>tt(t,W)},st=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 d(n)||d(a)?"":(t=>et[t.key]?et[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+st(t)},rt=(t,e)=>st({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),it=(t,e)=>Q(rt(Q(t),Q(e))),nt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),at=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(p(t)&&p(s))for(const e in s)p(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=at(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return at(t,...e)};class ot{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=o(t);return{break:!e,value:e?t:u(this._value)?this._value():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}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>o(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!m(this.validate(t).error)}testAsync(t){return q(this,void 0,void 0,(function*(){return!m((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 ht extends ot{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 ut extends ot{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:o(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class lt extends ot{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:F(this._format,new Date(t))})))}parse(){return this.unshift((t=>(m(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class pt extends ot{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>h(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:o(t)?parseFloat(t):t})))}}class ct extends ot{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:a,value:h}=n.validate(t[n.name]);a&&e.push(a),o(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(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?nt(e,t):t}}))}}class mt extends ot{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>m(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:Y(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:rt(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):o(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:X(t)})))}}class dt{static array(t){return(new ht).items(t)}static boolean(){return new ut}static date(){return new lt}static number(){return new pt}static object(t){return(new ct).keys(t)}static string(){return new mt}test(t,...e){return!m(dt.validate(t,...e).error)}testAsync(t,...e){return q(this,void 0,void 0,(function*(){return!m((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 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}))}}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=M,exports.RandomString=G,exports.Validation=dt,exports.ValidationBase=ot,exports.allowed=nt,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=z,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.colorPalette=(t,e)=>f(null!=e?e:S,(e=>0==e?t:e>0?k(t,e):w(t,1- -1*e))),exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=F,exports.dateRange=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},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(Z,((t,e)=>$[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>nt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?x(t,e,s):l(t)?v(t,e,s):t,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(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(P)})),exports.forEachIndex=x,exports.forEachKey=v,exports.forEachSeries=(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(P)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hexToRgb=y,exports.hslToRgb=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const a=s-.5*r;return[Math.round(255*(n[0]+a)),Math.round(255*(n[1]+a)),Math.round(255*(n[2]+a))]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDate=t=>"date"==i(t),exports.isDef=o,exports.isDigit=J,exports.isFloat=t=>h(t)&&!!(t%1),exports.isFunction=u,exports.isInteger=t=>h(t)&&!(t%1),exports.isLetter=V,exports.isMap=t=>"map"==i(t),exports.isNull=t=>null===t,exports.isNumber=h,exports.isObject=l,exports.isPlainObject=p,exports.isRegExp=t=>"regexp"==i(t),exports.isScalar=t=>-1!=c.indexOf(i(t)),exports.isSet=t=>"set"==i(t),exports.isSpecialChar=W,exports.isString=m,exports.isSymbol=t=>"symbol"==typeof t,exports.isUndefined=d,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(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}}},exports.lowerCamelCase=Y,exports.map=f,exports.mask=rt,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),it(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=it,exports.maskTest=(t,e)=>t.length==rt(t,e).length,exports.merge=at,exports.msToString=t=>{const e=[];return R.forEach((s=>{const r=H[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=P,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])*H[s[2]]||0;return r},exports.parseSize=t=>{if(h(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=b,exports.rgbToHsl=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),a=Math.max(r,i,n),o=Math.min(r,i,n),h=a-o,u=(o+a)/2;return[60*C(h,a,r,i,n),j(h,u),u]},exports.shadeColor=w,exports.shuffleArray=s,exports.shuffleString=K,exports.simpleID=()=>A()+I(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=Q,exports.tintColor=k,exports.toArray=t=>n(t)?t:o(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>D()+D()+D()+D(),exports.upperCamelCase=X,exports.uuidv4=()=>E()+"-"+I()+"-4"+A()+"-"+I()+"-"+(E()+I());
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.3.6
2
+ * @andrewcaires/utils.js v0.4.0
3
3
  * JavaScript utility library for web and nodejs development
4
4
  * (c) 2024 Andrew Caires
5
5
  * @license: MIT
@@ -20,6 +20,8 @@ declare const isArray: (test: any) => test is Array<any>;
20
20
 
21
21
  declare const isBoolean: (test: any) => test is boolean;
22
22
 
23
+ declare const isDate: (test: any) => test is Date;
24
+
23
25
  declare const isDef: (test: any) => test is any;
24
26
 
25
27
  declare const isFloat: (test: any) => test is number;
@@ -52,6 +54,8 @@ declare const isFunction: (test: any) => test is TypeCallbackFunction;
52
54
 
53
55
  declare const isInteger: (test: any) => test is number;
54
56
 
57
+ declare const isMap: <K = any, V = any>(test: any) => test is Map<K, V>;
58
+
55
59
  declare const isNull: (test: any) => test is null;
56
60
 
57
61
  declare const isNumber: (test: any) => test is number;
@@ -60,10 +64,16 @@ declare const isObject: (test: any) => test is TypeObjectAny;
60
64
 
61
65
  declare const isPlainObject: (test: any) => test is TypeObjectAny;
62
66
 
67
+ declare const isRegExp: (test: any) => test is RegExp;
68
+
63
69
  declare const isScalar: (test: any) => test is TypeScalar;
64
70
 
71
+ declare const isSet: <T = any>(test: any) => test is Set<T>;
72
+
65
73
  declare const isString: (test: any) => test is string;
66
74
 
75
+ declare const isSymbol: (test: any) => test is symbol;
76
+
67
77
  declare const isUndefined: (test: any) => test is undefined;
68
78
 
69
79
  declare const type: (test: any) => string;
@@ -202,7 +212,7 @@ declare const isDigit: (str: string) => boolean;
202
212
 
203
213
  declare const isLetter: (str: string) => boolean;
204
214
 
205
- declare const isSymbol: (str: string) => boolean;
215
+ declare const isSpecialChar: (str: string) => boolean;
206
216
 
207
217
  declare const lowerCamelCase: (str: string) => string;
208
218
 
@@ -359,4 +369,4 @@ declare class Validation {
359
369
 
360
370
  type ValidationCallbackAsync = (value: any) => Promise<IValidationResult>;
361
371
 
362
- export { type CacheCallback, type EachCallback, type EachValue, 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 TypeNullable, type TypeObject, type TypeObjectAny, type TypeObjectBoolean, type TypeObjectFunction, type TypeObjectNullable, type TypeObjectNumber, type TypeObjectString, type TypeScalar, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, colorPalette, cutText, dateFormat, dateRange, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hexToRgb, hslToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isScalar, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, rgbToHsl, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, tintColor, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
372
+ export { type CacheCallback, type EachCallback, type EachValue, 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 TypeNullable, type TypeObject, type TypeObjectAny, type TypeObjectBoolean, type TypeObjectFunction, type TypeObjectNullable, type TypeObjectNumber, type TypeObjectString, type TypeScalar, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, colorPalette, cutText, dateFormat, dateRange, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hexToRgb, hslToRgb, isArray, isBoolean, isCharacter, isDate, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isMap, isNull, isNumber, isObject, isPlainObject, isRegExp, isScalar, isSet, isSpecialChar, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, rgbToHsl, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, tintColor, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.3.6
2
+ * @andrewcaires/utils.js v0.4.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),m=t=>p(t)&&!!(t%1),d=t=>"function"==u(t),v=t=>p(t)&&!(t%1),f=t=>null===t,y=t=>"object"==u(t),g=t=>y(t)&&t.constructor===Object,_=["boolean","number","string"],x=t=>-1!=_.indexOf(u(t)),b=t=>"string"==u(t),w=t=>void 0===t,k=t=>l(t)?t:c(t)?[t]:[],j=t=>"true"===t||"1"===t,S=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},C=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},O=(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=(t,e,s)=>l(t)?C(t,e,s):y(t)?O(t,e,s):t,A=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},I=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),D=t=>parseInt(null!=t?t:"0",16)||0,L=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(D))||[]},U=t=>("0"+t.toString(16)).slice(-2),N=(t,e,s)=>"#"+U(t)+U(e)+U(s),E=(t,s)=>{const r=L(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t*s)));return N(i,n,h)},q=(t,s)=>{const r=L(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t+(255-t)*s)));return N(i,n,h)},F={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},T=(t,e)=>I(null!=e?e:F,(e=>0==e?t:e>0?q(t,e):E(t,1- -1*e))),Y=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const h=s-.5*r;return[Math.round(255*(n[0]+h)),Math.round(255*(n[1]+h)),Math.round(255*(n[2]+h))]},H=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,P=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1)),Z=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),h=Math.max(r,i,n),a=Math.min(r,i,n),u=h-a,l=(a+h)/2;return[60*H(u,h,r,i,n),P(u,l),l]};class ${constructor(t){this.key=this.shuffle($.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[$.dictionary.indexOf(t[s]),$.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return $.dictionary[(s?t-e+$.dictionary.length:t+e)%$.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}}$.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const z=t=>Math.random().toString(16).slice(t),J=()=>z(-3),W=()=>z(-4),B=()=>W()+W(),G=()=>B()+B(),K=()=>J()+W(),Q=()=>G()+G()+G()+G(),R=()=>B()+"-"+W()+"-4"+J()+"-"+W()+"-"+(B()+W()),V=t=>("0"+t.toString()).slice(-2),X={"%y":t=>V(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>V(t.getMonth()+1),"%d":t=>V(t.getDate()),"%H":t=>V(t.getHours()),"%M":t=>V(t.getMinutes()),"%S":t=>V(t.getSeconds())},tt=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>X[t]?X[t](e):t.slice(1)))),et=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},st=36e5,rt=24*st,it={ms:1,s:1e3,m:6e4,h:st,d:rt,w:7*rt,y:315576e5},nt=["y","d","h","m","s","ms"],ht=t=>{const e=[];return nt.forEach((s=>{const r=it[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},at=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])*it[s[2]]||0;return r},ut=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class lt{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 ot(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 ct=(t,e)=>{let s=!1;const r=()=>ot(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},pt=()=>{},mt=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),dt=t=>mt(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),vt=dt,ft=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},yt=/&(amp|apos|lt|gt|nbsp|quot);/g,gt={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},_t=t=>t.replace(yt,((t,e)=>gt[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),xt=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),bt=t=>/^[\w]$/.test(t),wt=t=>/^[\d]$/.test(t),kt=t=>/^[A-Za-z]$/.test(t),jt=t=>/^[^\w]$/.test(t),St=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"},Ct=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),Ot=t=>h(t.split("")).join("");class Mt{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 Mt).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Mt).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=Ot(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 Mt).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Mt).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Mt).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Mt).upper()}}const At=t=>t.split("").reverse().join(""),It=t=>mt(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Dt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),Lt={"#":t=>t.value,0:t=>Dt(t,wt),9:t=>Dt(t,wt),A:t=>Dt(t,kt),Z:t=>Dt(t,kt),L:t=>Dt(t,kt).toLowerCase(),U:t=>Dt(t,kt).toUpperCase(),S:t=>Dt(t,jt)},Ut=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 w(n)||w(h)?"":(t=>Lt[t.key]?Lt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Ut(t)},Nt=(t,e)=>Ut({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),Et=(t,e)=>At(Nt(At(t),At(e))),qt=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),Et(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),Ft=(t,e)=>t.length==Nt(t,e).length,Tt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Yt=(t,e)=>Tt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Ht=(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]=Ht(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Ht(t,...e)},Pt=(t,e,s)=>ot(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(pt)})),Zt=(t,e,s)=>ot(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(pt)})),$t=t=>new Promise((e=>setTimeout(e,t)));class zt{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:d(this._value)?this._value():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}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!b(this.validate(t).error)}testAsync(t){return ot(this,void 0,void 0,(function*(){return!b((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 ot(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 Jt extends zt{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 Wt extends zt{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 Bt extends zt{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:tt(this._format,new Date(t))})))}parse(){return this.unshift((t=>(b(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class Gt extends zt{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 Kt extends zt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(y(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:h,value:a}=n.validate(t[n.name]);h&&e.push(h),c(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(!y(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Tt(e,t):t}}))}}class Qt extends zt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>b(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:dt(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:Nt(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:It(t)})))}}class Rt{static array(t){return(new Jt).items(t)}static boolean(){return new Wt}static date(){return new Bt}static number(){return new Gt}static object(t){return(new Kt).keys(t)}static string(){return new Qt}test(t,...e){return!b(Rt.validate(t,...e).error)}testAsync(t,...e){return ot(this,void 0,void 0,(function*(){return!b((yield Rt.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 ot(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{lt as EventEmitter,$ as Hash,Mt as RandomString,Rt as Validation,zt as ValidationBase,Tt as allowed,S as cache,vt as camelCase,t as chunkArray,e as clamp,T as colorPalette,ft as cutText,tt as dateFormat,et as dateRange,ut as debounce,_t as decodeHTML,Yt as denied,M as each,xt as encodeHTML,Pt as forEachAsync,C as forEachIndex,O as forEachKey,Zt as forEachSeries,A as forLoop,L as hexToRgb,Y as hslToRgb,l as isArray,o as isBoolean,bt as isCharacter,c as isDef,wt as isDigit,m as isFloat,d as isFunction,v as isInteger,kt as isLetter,f as isNull,p as isNumber,y as isObject,g as isPlainObject,x as isScalar,b as isString,jt as isSymbol,w as isUndefined,s as lerp,ct as loop,dt as lowerCamelCase,I as map,Nt as mask,qt as maskMoney,Et as maskReverse,Ft as maskTest,Ht as merge,ht as msToString,pt as noop,at as parseMs,St as parseSize,r as random,Ct as randomCase,i as randomElement,n as rangeArray,N as rgbToHex,Z as rgbToHsl,E as shadeColor,h as shuffleArray,Ot as shuffleString,K as simpleID,$t as sleep,At as stringReverse,q as tintColor,k as toArray,j as toBool,u as type,Q as uniqueID,It as upperCamelCase,R 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","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((t=>a["[object "+t+"]"]=t.toLowerCase()));const l=t=>"array"==u(t),o=t=>"boolean"==u(t),c=t=>"date"==u(t),p=t=>"null"!=u(t),m=t=>"number"==u(t),d=t=>m(t)&&!!(t%1),v=t=>"function"==typeof t,f=t=>m(t)&&!(t%1),y=t=>"map"==u(t),g=t=>null===t,_=t=>"object"==u(t),x=t=>_(t)&&t.constructor===Object,b=t=>"regexp"==u(t),w=["boolean","number","string"],k=t=>-1!=w.indexOf(u(t)),S=t=>"set"==u(t),j=t=>"string"==typeof t,C=t=>"symbol"==typeof t,O=t=>void 0===t,M=t=>l(t)?t:p(t)?[t]:[],A=t=>"true"===t||"1"===t,I=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},D=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},L=(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},U=(t,e,s)=>l(t)?D(t,e,s):_(t)?L(t,e,s):t,E=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},N=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),F=t=>parseInt(null!=t?t:"0",16)||0,q=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(F))||[]},T=t=>("0"+t.toString(16)).slice(-2),Y=(t,e,s)=>"#"+T(t)+T(e)+T(s),H=(t,s)=>{const r=q(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t*s)));return Y(i,n,h)},P=(t,s)=>{const r=q(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t+(255-t)*s)));return Y(i,n,h)},Z={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},$=(t,e)=>N(null!=e?e:Z,(e=>0==e?t:e>0?P(t,e):H(t,1- -1*e))),z=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const h=s-.5*r;return[Math.round(255*(n[0]+h)),Math.round(255*(n[1]+h)),Math.round(255*(n[2]+h))]},J=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,W=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1)),B=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),h=Math.max(r,i,n),a=Math.min(r,i,n),u=h-a,l=(a+h)/2;return[60*J(u,h,r,i,n),W(u,l),l]};class R{constructor(t){this.key=this.shuffle(R.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[R.dictionary.indexOf(t[s]),R.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return R.dictionary[(s?t-e+R.dictionary.length:t+e)%R.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}}R.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const G=t=>Math.random().toString(16).slice(t),K=()=>G(-3),Q=()=>G(-4),V=()=>Q()+Q(),X=()=>V()+V(),tt=()=>K()+Q(),et=()=>X()+X()+X()+X(),st=()=>V()+"-"+Q()+"-4"+K()+"-"+Q()+"-"+(V()+Q()),rt=t=>("0"+t.toString()).slice(-2),it={"%y":t=>rt(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>rt(t.getMonth()+1),"%d":t=>rt(t.getDate()),"%H":t=>rt(t.getHours()),"%M":t=>rt(t.getMinutes()),"%S":t=>rt(t.getSeconds())},nt=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>it[t]?it[t](e):t.slice(1)))),ht=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},at=36e5,ut=24*at,lt={ms:1,s:1e3,m:6e4,h:at,d:ut,w:7*ut,y:315576e5},ot=["y","d","h","m","s","ms"],ct=t=>{const e=[];return ot.forEach((s=>{const r=lt[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},pt=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])*lt[s[2]]||0;return r},mt=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class dt{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 vt(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 ft=(t,e)=>{let s=!1;const r=()=>vt(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},yt=()=>{},gt=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),_t=t=>gt(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),xt=_t,bt=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},wt=/&(amp|apos|lt|gt|nbsp|quot);/g,kt={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},St=t=>t.replace(wt,((t,e)=>kt[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),jt=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),Ct=t=>/^[\w]$/.test(t),Ot=t=>/^[\d]$/.test(t),Mt=t=>/^[A-Za-z]$/.test(t),At=t=>/^[^\w]$/.test(t),It=t=>{if(m(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"},Dt=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),Lt=t=>h(t.split("")).join("");class Ut{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 Ut).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Ut).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=Lt(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 Ut).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Ut).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Ut).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Ut).upper()}}const Et=t=>t.split("").reverse().join(""),Nt=t=>gt(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Ft=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),qt={"#":t=>t.value,0:t=>Ft(t,Ot),9:t=>Ft(t,Ot),A:t=>Ft(t,Mt),Z:t=>Ft(t,Mt),L:t=>Ft(t,Mt).toLowerCase(),U:t=>Ft(t,Mt).toUpperCase(),S:t=>Ft(t,At)},Tt=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 O(n)||O(h)?"":(t=>qt[t.key]?qt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Tt(t)},Yt=(t,e)=>Tt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),Ht=(t,e)=>Et(Yt(Et(t),Et(e))),Pt=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),Ht(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),Zt=(t,e)=>t.length==Yt(t,e).length,$t=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),zt=(t,e)=>$t(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Jt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(x(t)&&x(s))for(const e in s)x(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Jt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Jt(t,...e)},Wt=(t,e,s)=>vt(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(yt)})),Bt=(t,e,s)=>vt(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(yt)})),Rt=t=>new Promise((e=>setTimeout(e,t)));class Gt{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=p(t);return{break:!e,value:e?t:v(this._value)?this._value():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}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>p(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!j(this.validate(t).error)}testAsync(t){return vt(this,void 0,void 0,(function*(){return!j((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 vt(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 Kt extends Gt{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 Qt extends Gt{constructor(){super(),this._type="boolean",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:p(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Vt extends Gt{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:nt(this._format,new Date(t))})))}parse(){return this.unshift((t=>(j(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class Xt extends Gt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>m(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:p(t)?parseFloat(t):t})))}}class te extends Gt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(_(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:h,value:a}=n.validate(t[n.name]);h&&e.push(h),p(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(!_(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?$t(e,t):t}}))}}class ee extends Gt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>j(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:_t(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:Yt(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:_(t)?JSON.stringify(t):p(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 se{static array(t){return(new Kt).items(t)}static boolean(){return new Qt}static date(){return new Vt}static number(){return new Xt}static object(t){return(new te).keys(t)}static string(){return new ee}test(t,...e){return!j(se.validate(t,...e).error)}testAsync(t,...e){return vt(this,void 0,void 0,(function*(){return!j((yield se.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 vt(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{dt as EventEmitter,R as Hash,Ut as RandomString,se as Validation,Gt as ValidationBase,$t as allowed,I as cache,xt as camelCase,t as chunkArray,e as clamp,$ as colorPalette,bt as cutText,nt as dateFormat,ht as dateRange,mt as debounce,St as decodeHTML,zt as denied,U as each,jt as encodeHTML,Wt as forEachAsync,D as forEachIndex,L as forEachKey,Bt as forEachSeries,E as forLoop,q as hexToRgb,z as hslToRgb,l as isArray,o as isBoolean,Ct as isCharacter,c as isDate,p as isDef,Ot as isDigit,d as isFloat,v as isFunction,f as isInteger,Mt as isLetter,y as isMap,g as isNull,m as isNumber,_ as isObject,x as isPlainObject,b as isRegExp,k as isScalar,S as isSet,At as isSpecialChar,j as isString,C as isSymbol,O as isUndefined,s as lerp,ft as loop,_t as lowerCamelCase,N as map,Yt as mask,Pt as maskMoney,Ht as maskReverse,Zt as maskTest,Jt as merge,ct as msToString,yt as noop,pt as parseMs,It as parseSize,r as random,Dt as randomCase,i as randomElement,n as rangeArray,Y as rgbToHex,B as rgbToHsl,H as shadeColor,h as shuffleArray,Lt as shuffleString,tt as simpleID,Rt as sleep,Et as stringReverse,P as tintColor,M as toArray,A as toBool,u as type,et as uniqueID,Nt as upperCamelCase,st as uuidv4};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.3.6
2
+ * @andrewcaires/utils.js v0.4.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),u=t=>"null"!=n(t),l=t=>"number"==n(t),o=t=>"function"==n(t),c=t=>"object"==n(t),p=t=>c(t)&&t.constructor===Object,m=["boolean","number","string"],d=t=>"string"==n(t),v=t=>void 0===t,f=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},g=(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},y=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),_=t=>parseInt(null!=t?t:"0",16)||0,x=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(_))||[]},b=t=>("0"+t.toString(16)).slice(-2),w=(t,e,s)=>"#"+b(t)+b(e)+b(s),k=(t,s)=>{const r=x(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return w(i,n,a)},S=(t,s)=>{const r=x(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return w(i,n,a)},C={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},j=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,O=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1));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,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}M.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const A=t=>Math.random().toString(16).slice(t),I=()=>A(-3),E=()=>A(-4),T=()=>E()+E(),D=()=>T()+T(),L=t=>("0"+t.toString()).slice(-2),U={"%y":t=>L(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>L(t.getMonth()+1),"%d":t=>L(t.getDate()),"%H":t=>L(t.getHours()),"%M":t=>L(t.getMinutes()),"%S":t=>L(t.getSeconds())},N=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>U[t]?U[t](e):t.slice(1)))),F=36e5,H=24*F,q={ms:1,s:1e3,m:6e4,h:F,d:H,w:7*H,y:315576e5},R=["y","d","h","m","s","ms"];function P(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 B=()=>{},Y=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),z=t=>Y(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),J=z,Z=/&(amp|apos|lt|gt|nbsp|quot);/g,$={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},V=t=>/^[\d]$/.test(t),W=t=>/^[A-Za-z]$/.test(t),K=t=>/^[^\w]$/.test(t),G=t=>r(t.split("")).join("");class Q{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 Q).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Q).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=G(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 Q).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Q).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Q).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Q).upper()}}const X=t=>t.split("").reverse().join(""),tt=t=>Y(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),et=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),st={"#":t=>t.value,0:t=>et(t,V),9:t=>et(t,V),A:t=>et(t,W),Z:t=>et(t,W),L:t=>et(t,W).toLowerCase(),U:t=>et(t,W).toUpperCase(),S:t=>et(t,K)},rt=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 v(n)||v(a)?"":(t=>st[t.key]?st[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+rt(t)},it=(t,e)=>rt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),nt=(t,e)=>X(it(X(t),X(e))),at=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),ht=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(p(t)&&p(s))for(const e in s)p(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=ht(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return ht(t,...e)};class ut{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:o(this._value)?this._value():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}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>u(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!d(this.validate(t).error)}testAsync(t){return P(this,void 0,void 0,(function*(){return!d((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 P(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 lt extends ut{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 ot extends ut{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 ct extends ut{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:N(this._format,new Date(t))})))}parse(){return this.unshift((t=>(d(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class pt extends ut{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 mt extends ut{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(c(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:a,value:h}=n.validate(t[n.name]);a&&e.push(a),u(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(!c(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?at(e,t):t}}))}}class dt extends ut{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>d(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:z(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:it(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:c(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:tt(t)})))}}class vt{static array(t){return(new lt).items(t)}static boolean(){return new ot}static date(){return new ct}static number(){return new pt}static object(t){return(new mt).keys(t)}static string(){return new dt}test(t,...e){return!d(vt.validate(t,...e).error)}testAsync(t,...e){return P(this,void 0,void 0,(function*(){return!d((yield vt.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 P(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=M,t.RandomString=Q,t.Validation=vt,t.ValidationBase=ut,t.allowed=at,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=J,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.colorPalette=(t,e)=>y(null!=e?e:C,(e=>0==e?t:e>0?S(t,e):k(t,1- -1*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=N,t.dateRange=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},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(Z,((t,e)=>$[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>at(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?f(t,e,s):c(t)?g(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>P(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(B)})),t.forEachIndex=f,t.forEachKey=g,t.forEachSeries=(t,e,s)=>P(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(B)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=x,t.hslToRgb=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const a=s-.5*r;return[Math.round(255*(n[0]+a)),Math.round(255*(n[1]+a)),Math.round(255*(n[2]+a))]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=u,t.isDigit=V,t.isFloat=t=>l(t)&&!!(t%1),t.isFunction=o,t.isInteger=t=>l(t)&&!(t%1),t.isLetter=W,t.isNull=t=>null===t,t.isNumber=l,t.isObject=c,t.isPlainObject=p,t.isScalar=t=>-1!=m.indexOf(n(t)),t.isString=d,t.isSymbol=K,t.isUndefined=v,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>P(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=z,t.map=y,t.mask=it,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),nt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=nt,t.maskTest=(t,e)=>t.length==it(t,e).length,t.merge=ht,t.msToString=t=>{const e=[];return R.forEach((s=>{const r=q[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=B,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])*q[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=w,t.rgbToHsl=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),a=Math.max(r,i,n),h=Math.min(r,i,n),u=a-h,l=(h+a)/2;return[60*j(u,a,r,i,n),O(u,l),l]},t.shadeColor=k,t.shuffleArray=r,t.shuffleString=G,t.simpleID=()=>I()+E(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=X,t.tintColor=S,t.toArray=t=>a(t)?t:u(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>D()+D()+D()+D(),t.upperCamelCase=tt,t.uuidv4=()=>T()+"-"+E()+"-4"+I()+"-"+E()+"-"+(T()+E()),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","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","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=>"function"==typeof t,c=t=>"object"==n(t),p=t=>c(t)&&t.constructor===Object,m=["boolean","number","string"],d=t=>"string"==typeof t,v=t=>void 0===t,f=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},g=(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},y=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),_=t=>parseInt(null!=t?t:"0",16)||0,x=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(_))||[]},b=t=>("0"+t.toString(16)).slice(-2),w=(t,e,s)=>"#"+b(t)+b(e)+b(s),k=(t,s)=>{const r=x(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return w(i,n,a)},S=(t,s)=>{const r=x(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return w(i,n,a)},C={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},j=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,M=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1));class O{constructor(t){this.key=this.shuffle(O.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[O.dictionary.indexOf(t[s]),O.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return O.dictionary[(s?t-e+O.dictionary.length:t+e)%O.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}}O.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const A=t=>Math.random().toString(16).slice(t),I=()=>A(-3),E=()=>A(-4),D=()=>E()+E(),T=()=>D()+D(),L=t=>("0"+t.toString()).slice(-2),U={"%y":t=>L(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>L(t.getMonth()+1),"%d":t=>L(t.getDate()),"%H":t=>L(t.getHours()),"%M":t=>L(t.getMinutes()),"%S":t=>L(t.getSeconds())},F=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>U[t]?U[t](e):t.slice(1)))),N=36e5,H=24*N,R={ms:1,s:1e3,m:6e4,h:N,d:H,w:7*H,y:315576e5},q=["y","d","h","m","s","ms"];function P(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 B=()=>{},Y=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),z=t=>Y(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),J=z,Z=/&(amp|apos|lt|gt|nbsp|quot);/g,$={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},V=t=>/^[\d]$/.test(t),W=t=>/^[A-Za-z]$/.test(t),K=t=>/^[^\w]$/.test(t),G=t=>r(t.split("")).join("");class Q{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 Q).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Q).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=G(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 Q).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Q).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Q).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Q).upper()}}const X=t=>t.split("").reverse().join(""),tt=t=>Y(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),et=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),st={"#":t=>t.value,0:t=>et(t,V),9:t=>et(t,V),A:t=>et(t,W),Z:t=>et(t,W),L:t=>et(t,W).toLowerCase(),U:t=>et(t,W).toUpperCase(),S:t=>et(t,K)},rt=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 v(n)||v(a)?"":(t=>st[t.key]?st[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+rt(t)},it=(t,e)=>rt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),nt=(t,e)=>X(it(X(t),X(e))),at=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),ht=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(p(t)&&p(s))for(const e in s)p(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=ht(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return ht(t,...e)};class ut{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:o(this._value)?this._value():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}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>u(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!d(this.validate(t).error)}testAsync(t){return P(this,void 0,void 0,(function*(){return!d((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 P(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 lt extends ut{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 ot extends ut{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 ct extends ut{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:F(this._format,new Date(t))})))}parse(){return this.unshift((t=>(d(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class pt extends ut{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 mt extends ut{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(c(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:a,value:h}=n.validate(t[n.name]);a&&e.push(a),u(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(!c(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?at(e,t):t}}))}}class dt extends ut{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>d(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:z(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:it(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:c(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:tt(t)})))}}class vt{static array(t){return(new lt).items(t)}static boolean(){return new ot}static date(){return new ct}static number(){return new pt}static object(t){return(new mt).keys(t)}static string(){return new dt}test(t,...e){return!d(vt.validate(t,...e).error)}testAsync(t,...e){return P(this,void 0,void 0,(function*(){return!d((yield vt.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 P(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=O,t.RandomString=Q,t.Validation=vt,t.ValidationBase=ut,t.allowed=at,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=J,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.colorPalette=(t,e)=>y(null!=e?e:C,(e=>0==e?t:e>0?S(t,e):k(t,1- -1*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=F,t.dateRange=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},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(Z,((t,e)=>$[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>at(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?f(t,e,s):c(t)?g(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>P(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(B)})),t.forEachIndex=f,t.forEachKey=g,t.forEachSeries=(t,e,s)=>P(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(B)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=x,t.hslToRgb=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const a=s-.5*r;return[Math.round(255*(n[0]+a)),Math.round(255*(n[1]+a)),Math.round(255*(n[2]+a))]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDate=t=>"date"==n(t),t.isDef=u,t.isDigit=V,t.isFloat=t=>l(t)&&!!(t%1),t.isFunction=o,t.isInteger=t=>l(t)&&!(t%1),t.isLetter=W,t.isMap=t=>"map"==n(t),t.isNull=t=>null===t,t.isNumber=l,t.isObject=c,t.isPlainObject=p,t.isRegExp=t=>"regexp"==n(t),t.isScalar=t=>-1!=m.indexOf(n(t)),t.isSet=t=>"set"==n(t),t.isSpecialChar=K,t.isString=d,t.isSymbol=t=>"symbol"==typeof t,t.isUndefined=v,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>P(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=z,t.map=y,t.mask=it,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),nt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=nt,t.maskTest=(t,e)=>t.length==it(t,e).length,t.merge=ht,t.msToString=t=>{const e=[];return q.forEach((s=>{const r=R[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=B,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])*R[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=w,t.rgbToHsl=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),a=Math.max(r,i,n),h=Math.min(r,i,n),u=a-h,l=(h+a)/2;return[60*j(u,a,r,i,n),M(u,l),l]},t.shadeColor=k,t.shuffleArray=r,t.shuffleString=G,t.simpleID=()=>I()+E(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=X,t.tintColor=S,t.toArray=t=>a(t)?t:u(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>T()+T()+T()+T(),t.upperCamelCase=tt,t.uuidv4=()=>D()+"-"+E()+"-4"+I()+"-"+E()+"-"+(D()+E()),t}({});
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@andrewcaires/utils.js",
3
- "version": "0.3.6",
3
+ "version": "0.4.0",
4
4
  "description": "JavaScript utility library for web and nodejs development",
5
+ "type": "module",
5
6
  "main": "./dist/index.cjs.js",
6
7
  "unpkg": "./dist/index.min.js",
7
8
  "types": "./dist/index.d.ts",
@@ -20,8 +21,8 @@
20
21
  "dist"
21
22
  ],
22
23
  "scripts": {
23
- "build": "rollup --bundleConfigAsCjs -c ./rollup.config.js",
24
- "dev": "ts-node ./src/index.ts"
24
+ "dev": "tsx watch ./src/index.ts",
25
+ "build": "rollup --bundleConfigAsCjs -c ./rollup.config.js"
25
26
  },
26
27
  "repository": {
27
28
  "type": "git",
@@ -46,12 +47,12 @@
46
47
  "@rollup/plugin-commonjs": "^28.0.0",
47
48
  "@rollup/plugin-terser": "^0.4.4",
48
49
  "@rollup/plugin-typescript": "^12.1.0",
49
- "@types/node": "^22.7.4",
50
- "eslint": "^9.11.1",
50
+ "@types/node": "^22.7.5",
51
+ "eslint": "^9.12.0",
51
52
  "rollup": "^4.24.0",
52
53
  "rollup-plugin-dts": "^6.1.1",
53
- "ts-node": "^10.9.2",
54
54
  "tslib": "^2.7.0",
55
- "typescript-eslint": "^8.8.0"
55
+ "tsx": "^4.19.1",
56
+ "typescript-eslint": "^8.8.1"
56
57
  }
57
58
  }