@andrewcaires/utils.js 0.3.2 → 0.3.4
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 +2 -2
- package/dist/index.d.ts +18 -5
- package/dist/index.esm.js +2 -2
- package/dist/index.min.js +2 -2
- package/package.json +5 -5
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
"use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),l=t=>"object"==i(t),u=t=>l(t)&&t.constructor===Object,p=t=>"string"==i(t),c=t=>void 0===t,m=t=>parseInt(null!=t?t:"0",16)||0,d=t=>("0"+t).slice(-2);class x{constructor(t){this.key=this.shuffle(x.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[x.dictionary.indexOf(t[s]),x.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return x.dictionary[(s?t-e+x.dictionary.length:t+e)%x.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}x.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const v=t=>Math.random().toString(16).slice(t),f=()=>v(-3),g=()=>v(-4),y=()=>g()+g(),_=()=>y()+y(),b=t=>("0"+t.toString()).slice(-2),w={"%y":t=>b(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>b(t.getMonth()+1),"%d":t=>b(t.getDate()),"%H":t=>b(t.getHours()),"%M":t=>b(t.getMinutes()),"%S":t=>b(t.getSeconds())},k=36e5,S=24*k,C={ms:1,s:1e3,m:6e4,h:k,d:S,w:7*S,y:315576e5},j=["y","d","h","m","s","ms"];function O(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{o(r.next(t))}catch(t){n(t)}}function h(t){try{o(r.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}o((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const A=()=>{},I=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},M=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},E=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),L=t=>E(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),T=L,N=/&(amp|apos|lt|gt|nbsp|quot);/g,U={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},F=t=>/^[\d]$/.test(t),q=t=>/^[A-Za-z]$/.test(t),H=t=>/^[^\w]$/.test(t),D=t=>s(t.split("")).join("");class B{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new B).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new B).lower()}make(t){let 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=D(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 B).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new B).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new B).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new B).upper()}}const P=t=>t.split("").reverse().join(""),R=t=>E(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Y=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),z={"#":t=>t.value,0:t=>Y(t,F),9:t=>Y(t,F),A:t=>Y(t,q),Z:t=>Y(t,q),L:t=>Y(t,q).toLowerCase(),U:t=>Y(t,q).toUpperCase(),S:t=>Y(t,H)},Z=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>z[t.key]?z[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Z(t)},$=(t,e)=>Z({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),J=(t,e)=>P($(P(t),P(e))),V=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),W=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(u(t)&&u(s))for(const e in s)u(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=W(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return W(t,...e)};class K{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=h(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return O(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return O(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class G extends K{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 Q extends K{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 X extends K{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 tt extends K{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?V(e,t):t}}))}}class et extends K{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:L(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:$(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:R(t)})))}}class st{static array(t){return(new G).items(t)}static boolean(){return new Q}static number(){return new X}static object(t){return(new tt).keys(t)}static string(){return new et}test(t,...e){return!p(st.validate(t,...e).error)}testAsync(t,...e){return O(this,void 0,void 0,(function*(){return!p((yield st.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return O(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},exports.Hash=x,exports.RandomString=B,exports.Validation=st,exports.ValidationBase=K,exports.allowed=V,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=T,exports.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},exports.clamp=t,exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>w[e]?w[e](t):e.slice(1))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(N,((t,e)=>U[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>V(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?I(t,e,s):l(t)?M(t,e,s):t,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>O(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(A)})),exports.forEachIndex=I,exports.forEachKey=M,exports.forEachSeries=(t,e,s)=>O(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(A)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>m(t))))||[]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=F,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=q,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=u,exports.isString=p,exports.isSymbol=H,exports.isUndefined=c,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>O(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=L,exports.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},exports.mask=$,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),J(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=J,exports.maskTest=(t,e)=>t.length==$(t,e).length,exports.merge=W,exports.msToString=t=>{const e=[];return j.forEach((s=>{const r=C[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=A,exports.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*C[s[2]]||0;return r},exports.parseSize=t=>{if(o(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.randomElement=t=>t.length?t[e(0,t.length-1)]:null,exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=(t,e,s)=>"#"+d(t.toString(16))+d(e.toString(16))+d(s.toString(16)),exports.shadeColor=(e,s)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>d(((e,s)=>t(m(e)+s,0,255).toString(16))(e,s)))),exports.shuffleArray=s,exports.shuffleString=D,exports.simpleID=()=>f()+g(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=P,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>_()+_()+_()+_(),exports.upperCamelCase=R,exports.uuidv4=()=>y()+"-"+g()+"-4"+f()+"-"+g()+"-"+(y()+g());
|
|
7
|
+
"use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),l=t=>"object"==i(t),u=t=>l(t)&&t.constructor===Object,p=["boolean","number","string"],c=t=>"string"==i(t),m=t=>void 0===t,d=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},x=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},v=(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,g=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(f))||[]},y=t=>("0"+t.toString(16)).slice(-2),_=(t,e,s)=>"#"+y(t)+y(e)+y(s),b=(e,s)=>{const r=g(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return _(i,n,a)},w=(e,s)=>{const r=g(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return _(i,n,a)},k={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,S=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1));class j{constructor(t){this.key=this.shuffle(j.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[j.dictionary.indexOf(t[s]),j.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return j.dictionary[(s?t-e+j.dictionary.length:t+e)%j.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}}j.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const O=t=>Math.random().toString(16).slice(t),M=()=>O(-3),A=()=>O(-4),I=()=>A()+A(),E=()=>I()+I(),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())},N=36e5,U=24*N,F={ms:1,s:1e3,m:6e4,h:N,d:U,w:7*U,y:315576e5},H=["y","d","h","m","s","ms"];function q(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 D=()=>{},P=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),R=t=>P(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),B=R,Y=/&(amp|apos|lt|gt|nbsp|quot);/g,z={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},Z=t=>/^[\d]$/.test(t),$=t=>/^[A-Za-z]$/.test(t),J=t=>/^[^\w]$/.test(t),V=t=>s(t.split("")).join("");class W{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 W).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new W).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=V(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 W).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new W).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new W).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new W).upper()}}const K=t=>t.split("").reverse().join(""),G=t=>P(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Q=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),X={"#":t=>t.value,0:t=>Q(t,Z),9:t=>Q(t,Z),A:t=>Q(t,$),Z:t=>Q(t,$),L:t=>Q(t,$).toLowerCase(),U:t=>Q(t,$).toUpperCase(),S:t=>Q(t,J)},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:a}=t;return m(n)||m(a)?"":(t=>X[t.key]?X[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+tt(t)},et=(t,e)=>tt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),st=(t,e)=>K(et(K(t),K(e))),rt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),it=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(u(t)&&u(s))for(const e in s)u(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=it(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return it(t,...e)};class nt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=h(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!c(this.validate(t).error)}testAsync(t){return q(this,void 0,void 0,(function*(){return!c((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 at extends nt{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 ht extends nt{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 ot extends nt{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 lt extends nt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?rt(e,t):t}}))}}class ut extends nt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>c(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:R(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:et(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:G(t)})))}}class pt{static array(t){return(new at).items(t)}static boolean(){return new ht}static number(){return new ot}static object(t){return(new lt).keys(t)}static string(){return new ut}test(t,...e){return!c(pt.validate(t,...e).error)}testAsync(t,...e){return q(this,void 0,void 0,(function*(){return!c((yield pt.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=j,exports.RandomString=W,exports.Validation=pt,exports.ValidationBase=nt,exports.allowed=rt,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=B,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)=>v(null!=e?e:k,(e=>0==e?t:e>0?w(t,e):b(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=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>L[t]?L[t](e):t.slice(1)))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(Y,((t,e)=>z[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>rt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?d(t,e,s):l(t)?x(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(D)})),exports.forEachIndex=d,exports.forEachKey=x,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(D)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hexToRgb=g,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=Z,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=$,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=u,exports.isScalar=t=>-1!=p.indexOf(i(t)),exports.isString=c,exports.isSymbol=J,exports.isUndefined=m,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=R,exports.map=v,exports.mask=et,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),st(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=st,exports.maskTest=(t,e)=>t.length==et(t,e).length,exports.merge=it,exports.msToString=t=>{const e=[];return H.forEach((s=>{const r=F[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=D,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])*F[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=_,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,l=(h+a)/2;return[60*C(o,a,r,i,n),S(o,l),l]},exports.shadeColor=b,exports.shuffleArray=s,exports.shuffleString=V,exports.simpleID=()=>M()+A(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=K,exports.tintColor=w,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>E()+E()+E()+E(),exports.upperCamelCase=G,exports.uuidv4=()=>I()+"-"+A()+"-4"+M()+"-"+A()+"-"+(I()+A());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
@@ -31,6 +31,7 @@ type TypeArrayString = TypeArray<string>;
|
|
|
31
31
|
type TypeArrayNumber = TypeArray<number>;
|
|
32
32
|
type TypeArrayFunction = TypeArray<TypeCallbackFunction>;
|
|
33
33
|
type TypeNullable<T> = T | null;
|
|
34
|
+
type TypeScalar = boolean | number | string;
|
|
34
35
|
type TypeObject<T> = {
|
|
35
36
|
[key: string]: T;
|
|
36
37
|
};
|
|
@@ -59,6 +60,8 @@ declare const isObject: (test: any) => test is TypeObjectAny;
|
|
|
59
60
|
|
|
60
61
|
declare const isPlainObject: (test: any) => test is TypeObjectAny;
|
|
61
62
|
|
|
63
|
+
declare const isScalar: (test: any) => test is TypeScalar;
|
|
64
|
+
|
|
62
65
|
declare const isString: (test: any) => test is string;
|
|
63
66
|
|
|
64
67
|
declare const isUndefined: (test: any) => test is undefined;
|
|
@@ -69,11 +72,19 @@ type CacheCallback = (key: any) => TypeObjectAny;
|
|
|
69
72
|
|
|
70
73
|
declare const cache: () => CacheCallback;
|
|
71
74
|
|
|
75
|
+
declare const colorPalette: (color: string, variants?: TypeObjectNumber) => TypeObjectString;
|
|
76
|
+
|
|
72
77
|
declare const hexToRgb: (color: string) => Array<number>;
|
|
73
78
|
|
|
79
|
+
declare const hslToRgb: (h: number, s: number, l: number) => number[];
|
|
80
|
+
|
|
74
81
|
declare const rgbToHex: (r: number, g: number, b: number) => string;
|
|
75
82
|
|
|
76
|
-
declare const
|
|
83
|
+
declare const rgbToHsl: (r: number, g: number, b: number) => Array<number>;
|
|
84
|
+
|
|
85
|
+
declare const shadeColor: (color: string, intensity: number) => string;
|
|
86
|
+
|
|
87
|
+
declare const tintColor: (color: string, intensity: number) => string;
|
|
77
88
|
|
|
78
89
|
declare class Hash {
|
|
79
90
|
private static readonly dictionary;
|
|
@@ -92,7 +103,7 @@ declare const uniqueID: () => string;
|
|
|
92
103
|
|
|
93
104
|
declare const uuidv4: () => string;
|
|
94
105
|
|
|
95
|
-
declare const dateFormat: (
|
|
106
|
+
declare const dateFormat: (format: string, date?: Date) => string;
|
|
96
107
|
|
|
97
108
|
declare const msToString: (ms: number) => string;
|
|
98
109
|
|
|
@@ -145,7 +156,7 @@ type LoopCallback = (index: number, count: number) => any;
|
|
|
145
156
|
|
|
146
157
|
declare const forLoop: (count: number, callback: LoopCallback, thisArg?: any) => number;
|
|
147
158
|
|
|
148
|
-
declare const map: <T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<
|
|
159
|
+
declare const map: <T, R = T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<R>;
|
|
149
160
|
|
|
150
161
|
declare const mask: (mask: string, text: string) => string;
|
|
151
162
|
|
|
@@ -162,7 +173,9 @@ declare const lerp: (value1: number, value2: number, amount: number) => number;
|
|
|
162
173
|
declare const random: (min: number, max: number) => number;
|
|
163
174
|
|
|
164
175
|
declare const allowed: (allowed: Array<string>, target: TypeObjectAny) => TypeObjectAny;
|
|
176
|
+
|
|
165
177
|
declare const denied: (denied: Array<string>, target: TypeObjectAny) => TypeObjectAny;
|
|
178
|
+
|
|
166
179
|
declare const merge: (target: TypeObjectAny, ...sources: Array<TypeObjectAny>) => TypeObjectAny;
|
|
167
180
|
|
|
168
181
|
type ForEachCallback<T> = TypeCallback<T, number, Array<T>, Promise<void>>;
|
|
@@ -334,4 +347,4 @@ declare class Validation {
|
|
|
334
347
|
|
|
335
348
|
type ValidationCallbackAsync = (value: any) => Promise<IValidationResult>;
|
|
336
349
|
|
|
337
|
-
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, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
|
350
|
+
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, 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 };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.4
|
|
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),v=t=>"function"==u(t),d=t=>p(t)&&!(t%1),f=t=>null===t,g=t=>"object"==u(t),y=t=>g(t)&&t.constructor===Object,_=t=>"string"==u(t),x=t=>void 0===t,b=t=>l(t)?t:c(t)?[t]:[],w=t=>"true"===t||"1"===t,k=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},S=t=>parseInt(null!=t?t:"0",16)||0,j=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>S(t))))||[]},C=t=>("0"+t).slice(-2),O=(t,e,s)=>"#"+C(t.toString(16))+C(e.toString(16))+C(s.toString(16)),A=(t,s)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>C(((t,s)=>e(S(t)+s,0,255).toString(16))(t,s))));class I{constructor(t){this.key=this.shuffle(I.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[I.dictionary.indexOf(t[s]),I.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return I.dictionary[(s?t-e+I.dictionary.length:t+e)%I.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}}I.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const M=t=>Math.random().toString(16).slice(t),L=()=>M(-3),U=()=>M(-4),N=()=>U()+U(),q=()=>N()+N(),E=()=>L()+U(),F=()=>q()+q()+q()+q(),T=()=>N()+"-"+U()+"-4"+L()+"-"+U()+"-"+(N()+U()),Y=t=>("0"+t.toString()).slice(-2),H={"%y":t=>Y(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>Y(t.getMonth()+1),"%d":t=>Y(t.getDate()),"%H":t=>Y(t.getHours()),"%M":t=>Y(t.getMinutes()),"%S":t=>Y(t.getSeconds())},P=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>H[e]?H[e](t):e.slice(1))),Z=36e5,$=24*Z,z={ms:1,s:1e3,m:6e4,h:Z,d:$,w:7*$,y:315576e5},D=["y","d","h","m","s","ms"],J=t=>{const e=[];return D.forEach((s=>{const r=z[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},W=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*z[s[2]]||0;return r},B=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class G{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}}function K(t,e,s,r){return new(s||(s=Promise))((function(i,n){function h(t){try{u(r.next(t))}catch(t){n(t)}}function a(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const Q=(t,e)=>{let s=!1;const r=()=>K(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},R=()=>{},V=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},X=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},tt=(t,e,s)=>l(t)?V(t,e,s):g(t)?X(t,e,s):t,et=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},st=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},rt=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),it=t=>rt(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),nt=it,ht=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},at=/&(amp|apos|lt|gt|nbsp|quot);/g,ut={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},lt=t=>t.replace(at,((t,e)=>ut[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),ot=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),ct=t=>/^[\w]$/.test(t),pt=t=>/^[\d]$/.test(t),mt=t=>/^[A-Za-z]$/.test(t),vt=t=>/^[^\w]$/.test(t),dt=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"},ft=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),gt=t=>h(t.split("")).join("");class yt{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 yt).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new yt).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=gt(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 yt).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new yt).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new yt).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new yt).upper()}}const _t=t=>t.split("").reverse().join(""),xt=t=>rt(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),bt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),wt={"#":t=>t.value,0:t=>bt(t,pt),9:t=>bt(t,pt),A:t=>bt(t,mt),Z:t=>bt(t,mt),L:t=>bt(t,mt).toLowerCase(),U:t=>bt(t,mt).toUpperCase(),S:t=>bt(t,vt)},kt=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:h}=t;return x(n)||x(h)?"":(t=>wt[t.key]?wt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+kt(t)},St=(t,e)=>kt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),jt=(t,e)=>_t(St(_t(t),_t(e))),Ct=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),jt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),Ot=(t,e)=>t.length==St(t,e).length,At=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),It=(t,e)=>At(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Mt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(y(t)&&y(s))for(const e in s)y(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Mt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Mt(t,...e)},Lt=(t,e,s)=>K(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(R)})),Ut=(t,e,s)=>K(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(R)})),Nt=t=>new Promise((e=>setTimeout(e,t)));class qt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=c(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!_(this.validate(t).error)}testAsync(t){return K(this,void 0,void 0,(function*(){return!_((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return K(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class Et extends qt{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 Ft extends qt{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 Tt extends qt{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 Yt extends qt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(g(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),c(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!g(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 Ht extends qt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>_(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:it(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:St(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:g(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:xt(t)})))}}class Pt{static array(t){return(new Et).items(t)}static boolean(){return new Ft}static number(){return new Tt}static object(t){return(new Yt).keys(t)}static string(){return new Ht}test(t,...e){return!_(Pt.validate(t,...e).error)}testAsync(t,...e){return K(this,void 0,void 0,(function*(){return!_((yield Pt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return K(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}export{G as EventEmitter,I as Hash,yt as RandomString,Pt as Validation,qt as ValidationBase,At as allowed,k as cache,nt as camelCase,t as chunkArray,e as clamp,ht as cutText,P as dateFormat,B as debounce,lt as decodeHTML,It as denied,tt as each,ot as encodeHTML,Lt as forEachAsync,V as forEachIndex,X as forEachKey,Ut as forEachSeries,et as forLoop,j as hexToRgb,l as isArray,o as isBoolean,ct as isCharacter,c as isDef,pt as isDigit,m as isFloat,v as isFunction,d as isInteger,mt as isLetter,f as isNull,p as isNumber,g as isObject,y as isPlainObject,_ as isString,vt as isSymbol,x as isUndefined,s as lerp,Q as loop,it as lowerCamelCase,st as map,St as mask,Ct as maskMoney,jt as maskReverse,Ot as maskTest,Mt as merge,J as msToString,R as noop,W as parseMs,dt as parseSize,r as random,ft as randomCase,i as randomElement,n as rangeArray,O as rgbToHex,A as shadeColor,h as shuffleArray,gt as shuffleString,E as simpleID,Nt as sleep,_t as stringReverse,b as toArray,w as toBool,u as type,F as uniqueID,xt as upperCamelCase,T as uuidv4};
|
|
7
|
+
const t=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,s,r)=>t+(s-t)*e(r,0,1),r=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),i=t=>t.length?t[r(0,t.length-1)]:null,n=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),h=t=>{for(let e=t.length-1;e>0;e--){const s=r(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},a={},u=t=>null==t?"null":a[a.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>a["[object "+t+"]"]=t.toLowerCase()));const l=t=>"array"==u(t),o=t=>"boolean"==u(t),c=t=>"null"!=u(t),p=t=>"number"==u(t),m=t=>p(t)&&!!(t%1),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)]))),L=t=>parseInt(null!=t?t:"0",16)||0,U=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(L))||[]},N=t=>("0"+t.toString(16)).slice(-2),E=(t,e,s)=>"#"+N(t)+N(e)+N(s),q=(t,s)=>{const r=U(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t*s)));return E(i,n,h)},F=(t,s)=>{const r=U(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t+(255-t)*s)));return E(i,n,h)},T={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},Y=(t,e)=>I(null!=e?e:T,(e=>0==e?t:e>0?F(t,e):q(t,1- -1*e))),D=(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=36e5,st=24*et,rt={ms:1,s:1e3,m:6e4,h:et,d:st,w:7*st,y:315576e5},it=["y","d","h","m","s","ms"],nt=t=>{const e=[];return it.forEach((s=>{const r=rt[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},ht=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])*rt[s[2]]||0;return r},at=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class ut{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 lt(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 ot=(t,e)=>{let s=!1;const r=()=>lt(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},ct=()=>{},pt=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),mt=t=>pt(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),dt=mt,vt=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},ft=/&(amp|apos|lt|gt|nbsp|quot);/g,yt={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},gt=t=>t.replace(ft,((t,e)=>yt[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),_t=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),xt=t=>/^[\w]$/.test(t),bt=t=>/^[\d]$/.test(t),wt=t=>/^[A-Za-z]$/.test(t),kt=t=>/^[^\w]$/.test(t),jt=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"},St=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),Ct=t=>h(t.split("")).join("");class Ot{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 Ot).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Ot).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=Ct(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 Ot).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Ot).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Ot).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Ot).upper()}}const Mt=t=>t.split("").reverse().join(""),At=t=>pt(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),It=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),Lt={"#":t=>t.value,0:t=>It(t,bt),9:t=>It(t,bt),A:t=>It(t,wt),Z:t=>It(t,wt),L:t=>It(t,wt).toLowerCase(),U:t=>It(t,wt).toUpperCase(),S:t=>It(t,kt)},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)=>Mt(Nt(Mt(t),Mt(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),Dt=(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]=Dt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Dt(t,...e)},Ht=(t,e,s)=>lt(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(ct)})),Pt=(t,e,s)=>lt(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(ct)})),Zt=t=>new Promise((e=>setTimeout(e,t)));class $t{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=c(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!b(this.validate(t).error)}testAsync(t){return lt(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 lt(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 zt extends $t{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 Jt extends $t{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 Wt extends $t{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 Bt extends $t{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(y(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),c(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!y(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Tt(e,t):t}}))}}class Gt extends $t{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)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:mt(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:At(t)})))}}class Kt{static array(t){return(new zt).items(t)}static boolean(){return new Jt}static number(){return new Wt}static object(t){return(new Bt).keys(t)}static string(){return new Gt}test(t,...e){return!b(Kt.validate(t,...e).error)}testAsync(t,...e){return lt(this,void 0,void 0,(function*(){return!b((yield Kt.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 lt(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{ut as EventEmitter,$ as Hash,Ot as RandomString,Kt as Validation,$t as ValidationBase,Tt as allowed,S as cache,dt as camelCase,t as chunkArray,e as clamp,Y as colorPalette,vt as cutText,tt as dateFormat,at as debounce,gt as decodeHTML,Yt as denied,M as each,_t as encodeHTML,Ht as forEachAsync,C as forEachIndex,O as forEachKey,Pt as forEachSeries,A as forLoop,U as hexToRgb,D as hslToRgb,l as isArray,o as isBoolean,xt as isCharacter,c as isDef,bt as isDigit,m as isFloat,d as isFunction,v as isInteger,wt as isLetter,f as isNull,p as isNumber,y as isObject,g as isPlainObject,x as isScalar,b as isString,kt as isSymbol,w as isUndefined,s as lerp,ot as loop,mt as lowerCamelCase,I as map,Nt as mask,qt as maskMoney,Et as maskReverse,Ft as maskTest,Dt as merge,nt as msToString,ct as noop,ht as parseMs,jt as parseSize,r as random,St as randomCase,i as randomElement,n as rangeArray,E as rgbToHex,Z as rgbToHsl,q as shadeColor,h as shuffleArray,Ct as shuffleString,K as simpleID,Zt as sleep,Mt as stringReverse,F as tintColor,k as toArray,j as toBool,u as type,Q as uniqueID,At as upperCamelCase,R as uuidv4};
|
package/dist/index.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),l=t=>"null"!=n(t),u=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=t=>"string"==n(t),m=t=>void 0===t,d=t=>parseInt(null!=t?t:"0",16)||0,v=t=>("0"+t).slice(-2);class f{constructor(t){this.key=this.shuffle(f.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[f.dictionary.indexOf(t[s]),f.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return f.dictionary[(s?t-e+f.dictionary.length:t+e)%f.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}}f.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const g=t=>Math.random().toString(16).slice(t),y=()=>g(-3),_=()=>g(-4),x=()=>_()+_(),b=()=>x()+x(),w=t=>("0"+t.toString()).slice(-2),k={"%y":t=>w(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>w(t.getMonth()+1),"%d":t=>w(t.getDate()),"%H":t=>w(t.getHours()),"%M":t=>w(t.getMinutes()),"%S":t=>w(t.getSeconds())},S=36e5,C=24*S,j={ms:1,s:1e3,m:6e4,h:S,d:C,w:7*C,y:315576e5},O=["y","d","h","m","s","ms"];function A(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{l(r.next(t))}catch(t){n(t)}}function h(t){try{l(r.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}l((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const I=()=>{},M=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},E=(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},L=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),T=t=>L(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),U=T,N=/&(amp|apos|lt|gt|nbsp|quot);/g,F={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},q=t=>/^[\d]$/.test(t),H=t=>/^[A-Za-z]$/.test(t),D=t=>/^[^\w]$/.test(t),B=t=>r(t.split("")).join("");class P{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 P).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new P).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=B(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 P).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new P).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new P).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new P).upper()}}const R=t=>t.split("").reverse().join(""),Y=t=>L(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),z=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),J={"#":t=>t.value,0:t=>z(t,q),9:t=>z(t,q),A:t=>z(t,H),Z:t=>z(t,H),L:t=>z(t,H).toLowerCase(),U:t=>z(t,H).toUpperCase(),S:t=>z(t,D)},Z=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return m(n)||m(a)?"":(t=>J[t.key]?J[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Z(t)},$=(t,e)=>Z({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),V=(t,e)=>R($(R(t),R(e))),W=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),K=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=K(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return K(t,...e)};class G{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=l(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>l(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return A(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return A(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class Q extends G{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(a(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class X extends G{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:l(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class tt extends G{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>u(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?parseFloat(t):t})))}}class et extends G{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),l(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?W(e,t):t}}))}}class st extends G{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value: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:$(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?JSON.stringify(t):l(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:Y(t)})))}}class rt{static array(t){return(new Q).items(t)}static boolean(){return new X}static number(){return new tt}static object(t){return(new et).keys(t)}static string(){return new st}test(t,...e){return!p(rt.validate(t,...e).error)}testAsync(t,...e){return A(this,void 0,void 0,(function*(){return!p((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 A(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},t.Hash=f,t.RandomString=P,t.Validation=rt,t.ValidationBase=G,t.allowed=W,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=U,t.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},t.clamp=e,t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>k[e]?k[e](t):e.slice(1))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(N,((t,e)=>F[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>W(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?M(t,e,s):o(t)?E(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>A(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(I)})),t.forEachIndex=M,t.forEachKey=E,t.forEachSeries=(t,e,s)=>A(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(I)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>d(t))))||[]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=l,t.isDigit=q,t.isFloat=t=>u(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>u(t)&&!(t%1),t.isLetter=H,t.isNull=t=>null===t,t.isNumber=u,t.isObject=o,t.isPlainObject=c,t.isString=p,t.isSymbol=D,t.isUndefined=m,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>A(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=T,t.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},t.mask=$,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),V(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=V,t.maskTest=(t,e)=>t.length==$(t,e).length,t.merge=K,t.msToString=t=>{const e=[];return O.forEach((s=>{const r=j[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=I,t.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*j[s[2]]||0;return r},t.parseSize=t=>{if(u(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.randomElement=t=>t.length?t[s(0,t.length-1)]:null,t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=(t,e,s)=>"#"+v(t.toString(16))+v(e.toString(16))+v(s.toString(16)),t.shadeColor=(t,s)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>v(((t,s)=>e(d(t)+s,0,255).toString(16))(t,s)))),t.shuffleArray=r,t.shuffleString=B,t.simpleID=()=>y()+_(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=R,t.toArray=t=>a(t)?t:l(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>b()+b()+b()+b(),t.upperCamelCase=Y,t.uuidv4=()=>x()+"-"+_()+"-4"+y()+"-"+_()+"-"+(x()+_()),t}({});
|
|
7
|
+
var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),l=t=>"null"!=n(t),u=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=["boolean","number","string"],m=t=>"string"==n(t),d=t=>void 0===t,v=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},f=(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},g=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),y=t=>parseInt(null!=t?t:"0",16)||0,_=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(y))||[]},x=t=>("0"+t.toString(16)).slice(-2),b=(t,e,s)=>"#"+x(t)+x(e)+x(s),w=(t,s)=>{const r=_(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return b(i,n,a)},k=(t,s)=>{const r=_(t);s=e(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 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(),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=36e5,F=24*N,H={ms:1,s:1e3,m:6e4,h:N,d:F,w:7*F,y:315576e5},q=["y","d","h","m","s","ms"];function D(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{l(r.next(t))}catch(t){n(t)}}function h(t){try{l(r.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}l((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const P=()=>{},R=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),B=t=>R(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),Y=B,z=/&(amp|apos|lt|gt|nbsp|quot);/g,J={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},Z=t=>/^[\d]$/.test(t),$=t=>/^[A-Za-z]$/.test(t),V=t=>/^[^\w]$/.test(t),W=t=>r(t.split("")).join("");class K{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 K).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new K).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=W(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 K).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new K).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new K).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new K).upper()}}const G=t=>t.split("").reverse().join(""),Q=t=>R(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),X=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),tt={"#":t=>t.value,0:t=>X(t,Z),9:t=>X(t,Z),A:t=>X(t,$),Z:t=>X(t,$),L:t=>X(t,$).toLowerCase(),U:t=>X(t,$).toUpperCase(),S:t=>X(t,V)},et=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=>tt[t.key]?tt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+et(t)},st=(t,e)=>et({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),rt=(t,e)=>G(st(G(t),G(e))),it=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),nt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=nt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return nt(t,...e)};class at{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=l(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>l(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!m(this.validate(t).error)}testAsync(t){return D(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 D(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 at{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 lt extends at{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:l(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class ut extends at{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>u(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?parseFloat(t):t})))}}class ot extends at{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),l(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?it(e,t):t}}))}}class ct extends at{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)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:B(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:st(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?JSON.stringify(t):l(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:Q(t)})))}}class pt{static array(t){return(new ht).items(t)}static boolean(){return new lt}static number(){return new ut}static object(t){return(new ot).keys(t)}static string(){return new ct}test(t,...e){return!m(pt.validate(t,...e).error)}testAsync(t,...e){return D(this,void 0,void 0,(function*(){return!m((yield pt.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 D(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=K,t.Validation=pt,t.ValidationBase=at,t.allowed=it,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=Y,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)=>g(null!=e?e:S,(e=>0==e?t:e>0?k(t,e):w(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=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>U[t]?U[t](e):t.slice(1)))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(z,((t,e)=>J[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>it(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?v(t,e,s):o(t)?f(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>D(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(P)})),t.forEachIndex=v,t.forEachKey=f,t.forEachSeries=(t,e,s)=>D(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)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=_,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=l,t.isDigit=Z,t.isFloat=t=>u(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>u(t)&&!(t%1),t.isLetter=$,t.isNull=t=>null===t,t.isNumber=u,t.isObject=o,t.isPlainObject=c,t.isScalar=t=>-1!=p.indexOf(n(t)),t.isString=m,t.isSymbol=V,t.isUndefined=d,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>D(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=B,t.map=g,t.mask=st,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),rt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=rt,t.maskTest=(t,e)=>t.length==st(t,e).length,t.merge=nt,t.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(" ")},t.noop=P,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])*H[s[2]]||0;return r},t.parseSize=t=>{if(u(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.randomElement=t=>t.length?t[s(0,t.length-1)]:null,t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=b,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),l=a-h,u=(h+a)/2;return[60*C(l,a,r,i,n),j(l,u),u]},t.shadeColor=w,t.shuffleArray=r,t.shuffleString=W,t.simpleID=()=>A()+I(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=G,t.tintColor=k,t.toArray=t=>a(t)?t:l(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>T()+T()+T()+T(),t.upperCamelCase=Q,t.uuidv4=()=>E()+"-"+I()+"-4"+A()+"-"+I()+"-"+(E()+I()),t}({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/utils.js",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "JavaScript utility library for web and nodejs development",
|
|
5
5
|
"main": "./dist/index.cjs.js",
|
|
6
6
|
"unpkg": "./dist/index.min.js",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
49
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
50
|
-
"@types/node": "^
|
|
51
|
-
"eslint": "^9.
|
|
52
|
-
"rollup": "^4.
|
|
50
|
+
"@types/node": "^22.0.2",
|
|
51
|
+
"eslint": "^9.8.0",
|
|
52
|
+
"rollup": "^4.19.2",
|
|
53
53
|
"rollup-plugin-dts": "^6.1.1",
|
|
54
54
|
"tslib": "^2.6.3",
|
|
55
|
-
"typescript-eslint": "^8.0.0
|
|
55
|
+
"typescript-eslint": "^8.0.0"
|
|
56
56
|
}
|
|
57
57
|
}
|