@andrewcaires/utils.js 0.4.2 → 0.4.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 +3 -3
- package/dist/index.d.ts +72 -74
- package/dist/index.esm.js +3 -3
- package/dist/index.min.js +3 -3
- package/package.json +7 -7
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.4.
|
|
2
|
+
* @andrewcaires/utils.js v0.4.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) 2025 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","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),o=t=>"null"!=i(t),h=t=>"number"==i(t),u=t=>"function"==typeof t,l=t=>"object"==i(t),p=t=>l(t)&&t.constructor===Object,c=["boolean","number","string"],m=t=>"string"==typeof t,x=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},v=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},f=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),y=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(y))||[]},_=t=>("0"+t.toString(16)).slice(-2),b=(t,e,s)=>"#"+_(t)+_(e)+_(s),w=(e,s)=>{const r=g(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return b(i,n,a)},k=(e,s)=>{const r=g(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return b(i,n,a)},S={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},C=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,O=(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 M=t=>Math.random().toString(16).slice(t),A=()=>M(-3),E=()=>M(-4),I=()=>E()+E(),D=()=>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())},F=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>L[t]?L[t](e):t.slice(1)))),N=36e5,U=24*N,q={ms:1,s:1e3,m:6e4,h:N,d:U,w:7*U,y:315576e5},H=["y","d","h","m","s","ms"];function R(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{h(r.next(t))}catch(t){n(t)}}function o(t){try{h(r.throw(t))}catch(t){n(t)}}function h(t){t.done?i(t.value):function(t){return t instanceof s?t:new s((function(e){e(t)}))}(t.value).then(a,o)}h((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const P=t=>{var e;return 0==(null!==(e=null==t?void 0:t.length)&&void 0!==e?e:0)},B=["",0,"0",null,!1,void 0],Y=t=>-1==B.indexOf(t),z=()=>{},Z=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),$=t=>Z(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),J=$,V=/&(amp|apos|lt|gt|nbsp|quot);/g,W={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},K=t=>/^[\d]$/.test(t),G=t=>/^[A-Za-z]$/.test(t),Q=t=>/^[^\w]$/.test(t),X=t=>s(t.split("")).join("");class tt{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 tt).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new tt).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=X(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 tt).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new tt).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new tt).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new tt).upper()}}const et=t=>t.split("").reverse().join(""),st=t=>Z(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),rt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),it={"#":t=>t.value,0:t=>rt(t,K),9:t=>rt(t,K),A:t=>rt(t,G),Z:t=>rt(t,G),L:t=>rt(t,G).toLowerCase(),U:t=>rt(t,G).toUpperCase(),S:t=>rt(t,Q)},nt=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 x(n)||x(a)?"":(t=>it[t.key]?it[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+nt(t)},at=(t,e)=>nt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),ot=(t,e)=>et(at(et(t),et(e))),ht=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),ut=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(p(t)&&p(s))for(const e in s)p(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=ut(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return ut(t,...e)};class lt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=o(t);return{break:!e,value:e?t:u(this._value)?this._value():this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>o(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!m(this.validate(t).error)}testAsync(t){return R(this,void 0,void 0,(function*(){return!m((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return R(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class pt extends lt{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 ct extends lt{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:o(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class mt extends lt{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:F(this._format,new Date(t))})))}parse(){return this.unshift((t=>(m(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class xt extends lt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?parseFloat(t):t})))}}class dt extends lt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:a,value:h}=n.validate(t[n.name]);a&&e.push(a),o(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?ht(e,t):t}}))}}class vt extends lt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>m(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:$(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:at(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?JSON.stringify(t):o(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:st(t)})))}}class ft{static array(t){return(new pt).items(t)}static boolean(){return new ct}static date(){return new mt}static number(){return new xt}static object(t){return(new dt).keys(t)}static string(){return new vt}test(t,...e){return!m(ft.validate(t,...e).error)}testAsync(t,...e){return R(this,void 0,void 0,(function*(){return!m((yield ft.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return R(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},exports.Hash=j,exports.RandomString=tt,exports.Validation=ft,exports.ValidationBase=lt,exports.allowed=ht,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=J,exports.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},exports.clamp=t,exports.colorPalette=(t,e)=>f(null!=e?e:S,(e=>0==e?t:e>0?k(t,e):w(t,1- -1*e))),exports.countArray=t=>{const e={};return t.forEach((t=>{const s=t.toString();e[s]=(e[s]||0)+1})),e},exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=F,exports.dateRange=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(V,((t,e)=>W[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>ht(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?d(t,e,s):l(t)?v(t,e,s):t,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>R(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(z)})),exports.forEachIndex=d,exports.forEachKey=v,exports.forEachSeries=(t,e,s)=>R(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(z)})),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.isDate=t=>"date"==i(t),exports.isDef=o,exports.isDigit=K,exports.isEmpty=P,exports.isFalse=t=>!Y(t),exports.isFloat=t=>h(t)&&!!(t%1),exports.isFunction=u,exports.isInteger=t=>h(t)&&!(t%1),exports.isLetter=G,exports.isMap=t=>"map"==i(t),exports.isNotEmpty=t=>!P(t),exports.isNull=t=>null===t,exports.isNumber=h,exports.isObject=l,exports.isPlainObject=p,exports.isRegExp=t=>"regexp"==i(t),exports.isScalar=t=>-1!=c.indexOf(i(t)),exports.isSet=t=>"set"==i(t),exports.isSpecialChar=Q,exports.isString=m,exports.isSymbol=t=>"symbol"==typeof t,exports.isTrue=Y,exports.isUndefined=x,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>R(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=$,exports.map=f,exports.mask=at,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),ot(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=ot,exports.maskTest=(t,e)=>t.length==at(t,e).length,exports.merge=ut,exports.msToString=t=>{const e=[];return H.forEach((s=>{const r=q[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=z,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])*q[s[2]]||0;return r},exports.parseSize=t=>{if(h(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.randomElement=t=>t.length?t[e(0,t.length-1)]:null,exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=b,exports.rgbToHsl=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),a=Math.max(r,i,n),o=Math.min(r,i,n),h=a-o,u=(o+a)/2;return[60*C(h,a,r,i,n),O(h,u),u]},exports.shadeColor=w,exports.shuffleArray=s,exports.shuffleString=X,exports.simpleID=()=>A()+E(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=et,exports.tintColor=k,exports.toArray=t=>n(t)?t:o(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueArray=t=>t.filter(((t,e,s)=>s.indexOf(t)===e)),exports.uniqueID=()=>D()+D()+D()+D(),exports.upperCamelCase=st,exports.uuidv4=()=>I()+"-"+E()+"-4"+A()+"-"+E()+"-"+(I()+E());
|
|
7
|
+
"use strict";const e=(e,t,r)=>Math.max(t,Math.min(r,e)),t=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),r=e=>{for(let r=e.length-1;r>0;r--){const s=t(0,r);[e[r],e[s]]=[e[s],e[r]]}return e},s={},n=e=>null==e?"null":s[s.toString.call(e)]||"object";["Array","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((e=>s["[object "+e+"]"]=e.toLowerCase()));const a=e=>"array"==n(e),i=e=>"boolean"==n(e),o=e=>"null"!=n(e),u=e=>"number"==n(e),l=e=>u(e)&&!!(e%1),h=e=>"function"==typeof e,c=e=>u(e)&&!(e%1),p=e=>"object"==n(e),v=e=>p(e)&&e.constructor===Object,x=["boolean","number","string"],d=e=>"string"==typeof e,m=e=>void 0===e,f=[1,"1",!0,"true"],g=e=>-1!=f.indexOf(e),y=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},b=(e,t,r)=>{let s="";const n=Object.keys(e);for(let a=0;a<n.length&&(s=n[a],!1!==t.call(r,e[s],s,e));a++);return e},w=(e,t,r)=>Object.fromEntries(Object.entries(e).map((([s,n])=>[s,t.call(r,n,s,e)]))),S=e=>parseInt(null!=e?e:"0",16)||0,C=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map(S))||[]},k=e=>("0"+e.toString(16)).slice(-2),O=(e,t,r)=>"#"+k(e)+k(t)+k(r),j=(t,r)=>{const s=C(t);r=e(r,0,1);const[n,a,i]=s.map((e=>Math.round(e*r)));return O(n,a,i)},M=(t,r)=>{const s=C(t);r=e(r,0,1);const[n,a,i]=s.map((e=>Math.round(e+(255-e)*r)));return O(n,a,i)},A={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},E=(e,t,r,s,n)=>0===e?0:t===r?(s-n)/e%6:t===s?(n-r)/e+2:t===n?(r-s)/e+4:0,I=(e,t)=>0===e?0:e/(1-Math.abs(2*t-1));class _{constructor(e){this.key=this.shuffle(_.dictionary,e,!1)}encrypt(e){return this.shuffle(e,this.key,!1)}decrypt(e){return this.shuffle(e,this.key,!0)}index(e,t,r){return[_.dictionary.indexOf(e[r]),_.dictionary.indexOf(t[r%t.length])]}result(e,t,r){return _.dictionary[(r?e-t+_.dictionary.length:e+t)%_.dictionary.length]}shuffle(e,t,r){let s="";for(let n=0;n<e.length;n++){const[a,i]=this.index(e,t,n);s+=-1===a||-1===i?e[n]:this.result(a,i,r)}return s}}_.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const D=e=>Math.random().toString(16).slice(e),T=()=>D(-3),L=()=>D(-4),$=()=>L()+L(),F=()=>$()+$(),N=e=>("0"+e.toString()).slice(-2),q={"%y":e=>N(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>N(e.getMonth()+1),"%d":e=>N(e.getDate()),"%H":e=>N(e.getHours()),"%M":e=>N(e.getMinutes()),"%S":e=>N(e.getSeconds())},U=(e,t)=>(t=null!=t?t:new Date,e.replace(/%[yYmdHMS]/g,(e=>q[e]?q[e](t):e.slice(1)))),H=36e5,R=24*H,P={ms:1,s:1e3,m:6e4,h:H,d:R,w:7*R,y:315576e5},B=["y","d","h","m","s","ms"];function Y(e,t,r,s){return new(r||(r=Promise))((function(n,a){function i(e){try{u(s.next(e))}catch(e){a(e)}}function o(e){try{u(s.throw(e))}catch(e){a(e)}}function u(e){e.done?n(e.value):function(e){return e instanceof r?e:new r((function(t){t(e)}))}(e.value).then(i,o)}u((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const z=e=>{var t;return 0==(null!==(t=null==e?void 0:e.length)&&void 0!==t?t:0)},Z=["",0,"0",null,!1,void 0],J=e=>-1==Z.indexOf(e),V=()=>{},K=(e,t,r)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,null!=r?r:""),W=(e,t)=>K(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase()))),t),G=W,Q=/&(amp|apos|lt|gt|nbsp|quot);/g,X={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},ee=e=>/^[\d]$/.test(e),te=e=>/^[A-Za-z]$/.test(e),re=e=>/^[^\w]$/.test(e),se=e=>r(e.split("")).join("");class ne{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 ne).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new ne).lower()}make(e){let r="";this.hasSpace&&(r+=this._space),this.hasNumbers&&(r+=this._numbers),this.hasSymbols&&(r+=this._symbols),this.hasUpper&&!this.hasChars&&(r+=this._upper),this.hasLower&&!this.hasChars&&(r+=this._lower),this.hasChars&&(r+=this._chars);let s="";if(e&&r.length){r=se(r);for(let n=0;n<e;n++)s+=r.charAt(t(0,r.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new ne).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new ne).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new ne).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new ne).upper()}}const ae=e=>e.split("").reverse().join(""),ie=(e,t)=>K(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r))),t),oe=(e,t)=>t(e.value)?e.value:(e=>(e.maskIndex--,""))(e),ue={"#":e=>e.value,0:e=>oe(e,ee),9:e=>oe(e,ee),A:e=>oe(e,te),Z:e=>oe(e,te),L:e=>oe(e,te).toLowerCase(),U:e=>oe(e,te).toUpperCase(),S:e=>oe(e,re)},le=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:n}=e;e.key=t[r],e.value=s[n];const{key:a,value:i}=e;return m(a)||m(i)?"":(e=>ue[e.key]?ue[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+le(e)},he=(e,t)=>le({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),ce=(e,t)=>ae(he(ae(e),ae(t))),pe=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{}),ve=(e,...t)=>{if(!t.length)return e;const r=t.shift();if(v(e)&&v(r))for(const t in r)v(r[t])?(e[t]||(e=Object.assign(Object.assign({},e),{[t]:{}})),e[t]=ve(e[t],r[t])):e=Object.assign(Object.assign({},e),{[t]:r[t]});return ve(e,...t)};class xe{constructor(e){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[],this._type=e}empty(e){const t=e;return this.unshift((({value:e})=>{const r=o(e);return{break:!r,value:r?e:h(t)?t():t}}))}equal(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e==n?{value:e}:this.invalid(`.equal(${n})`,t)}))}error(e){return d(e)&&(this._error=e),this}filter(...e){const t=e;return this.unshift((({value:e})=>{const r=-1==t.indexOf(e);return{break:!r,value:r?e:void 0}}))}fn(e){return h(e)?e:()=>e}from(e){return this.source=e,this}invalid(e,t){return e=`[${this._type}]${e}`,{error:this._error?this._error:this.name?this.name+":"+e:e,message:t}}parse(){return this.unshift((({value:e})=>({value:e})))}push(e){return this.rules.push(e),this}required(e){return this.unshift((({value:t})=>o(t)?{value:t}:this.invalid(".required",e)))}test(e){return!d(this.validate(e).error)}testAsync(e){return Y(this,void 0,void 0,(function*(){return!d((yield this.validateAsync(e)).error)}))}unshift(e){return this.rules.unshift(e),this}validate(e,t){var r;if(this.custom.length)return this.invalid("[async]");let s={value:e};for(let e=0;e<this.rules.length;e++){if(s=this.rules[e]({value:s.value,context:t}),null===(r=s.error)||void 0===r?void 0:r.length)return Object.assign({error:s.error},s.message?{message:s.message}:{});if(s.break)return{value:s.value}}return{value:s.value}}validateAsync(e,t){return Y(this,void 0,void 0,(function*(){let r=this.validate(e,t);if(r.error)return{error:r.error};for(let e=0;e<this.custom.length;e++){if(r=yield this.custom[e]({value:r.value}),r.error)return{error:r.error};if(r.break)return{value:r.value}}return{value:r.value}}))}}class de extends xe{constructor(e){super("array"),this.push((({value:t})=>a(t)?{value:t}:this.invalid("",e)))}between(e,t,r){return this.min(e,r),this.max(t,r)}items(e,t){const r=e;return r?this.push((({value:e})=>{r.name="";for(let s=0;s<e.length;s++){const{error:n,value:a}=r.validate(e[s],e);if(n)return this.invalid("< "+n+"["+s+"] >",t);e[s]=a}return{value:e}})):this}length(e,t){const r=e;return this.push((({value:e})=>e.length==r?{value:e}:this.invalid(".length("+r+")",t)))}max(e,t){const r=e;return this.push((({value:e})=>e.length<=r?{value:e}:this.invalid(".max("+r+")",t)))}min(e,t){const r=e;return this.push((({value:e})=>e.length>=r?{value:e}:this.invalid(".min("+r+")",t)))}}class me extends xe{constructor(e){super("boolean"),this.push((({value:t})=>i(t)?{value:t}:this.invalid("",e)))}parse(){return this.unshift((({value:e})=>({value:o(e)?g(e):e})))}}class fe extends xe{constructor(e){super("date"),this.push((({value:t})=>d(t)&&t.length>=9&&!isNaN(new Date(t).getTime())?{value:t}:this.invalid("",e)))}format(e){const t=e;return this.push((({value:e})=>({value:U(t,new Date(e))})))}parse(){return this.unshift((({value:e})=>({value:new Date(d(e)&&e.length<=10?`${e} 12:00`:e)})))}}class ge extends xe{constructor(e){super("number"),this.push((({value:t})=>u(t)?{value:t}:this.invalid("",e)))}between(e,t,r){return this.min(e,r),this.max(t,r)}float(e){return this.push((({value:t})=>l(t)?{value:t}:this.invalid(".float",e)))}integer(e){return this.push((({value:t})=>c(t)?{value:t}:this.invalid(".integer",e)))}max(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e<=n?{value:e}:this.invalid(`.max(${n})`,t)}))}min(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e>=n?{value:e}:this.invalid(`.min(${n})`,t)}))}parse(){return this.unshift((({value:e})=>({value:o(e)?parseFloat(e.toString()):e})))}}class ye extends xe{constructor(e){super("object"),this.push((({value:t})=>p(t)?{value:t}:this.invalid("",e)))}keys(e,t){const r=e;return r?this.push((({value:e})=>{const s=[],n={};return Object.keys(r).forEach((a=>{var i;const u=r[a];u.name=null!==(i=u.source)&&void 0!==i?i:a;const{error:l,message:h,value:c}=u.validate(e[u.name],n);!t&&h&&(t=h),l&&s.push(l),o(c)&&(n[a]=c)})),s.length?this.invalid("{ "+s.join(", ")+" }",t):{value:n}})):this}parse(e){return this.unshift((({value:t})=>{if(!p(t))try{t=JSON.parse(t)}catch(t){return this.invalid("",e)}return{value:t}}))}}class be extends xe{constructor(e){super("string"),this.push((({value:t})=>d(t)?{value:t}:this.invalid("",e)))}between(e,t,r){return this.min(e,r),this.max(t,r)}email(e){const t=/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i;return this.push((({value:r})=>t.test(r)?{value:r}:this.invalid(".email",e)))}length(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e.length==n?{value:e}:this.invalid(`.length(${n})`,t)}))}lowerCase(){return this.push((({value:e})=>({value:e.toLowerCase()})))}lowerCamelCase(){return this.push((({value:e})=>({value:W(e)})))}max(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e.length<=n?{value:e}:this.invalid(`.max(${n})`,t)}))}mask(e){const t=this.fn(e);return this.push((({value:e,context:r})=>({value:he(t({value:e,context:r}),e)})))}min(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e.length>=n?{value:e}:this.invalid(`.min(${n})`,t)}))}parse(){return this.unshift((({value:e})=>({value:p(e)?JSON.stringify(e):o(e)?e.toString():e})))}required(e){return this.push((({value:t})=>t.length?{value:t}:this.invalid(".required",e)))}regex(e,t){return this.push((({value:r})=>e.test(r)?{value:r}:this.invalid(".regex",t)))}replace(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>({value:e.replace(r({value:e,context:s}),((...e)=>h(t)?t(...e):t))})))}trim(){return this.push((({value:e})=>({value:e.trim()})))}upperCase(){return this.push((({value:e})=>({value:e.toUpperCase()})))}upperCamelCase(){return this.push((({value:e})=>({value:ie(e)})))}}class we{static array(e,t){return new de(t).items(e)}static boolean(e){return new me(e)}static date(e){return new fe(e)}static number(e){return new ge(e)}static object(e,t){return new ye(t).keys(e)}static ref(e,t){return({context:r})=>r&&o(r[e])?r[e]:t}static string(e){return new be(e)}test(e,...t){return!d(we.validate(e,...t).error)}testAsync(e,...t){return Y(this,void 0,void 0,(function*(){return!d((yield we.validateAsync(e,...t)).error)}))}static validate(e,...t){let r={value:e};for(let e=0;e<t.length;e++)if(r=t[e].validate(r.value),r.error)return{value:r.value,error:r.error};return{value:r.value}}static validateAsync(e,...t){return Y(this,void 0,void 0,(function*(){let r={value:e};for(let e=0;e<t.length;e++)if(r=yield t[e].validateAsync(r.value),r.error)return{value:r.value,error:r.error};return{value:r.value}}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(e,t){const r=this.events.get(e);return r?r.push(t):this.events.set(e,[t]),{dispose:()=>this.off(e,t)}}once(e,t){const r=s=>{t(s),this.off(e,r)};return this.on(e,r),{dispose:()=>this.off(e,r)}}off(e,t){const r=this.events.get(e);if(t){if(r){const e=r.indexOf(t);e>=0&&r.splice(e,1)}}else this.events.set(e,[])}emit(e,t){const r=this.events.get(e);r&&r.forEach((e=>e(t)))}},exports.Hash=_,exports.RandomString=ne,exports.Validation=we,exports.ValidationBase=xe,exports.allowed=pe,exports.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},exports.camelCase=G,exports.chunkArray=(e,t)=>{const r=[];for(let s=0;s<e.length;s+=t)r.push(e.slice(s,s+t));return r},exports.clamp=e,exports.colorPalette=(e,t)=>w(null!=t?t:A,(t=>0==t?e:t>0?M(e,t):j(e,1- -1*t))),exports.countArray=e=>{const t={};return e.forEach((e=>{const r=e.toString();t[r]=(t[r]||0)+1})),t},exports.cutText=(e,t)=>{if(e.length>t){const r=e.substring(0,t).split(" ");return r.pop(),r.join(" ")+"..."}return e},exports.dateFormat=U,exports.dateRange=(e,t)=>{const r=new Date(e),s=[];for(;e<t;)r.setDate(r.getDate()+1),s.push(new Date(r));return s},exports.debounce=(e,t)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...n){s(),r=setTimeout((()=>e(...n)),t)}}},exports.decodeHTML=e=>e.replace(Q,((e,t)=>X[t])).replace(/&#(\d+);/gi,((e,t)=>String.fromCharCode(parseInt(t,10)))),exports.denied=(e,t)=>pe(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,t,r)=>a(e)?y(e,t,r):p(e)?b(e,t,r):e,exports.encodeHTML=e=>e.replace(/[^0-9A-Za-z ]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>Y(void 0,void 0,void 0,(function*(){yield Promise.allSettled(e.map(((s,n)=>t.call(r,s,n,e)))).catch(V)})),exports.forEachIndex=y,exports.forEachKey=b,exports.forEachSeries=(e,t,r)=>Y(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e).catch(V)})),exports.forLoop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.hexToRgb=C,exports.hslToRgb=(e,t,r)=>{e/=60;const s=(1-Math.abs(2*r-1))*t,n=s*(1-Math.abs(e%2-1));let a=[0,0,0];e<=1?a=[s,n,0]:e<=2?a=[n,s,0]:e<=3?a=[0,s,n]:e<=4?a=[0,n,s]:e<=5?a=[n,0,s]:e<=6&&(a=[s,0,n]);const i=r-.5*s;return[Math.round(255*(a[0]+i)),Math.round(255*(a[1]+i)),Math.round(255*(a[2]+i))]},exports.isArray=a,exports.isBoolean=i,exports.isCharacter=e=>/^[\w]$/.test(e),exports.isDate=e=>"date"==n(e),exports.isDef=o,exports.isDigit=ee,exports.isEmpty=z,exports.isFalse=e=>!J(e),exports.isFloat=l,exports.isFunction=h,exports.isInteger=c,exports.isLetter=te,exports.isMap=e=>"map"==n(e),exports.isNotEmpty=e=>!z(e),exports.isNull=e=>null===e,exports.isNumber=u,exports.isObject=p,exports.isPlainObject=v,exports.isRegExp=e=>"regexp"==n(e),exports.isScalar=e=>-1!=x.indexOf(n(e)),exports.isSet=e=>"set"==n(e),exports.isSpecialChar=re,exports.isString=d,exports.isSymbol=e=>"symbol"==typeof e,exports.isTrue=J,exports.isUndefined=m,exports.lerp=(t,r,s)=>t+(r-t)*e(s,0,1),exports.loop=(e,t)=>{let r=!1;const s=()=>Y(void 0,void 0,void 0,(function*(){r&&(yield e(),setTimeout(s,t))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},exports.lowerCamelCase=W,exports.map=w,exports.mask=he,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\D/g,"")).toString(),ce(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=ce,exports.maskTest=(e,t)=>e.length==he(e,t).length,exports.merge=ve,exports.msToString=e=>{const t=[];return B.forEach((r=>{const s=P[r],n=(e-e%s)/s;n&&(e-=n*s,t.push(n.toString()+r))})),t.join(" ")},exports.noop=V,exports.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*P[r[2]]||0;return s},exports.parseSize=e=>{if(u(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},exports.random=t,exports.randomCase=e=>e.split("").map((e=>Math.random()<.5?e.toUpperCase():e.toLowerCase())).join(""),exports.randomElement=e=>e.length?e[t(0,e.length-1)]:null,exports.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),exports.rgbToHex=O,exports.rgbToHsl=(e,t,r)=>{const[s,n,a]=((...e)=>e.map((e=>e/255)))(e,t,r),i=Math.max(s,n,a),o=Math.min(s,n,a),u=i-o,l=(o+i)/2;return[60*E(u,i,s,n,a),I(u,l),l]},exports.shadeColor=j,exports.shuffleArray=r,exports.shuffleString=se,exports.simpleID=()=>T()+L(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=ae,exports.tintColor=M,exports.toArray=e=>a(e)?e:o(e)?[e]:[],exports.toBool=g,exports.type=n,exports.uniqueArray=e=>e.filter(((e,t,r)=>r.indexOf(e)===t)),exports.uniqueID=()=>F()+F()+F()+F(),exports.upperCamelCase=ie,exports.uuidv4=()=>$()+"-"+L()+"-4"+T()+"-"+L()+"-"+($()+L());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.4.
|
|
2
|
+
* @andrewcaires/utils.js v0.4.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) 2025 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
7
|
declare const chunkArray: <T>(arr: Array<T>, length: number) => Array<Array<T>>;
|
|
@@ -12,7 +12,7 @@ declare const isBoolean: (test: any) => test is boolean;
|
|
|
12
12
|
|
|
13
13
|
declare const isDate: (test: any) => test is Date;
|
|
14
14
|
|
|
15
|
-
declare const isDef: (test: any) => test is
|
|
15
|
+
declare const isDef: <T = any>(test: any) => test is T;
|
|
16
16
|
|
|
17
17
|
declare const isFloat: (test: any) => test is number;
|
|
18
18
|
|
|
@@ -81,7 +81,7 @@ declare const toArray: (value: any) => Array<any>;
|
|
|
81
81
|
|
|
82
82
|
declare const uniqueArray: <T>(arr: Array<T>) => Array<T>;
|
|
83
83
|
|
|
84
|
-
declare const toBool: (value:
|
|
84
|
+
declare const toBool: (value: any) => boolean;
|
|
85
85
|
|
|
86
86
|
type CacheCallback = (key: any) => TypeObject<any, string | symbol>;
|
|
87
87
|
|
|
@@ -211,7 +211,7 @@ declare const forEachSeries: <T>(array: Array<T>, callback: ForEachCallback<T>,
|
|
|
211
211
|
|
|
212
212
|
declare const sleep: (ms: number) => Promise<void>;
|
|
213
213
|
|
|
214
|
-
declare const camelCase: (str: string) => string;
|
|
214
|
+
declare const camelCase: (str: string, space?: string) => string;
|
|
215
215
|
|
|
216
216
|
declare const cutText: (str: string, length: number) => string;
|
|
217
217
|
|
|
@@ -227,7 +227,7 @@ declare const isLetter: (str: string) => boolean;
|
|
|
227
227
|
|
|
228
228
|
declare const isSpecialChar: (str: string) => boolean;
|
|
229
229
|
|
|
230
|
-
declare const lowerCamelCase: (str: string) => string;
|
|
230
|
+
declare const lowerCamelCase: (str: string, space?: string) => string;
|
|
231
231
|
|
|
232
232
|
declare const parseSize: (size: number | string) => string;
|
|
233
233
|
|
|
@@ -265,121 +265,119 @@ declare const shuffleString: (str: string) => string;
|
|
|
265
265
|
|
|
266
266
|
declare const stringReverse: (str: string) => string;
|
|
267
267
|
|
|
268
|
-
declare const upperCamelCase: (str: string) => string;
|
|
268
|
+
declare const upperCamelCase: (str: string, space?: string) => string;
|
|
269
269
|
|
|
270
|
-
|
|
271
|
-
break?: boolean;
|
|
272
|
-
error?: string;
|
|
273
|
-
value?: T;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
type ValidationCallback = (value: any) => IValidationResult;
|
|
270
|
+
type ValidationCallback<T = any> = (value: IValidationInput<T>) => IValidationOutput<T>;
|
|
277
271
|
|
|
278
272
|
declare class ValidationBase<T = any> {
|
|
279
273
|
name: string;
|
|
280
274
|
source?: string;
|
|
281
|
-
private _value;
|
|
282
275
|
private _error;
|
|
283
|
-
private
|
|
284
|
-
protected _type: string;
|
|
276
|
+
private _type;
|
|
285
277
|
private rules;
|
|
286
278
|
private custom;
|
|
279
|
+
constructor(type: string);
|
|
287
280
|
empty(value?: T | TypeCallbackFunction<T>): this;
|
|
288
|
-
|
|
281
|
+
equal(value: T | IValidationFunction<T>, message?: string): this;
|
|
282
|
+
error(value?: string): this;
|
|
289
283
|
filter(...values: Array<any>): this;
|
|
284
|
+
protected fn<T = any>(value: T | TypeCallbackFunction<T>): TypeCallbackFunction<T>;
|
|
290
285
|
from(value: string): this;
|
|
291
|
-
protected invalid(
|
|
286
|
+
protected invalid(error: string, message?: string): IValidationOutput<T>;
|
|
292
287
|
parse(): this;
|
|
293
|
-
push(callback: ValidationCallback): this;
|
|
294
|
-
required(): this;
|
|
288
|
+
push(callback: ValidationCallback<T>): this;
|
|
289
|
+
required(message?: string): this;
|
|
295
290
|
test(value?: any): boolean;
|
|
296
291
|
testAsync(value?: any): Promise<boolean>;
|
|
297
|
-
unshift(callback: ValidationCallback): this;
|
|
298
|
-
validate(value?: any):
|
|
299
|
-
validateAsync(value?: any): Promise<
|
|
292
|
+
unshift(callback: ValidationCallback<T>): this;
|
|
293
|
+
validate(value?: any, context?: any): IValidationOutput<T>;
|
|
294
|
+
validateAsync(value?: any, context?: any): Promise<IValidationOutput<T>>;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
interface IValidationOutput<T = any> {
|
|
298
|
+
break?: boolean;
|
|
299
|
+
context?: any;
|
|
300
|
+
error?: string;
|
|
301
|
+
message?: string;
|
|
302
|
+
self?: ValidationBase<T>;
|
|
303
|
+
value?: T;
|
|
300
304
|
}
|
|
301
305
|
|
|
306
|
+
interface IValidationInput<T = any> extends IValidationOutput<T> {
|
|
307
|
+
value: T;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
type IValidationFunction<T> = (value: IValidationInput) => T;
|
|
311
|
+
|
|
302
312
|
declare class ValidationArray extends ValidationBase<Array<any>> {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
between(min: number, max: number): this;
|
|
310
|
-
items(value?: ValidationBase): this;
|
|
311
|
-
length(value: number): this;
|
|
312
|
-
max(value: number): this;
|
|
313
|
-
min(value: number): this;
|
|
313
|
+
constructor(message?: string);
|
|
314
|
+
between(min: number, max: number, message?: string): this;
|
|
315
|
+
items(value?: ValidationBase, message?: string): this;
|
|
316
|
+
length(value: number, message?: string): this;
|
|
317
|
+
max(value: number, message?: string): this;
|
|
318
|
+
min(value: number, message?: string): this;
|
|
314
319
|
}
|
|
315
320
|
|
|
316
321
|
declare class ValidationBoolean extends ValidationBase<boolean> {
|
|
317
|
-
|
|
318
|
-
constructor();
|
|
322
|
+
constructor(message?: string);
|
|
319
323
|
parse(): this;
|
|
320
324
|
}
|
|
321
325
|
|
|
322
326
|
declare class ValidationDate extends ValidationBase<Date | string> {
|
|
323
|
-
|
|
324
|
-
protected _format: string;
|
|
325
|
-
constructor();
|
|
327
|
+
constructor(message?: string);
|
|
326
328
|
format(value: string): this;
|
|
327
329
|
parse(): this;
|
|
328
330
|
}
|
|
329
331
|
|
|
330
332
|
declare class ValidationNumber extends ValidationBase<number> {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
min(value: number): this;
|
|
333
|
+
constructor(message?: string);
|
|
334
|
+
between(min: number | TypeCallbackFunction, max: number | TypeCallbackFunction, message?: string): this;
|
|
335
|
+
float(message?: string): this;
|
|
336
|
+
integer(message?: string): this;
|
|
337
|
+
max(value: number | TypeCallbackFunction, message?: string): this;
|
|
338
|
+
min(value: number | TypeCallbackFunction, message?: string): this;
|
|
338
339
|
parse(): this;
|
|
339
340
|
}
|
|
340
341
|
|
|
341
342
|
declare class ValidationObject extends ValidationBase<TypeObjectAny> {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
keys(value?: TypeObject<ValidationBase>): this;
|
|
346
|
-
parse(): this;
|
|
343
|
+
constructor(message?: string);
|
|
344
|
+
keys(value?: TypeObject<ValidationBase>, message?: string): this;
|
|
345
|
+
parse(message?: string): this;
|
|
347
346
|
}
|
|
348
347
|
|
|
349
348
|
declare class ValidationString extends ValidationBase<string> {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
constructor();
|
|
355
|
-
between(min: number, max: number): this;
|
|
356
|
-
email(): this;
|
|
357
|
-
length(value: number): this;
|
|
349
|
+
constructor(message?: string);
|
|
350
|
+
between<T extends number>(min: T | IValidationFunction<T>, max: number | IValidationFunction<T>, message?: string): this;
|
|
351
|
+
email(message?: string): this;
|
|
352
|
+
length<T extends number>(value: T | IValidationFunction<T>, message?: string): this;
|
|
358
353
|
lowerCase(): this;
|
|
359
354
|
lowerCamelCase(): this;
|
|
360
|
-
max(value:
|
|
361
|
-
mask(
|
|
362
|
-
min(value:
|
|
355
|
+
max<T extends number>(value: T | IValidationFunction<T>, message?: string): this;
|
|
356
|
+
mask<T extends string>(value: T | IValidationFunction<T>): this;
|
|
357
|
+
min<T extends number>(value: T | IValidationFunction<T>, message?: string): this;
|
|
363
358
|
parse(): this;
|
|
364
|
-
required(): this;
|
|
359
|
+
required(message?: string): this;
|
|
360
|
+
regex(regex: RegExp, message?: string): this;
|
|
361
|
+
replace<T extends string>(value: T | RegExp | IValidationFunction<T>, text: T | TypeCallbackFunction<T>): this;
|
|
365
362
|
trim(): this;
|
|
366
363
|
upperCase(): this;
|
|
367
364
|
upperCamelCase(): this;
|
|
368
365
|
}
|
|
369
366
|
|
|
370
367
|
declare class Validation {
|
|
371
|
-
static array(value?: ValidationBase): ValidationArray;
|
|
372
|
-
static boolean(): ValidationBoolean;
|
|
373
|
-
static date(): ValidationDate;
|
|
374
|
-
static number(): ValidationNumber;
|
|
375
|
-
static object(value?: TypeObject<ValidationBase
|
|
376
|
-
static string
|
|
368
|
+
static array(value?: ValidationBase, message?: string): ValidationArray;
|
|
369
|
+
static boolean(message?: string): ValidationBoolean;
|
|
370
|
+
static date(message?: string): ValidationDate;
|
|
371
|
+
static number(message?: string): ValidationNumber;
|
|
372
|
+
static object(value?: TypeObject<ValidationBase>, message?: string): ValidationObject;
|
|
373
|
+
static ref(ref: string | number, value?: any): TypeCallbackFunction;
|
|
374
|
+
static string(message?: string): ValidationString;
|
|
377
375
|
test(value?: any, ...rules: Array<ValidationBase>): boolean;
|
|
378
376
|
testAsync(value?: any, ...rules: Array<ValidationBase>): Promise<boolean>;
|
|
379
|
-
static validate(value?: any, ...rules: Array<ValidationBase>):
|
|
380
|
-
static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<
|
|
377
|
+
static validate(value?: any, ...rules: Array<ValidationBase>): IValidationOutput;
|
|
378
|
+
static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<IValidationOutput>;
|
|
381
379
|
}
|
|
382
380
|
|
|
383
|
-
type ValidationCallbackAsync = (value:
|
|
381
|
+
type ValidationCallbackAsync<T = any> = (value: IValidationInput<T>) => Promise<IValidationOutput<T>>;
|
|
384
382
|
|
|
385
|
-
export { type CacheCallback, type EachCallback, type EachValue, type EventCallback, EventEmitter, type EventList, type ForEachCallback, Hash, type IDebounceCallbacks, type IEventDisposable, type ILoopCallbacks, type
|
|
383
|
+
export { type CacheCallback, type EachCallback, type EachValue, type EventCallback, EventEmitter, type EventList, type ForEachCallback, Hash, type IDebounceCallbacks, type IEventDisposable, type ILoopCallbacks, type IValidationFunction, type IValidationInput, type IValidationOutput, RandomString, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeKey, 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, countArray, cutText, dateFormat, dateRange, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hexToRgb, hslToRgb, isArray, isBoolean, isCharacter, isDate, isDef, isDigit, isEmpty, isFalse, isFloat, isFunction, isInteger, isLetter, isMap, isNotEmpty, isNull, isNumber, isObject, isPlainObject, isRegExp, isScalar, isSet, isSpecialChar, isString, isSymbol, isTrue, 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, uniqueArray, uniqueID, upperCamelCase, uuidv4 };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.4.
|
|
2
|
+
* @andrewcaires/utils.js v0.4.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) 2025 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=>{const e={};return t.forEach((t=>{const s=t.toString();e[s]=(e[s]||0)+1})),e},s=(t,e,s)=>Math.max(e,Math.min(s,t)),r=(t,e,r)=>t+(e-t)*s(r,0,1),i=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),n=t=>t.length?t[i(0,t.length-1)]:null,h=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),a=t=>{for(let e=t.length-1;e>0;e--){const s=i(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},u={},l=t=>null==t?"null":u[u.toString.call(t)]||"object";["Array","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((t=>u["[object "+t+"]"]=t.toLowerCase()));const o=t=>"array"==l(t),c=t=>"boolean"==l(t),p=t=>"date"==l(t),m=t=>"null"!=l(t),d=t=>"number"==l(t),v=t=>d(t)&&!!(t%1),f=t=>"function"==typeof t,y=t=>d(t)&&!(t%1),g=t=>"map"==l(t),_=t=>null===t,x=t=>"object"==l(t),b=t=>x(t)&&t.constructor===Object,w=t=>"regexp"==l(t),k=["boolean","number","string"],S=t=>-1!=k.indexOf(l(t)),O=t=>"set"==l(t),j=t=>"string"==typeof t,C=t=>"symbol"==typeof t,M=t=>void 0===t,A=t=>o(t)?t:m(t)?[t]:[],I=t=>t.filter(((t,e,s)=>s.indexOf(t)===e)),D=t=>"true"===t||"1"===t,E=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},L=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},U=(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},N=(t,e,s)=>o(t)?L(t,e,s):x(t)?U(t,e,s):t,F=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},q=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),T=t=>parseInt(null!=t?t:"0",16)||0,Y=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(T))||[]},H=t=>("0"+t.toString(16)).slice(-2),P=(t,e,s)=>"#"+H(t)+H(e)+H(s),Z=(t,e)=>{const r=Y(t);e=s(e,0,1);const[i,n,h]=r.map((t=>Math.round(t*e)));return P(i,n,h)},$=(t,e)=>{const r=Y(t);e=s(e,0,1);const[i,n,h]=r.map((t=>Math.round(t+(255-t)*e)));return P(i,n,h)},z={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},J=(t,e)=>q(null!=e?e:z,(e=>0==e?t:e>0?$(t,e):Z(t,1- -1*e))),W=(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))]},B=(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,R=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1)),G=(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*B(u,h,r,i,n),R(u,l),l]};class K{constructor(t){this.key=this.shuffle(K.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[K.dictionary.indexOf(t[s]),K.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return K.dictionary[(s?t-e+K.dictionary.length:t+e)%K.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}}K.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const Q=t=>Math.random().toString(16).slice(t),V=()=>Q(-3),X=()=>Q(-4),tt=()=>X()+X(),et=()=>tt()+tt(),st=()=>V()+X(),rt=()=>et()+et()+et()+et(),it=()=>tt()+"-"+X()+"-4"+V()+"-"+X()+"-"+(tt()+X()),nt=t=>("0"+t.toString()).slice(-2),ht={"%y":t=>nt(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>nt(t.getMonth()+1),"%d":t=>nt(t.getDate()),"%H":t=>nt(t.getHours()),"%M":t=>nt(t.getMinutes()),"%S":t=>nt(t.getSeconds())},at=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>ht[t]?ht[t](e):t.slice(1)))),ut=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},lt=36e5,ot=24*lt,ct={ms:1,s:1e3,m:6e4,h:lt,d:ot,w:7*ot,y:315576e5},pt=["y","d","h","m","s","ms"],mt=t=>{const e=[];return pt.forEach((s=>{const r=ct[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},dt=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])*ct[s[2]]||0;return r},vt=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class ft{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 yt(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){t.done?i(t.value):function(t){return t instanceof s?t:new s((function(e){e(t)}))}(t.value).then(h,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const gt=(t,e)=>{let s=!1;const r=()=>yt(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},_t=t=>{var e;return 0==(null!==(e=null==t?void 0:t.length)&&void 0!==e?e:0)},xt=["",0,"0",null,!1,void 0],bt=t=>-1==xt.indexOf(t),wt=t=>!bt(t),kt=t=>!_t(t),St=()=>{},Ot=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),jt=t=>Ot(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),Ct=jt,Mt=(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,It={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},Dt=t=>t.replace(At,((t,e)=>It[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),Et=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),Lt=t=>/^[\w]$/.test(t),Ut=t=>/^[\d]$/.test(t),Nt=t=>/^[A-Za-z]$/.test(t),Ft=t=>/^[^\w]$/.test(t),qt=t=>{if(d(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"},Tt=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),Yt=t=>a(t.split("")).join("");class Ht{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 Ht).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Ht).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=Yt(e);for(let r=0;r<t;r++)s+=e.charAt(i(0,e.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new Ht).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Ht).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Ht).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Ht).upper()}}const Pt=t=>t.split("").reverse().join(""),Zt=t=>Ot(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),$t=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),zt={"#":t=>t.value,0:t=>$t(t,Ut),9:t=>$t(t,Ut),A:t=>$t(t,Nt),Z:t=>$t(t,Nt),L:t=>$t(t,Nt).toLowerCase(),U:t=>$t(t,Nt).toUpperCase(),S:t=>$t(t,Ft)},Jt=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:h}=t;return M(n)||M(h)?"":(t=>zt[t.key]?zt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Jt(t)},Wt=(t,e)=>Jt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),Bt=(t,e)=>Pt(Wt(Pt(t),Pt(e))),Rt=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),Bt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),Gt=(t,e)=>t.length==Wt(t,e).length,Kt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Qt=(t,e)=>Kt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Vt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(b(t)&&b(s))for(const e in s)b(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Vt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Vt(t,...e)},Xt=(t,e,s)=>yt(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(St)})),te=(t,e,s)=>yt(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(St)})),ee=t=>new Promise((e=>setTimeout(e,t)));class se{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=m(t);return{break:!e,value:e?t:f(this._value)?this._value():this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>m(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!j(this.validate(t).error)}testAsync(t){return yt(this,void 0,void 0,(function*(){return!j((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return yt(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 re extends se{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(o(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 ie extends se{constructor(){super(),this._type="boolean",this.push((t=>c(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:m(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class ne extends se{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:at(this._format,new Date(t))})))}parse(){return this.unshift((t=>(j(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class he extends se{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>d(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:m(t)?parseFloat(t):t})))}}class ae extends se{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(x(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:h,value:a}=n.validate(t[n.name]);h&&e.push(h),m(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(!x(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Kt(e,t):t}}))}}class ue extends se{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>j(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:jt(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:Wt(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:x(t)?JSON.stringify(t):m(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:Zt(t)})))}}class le{static array(t){return(new re).items(t)}static boolean(){return new ie}static date(){return new ne}static number(){return new he}static object(t){return(new ae).keys(t)}static string(){return new ue}test(t,...e){return!j(le.validate(t,...e).error)}testAsync(t,...e){return yt(this,void 0,void 0,(function*(){return!j((yield le.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 yt(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{ft as EventEmitter,K as Hash,Ht as RandomString,le as Validation,se as ValidationBase,Kt as allowed,E as cache,Ct as camelCase,t as chunkArray,s as clamp,J as colorPalette,e as countArray,Mt as cutText,at as dateFormat,ut as dateRange,vt as debounce,Dt as decodeHTML,Qt as denied,N as each,Et as encodeHTML,Xt as forEachAsync,L as forEachIndex,U as forEachKey,te as forEachSeries,F as forLoop,Y as hexToRgb,W as hslToRgb,o as isArray,c as isBoolean,Lt as isCharacter,p as isDate,m as isDef,Ut as isDigit,_t as isEmpty,wt as isFalse,v as isFloat,f as isFunction,y as isInteger,Nt as isLetter,g as isMap,kt as isNotEmpty,_ as isNull,d as isNumber,x as isObject,b as isPlainObject,w as isRegExp,S as isScalar,O as isSet,Ft as isSpecialChar,j as isString,C as isSymbol,bt as isTrue,M as isUndefined,r as lerp,gt as loop,jt as lowerCamelCase,q as map,Wt as mask,Rt as maskMoney,Bt as maskReverse,Gt as maskTest,Vt as merge,mt as msToString,St as noop,dt as parseMs,qt as parseSize,i as random,Tt as randomCase,n as randomElement,h as rangeArray,P as rgbToHex,G as rgbToHsl,Z as shadeColor,a as shuffleArray,Yt as shuffleString,st as simpleID,ee as sleep,Pt as stringReverse,$ as tintColor,A as toArray,D as toBool,l as type,I as uniqueArray,rt as uniqueID,Zt as upperCamelCase,it as uuidv4};
|
|
7
|
+
const t=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},e=t=>{const e={};return t.forEach((t=>{const r=t.toString();e[r]=(e[r]||0)+1})),e},r=(t,e,r)=>Math.max(e,Math.min(r,t)),s=(t,e,s)=>t+(e-t)*r(s,0,1),n=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),i=t=>t.length?t[n(0,t.length-1)]:null,a=(t,e,r=1)=>Array.from({length:(e-t)/r+1},((e,s)=>t+s*r)),u=t=>{for(let e=t.length-1;e>0;e--){const r=n(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},l={},o=t=>null==t?"null":l[l.toString.call(t)]||"object";["Array","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((t=>l["[object "+t+"]"]=t.toLowerCase()));const h=t=>"array"==o(t),c=t=>"boolean"==o(t),v=t=>"date"==o(t),p=t=>"null"!=o(t),d=t=>"number"==o(t),f=t=>d(t)&&!!(t%1),m=t=>"function"==typeof t,g=t=>d(t)&&!(t%1),x=t=>"map"==o(t),y=t=>null===t,b=t=>"object"==o(t),w=t=>b(t)&&t.constructor===Object,S=t=>"regexp"==o(t),k=["boolean","number","string"],O=t=>-1!=k.indexOf(o(t)),j=t=>"set"==o(t),C=t=>"string"==typeof t,M=t=>"symbol"==typeof t,_=t=>void 0===t,A=t=>h(t)?t:p(t)?[t]:[],I=t=>t.filter(((t,e,r)=>r.indexOf(t)===e)),$=[1,"1",!0,"true"],D=t=>-1!=$.indexOf(t),E=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},L=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},U=(t,e,r)=>{let s="";const n=Object.keys(t);for(let i=0;i<n.length&&(s=n[i],!1!==e.call(r,t[s],s,t));i++);return t},q=(t,e,r)=>h(t)?L(t,e,r):b(t)?U(t,e,r):t,N=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},F=(t,e,r)=>Object.fromEntries(Object.entries(t).map((([s,n])=>[s,e.call(r,n,s,t)]))),T=t=>parseInt(null!=t?t:"0",16)||0,Y=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(T))||[]},H=t=>("0"+t.toString(16)).slice(-2),P=(t,e,r)=>"#"+H(t)+H(e)+H(r),Z=(t,e)=>{const s=Y(t);e=r(e,0,1);const[n,i,a]=s.map((t=>Math.round(t*e)));return P(n,i,a)},z=(t,e)=>{const s=Y(t);e=r(e,0,1);const[n,i,a]=s.map((t=>Math.round(t+(255-t)*e)));return P(n,i,a)},J={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},B=(t,e)=>F(null!=e?e:J,(e=>0==e?t:e>0?z(t,e):Z(t,1- -1*e))),R=(t,e,r)=>{t/=60;const s=(1-Math.abs(2*r-1))*e,n=s*(1-Math.abs(t%2-1));let i=[0,0,0];t<=1?i=[s,n,0]:t<=2?i=[n,s,0]:t<=3?i=[0,s,n]:t<=4?i=[0,n,s]:t<=5?i=[n,0,s]:t<=6&&(i=[s,0,n]);const a=r-.5*s;return[Math.round(255*(i[0]+a)),Math.round(255*(i[1]+a)),Math.round(255*(i[2]+a))]},W=(t,e,r,s,n)=>0===t?0:e===r?(s-n)/t%6:e===s?(n-r)/t+2:e===n?(r-s)/t+4:0,G=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1)),K=(t,e,r)=>{const[s,n,i]=((...t)=>t.map((t=>t/255)))(t,e,r),a=Math.max(s,n,i),u=Math.min(s,n,i),l=a-u,o=(u+a)/2;return[60*W(l,a,s,n,i),G(l,o),o]};class Q{constructor(t){this.key=this.shuffle(Q.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,r){return[Q.dictionary.indexOf(t[r]),Q.dictionary.indexOf(e[r%e.length])]}result(t,e,r){return Q.dictionary[(r?t-e+Q.dictionary.length:t+e)%Q.dictionary.length]}shuffle(t,e,r){let s="";for(let n=0;n<t.length;n++){const[i,a]=this.index(t,e,n);s+=-1===i||-1===a?t[n]:this.result(i,a,r)}return s}}Q.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const V=t=>Math.random().toString(16).slice(t),X=()=>V(-3),tt=()=>V(-4),et=()=>tt()+tt(),rt=()=>et()+et(),st=()=>X()+tt(),nt=()=>rt()+rt()+rt()+rt(),it=()=>et()+"-"+tt()+"-4"+X()+"-"+tt()+"-"+(et()+tt()),at=t=>("0"+t.toString()).slice(-2),ut={"%y":t=>at(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>at(t.getMonth()+1),"%d":t=>at(t.getDate()),"%H":t=>at(t.getHours()),"%M":t=>at(t.getMinutes()),"%S":t=>at(t.getSeconds())},lt=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>ut[t]?ut[t](e):t.slice(1)))),ot=(t,e)=>{const r=new Date(t),s=[];for(;t<e;)r.setDate(r.getDate()+1),s.push(new Date(r));return s},ht=36e5,ct=24*ht,vt={ms:1,s:1e3,m:6e4,h:ht,d:ct,w:7*ct,y:315576e5},pt=["y","d","h","m","s","ms"],dt=t=>{const e=[];return pt.forEach((r=>{const s=vt[r],n=(t-t%s)/s;n&&(t-=n*s,e.push(n.toString()+r))})),e.join(" ")},ft=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=e.exec(t));)r.index===e.lastIndex&&e.lastIndex++,s+=parseFloat(r[1])*vt[r[2]]||0;return s},mt=(t,e)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...n){s(),r=setTimeout((()=>t(...n)),e)}}};class gt{constructor(){this.events=new Map}on(t,e){const r=this.events.get(t);return r?r.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const r=s=>{e(s),this.off(t,r)};return this.on(t,r),{dispose:()=>this.off(t,r)}}off(t,e){const r=this.events.get(t);if(e){if(r){const t=r.indexOf(e);t>=0&&r.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const r=this.events.get(t);r&&r.forEach((t=>t(e)))}}function xt(t,e,r,s){return new(r||(r=Promise))((function(n,i){function a(t){try{l(s.next(t))}catch(t){i(t)}}function u(t){try{l(s.throw(t))}catch(t){i(t)}}function l(t){t.done?n(t.value):function(t){return t instanceof r?t:new r((function(e){e(t)}))}(t.value).then(a,u)}l((s=s.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const yt=(t,e)=>{let r=!1;const s=()=>xt(void 0,void 0,void 0,(function*(){r&&(yield t(),setTimeout(s,e))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},bt=t=>{var e;return 0==(null!==(e=null==t?void 0:t.length)&&void 0!==e?e:0)},wt=["",0,"0",null,!1,void 0],St=t=>-1==wt.indexOf(t),kt=t=>!St(t),Ot=t=>!bt(t),jt=()=>{},Ct=(t,e,r)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,null!=r?r:""),Mt=(t,e)=>Ct(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase()))),e),_t=Mt,At=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},It=/&(amp|apos|lt|gt|nbsp|quot);/g,$t={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},Dt=t=>t.replace(It,((t,e)=>$t[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),Et=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),Lt=t=>/^[\w]$/.test(t),Ut=t=>/^[\d]$/.test(t),qt=t=>/^[A-Za-z]$/.test(t),Nt=t=>/^[^\w]$/.test(t),Ft=t=>{if(d(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"},Tt=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),Yt=t=>u(t.split("")).join("");class Ht{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 Ht).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Ht).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=Yt(e);for(let s=0;s<t;s++)r+=e.charAt(n(0,e.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new Ht).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Ht).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Ht).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Ht).upper()}}const Pt=t=>t.split("").reverse().join(""),Zt=(t,e)=>Ct(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r))),e),zt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),Jt={"#":t=>t.value,0:t=>zt(t,Ut),9:t=>zt(t,Ut),A:t=>zt(t,qt),Z:t=>zt(t,qt),L:t=>zt(t,qt).toLowerCase(),U:t=>zt(t,qt).toUpperCase(),S:t=>zt(t,Nt)},Bt=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:r,text:s,textIndex:n}=t;t.key=e[r],t.value=s[n];const{key:i,value:a}=t;return _(i)||_(a)?"":(t=>Jt[t.key]?Jt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Bt(t)},Rt=(t,e)=>Bt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),Wt=(t,e)=>Pt(Rt(Pt(t),Pt(e))),Gt=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\D/g,"")).toString(),Wt(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),Kt=(t,e)=>t.length==Rt(t,e).length,Qt=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),Vt=(t,e)=>Qt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Xt=(t,...e)=>{if(!e.length)return t;const r=e.shift();if(w(t)&&w(r))for(const e in r)w(r[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Xt(t[e],r[e])):t=Object.assign(Object.assign({},t),{[e]:r[e]});return Xt(t,...e)},te=(t,e,r)=>xt(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((s,n)=>e.call(r,s,n,t)))).catch(jt)})),ee=(t,e,r)=>xt(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t).catch(jt)})),re=t=>new Promise((e=>setTimeout(e,t)));class se{constructor(t){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[],this._type=t}empty(t){const e=t;return this.unshift((({value:t})=>{const r=p(t);return{break:!r,value:r?t:m(e)?e():e}}))}equal(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>{const n=r({value:t,context:s});return t==n?{value:t}:this.invalid(`.equal(${n})`,e)}))}error(t){return C(t)&&(this._error=t),this}filter(...t){const e=t;return this.unshift((({value:t})=>{const r=-1==e.indexOf(t);return{break:!r,value:r?t:void 0}}))}fn(t){return m(t)?t:()=>t}from(t){return this.source=t,this}invalid(t,e){return t=`[${this._type}]${t}`,{error:this._error?this._error:this.name?this.name+":"+t:t,message:e}}parse(){return this.unshift((({value:t})=>({value:t})))}push(t){return this.rules.push(t),this}required(t){return this.unshift((({value:e})=>p(e)?{value:e}:this.invalid(".required",t)))}test(t){return!C(this.validate(t).error)}testAsync(t){return xt(this,void 0,void 0,(function*(){return!C((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t,e){var r;if(this.custom.length)return this.invalid("[async]");let s={value:t};for(let t=0;t<this.rules.length;t++){if(s=this.rules[t]({value:s.value,context:e}),null===(r=s.error)||void 0===r?void 0:r.length)return Object.assign({error:s.error},s.message?{message:s.message}:{});if(s.break)return{value:s.value}}return{value:s.value}}validateAsync(t,e){return xt(this,void 0,void 0,(function*(){let r=this.validate(t,e);if(r.error)return{error:r.error};for(let t=0;t<this.custom.length;t++){if(r=yield this.custom[t]({value:r.value}),r.error)return{error:r.error};if(r.break)return{value:r.value}}return{value:r.value}}))}}class ne extends se{constructor(t){super("array"),this.push((({value:e})=>h(e)?{value:e}:this.invalid("",t)))}between(t,e,r){return this.min(t,r),this.max(e,r)}items(t,e){const r=t;return r?this.push((({value:t})=>{r.name="";for(let s=0;s<t.length;s++){const{error:n,value:i}=r.validate(t[s],t);if(n)return this.invalid("< "+n+"["+s+"] >",e);t[s]=i}return{value:t}})):this}length(t,e){const r=t;return this.push((({value:t})=>t.length==r?{value:t}:this.invalid(".length("+r+")",e)))}max(t,e){const r=t;return this.push((({value:t})=>t.length<=r?{value:t}:this.invalid(".max("+r+")",e)))}min(t,e){const r=t;return this.push((({value:t})=>t.length>=r?{value:t}:this.invalid(".min("+r+")",e)))}}class ie extends se{constructor(t){super("boolean"),this.push((({value:e})=>c(e)?{value:e}:this.invalid("",t)))}parse(){return this.unshift((({value:t})=>({value:p(t)?D(t):t})))}}class ae extends se{constructor(t){super("date"),this.push((({value:e})=>C(e)&&e.length>=9&&!isNaN(new Date(e).getTime())?{value:e}:this.invalid("",t)))}format(t){const e=t;return this.push((({value:t})=>({value:lt(e,new Date(t))})))}parse(){return this.unshift((({value:t})=>({value:new Date(C(t)&&t.length<=10?`${t} 12:00`:t)})))}}class ue extends se{constructor(t){super("number"),this.push((({value:e})=>d(e)?{value:e}:this.invalid("",t)))}between(t,e,r){return this.min(t,r),this.max(e,r)}float(t){return this.push((({value:e})=>f(e)?{value:e}:this.invalid(".float",t)))}integer(t){return this.push((({value:e})=>g(e)?{value:e}:this.invalid(".integer",t)))}max(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>{const n=r({value:t,context:s});return t<=n?{value:t}:this.invalid(`.max(${n})`,e)}))}min(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>{const n=r({value:t,context:s});return t>=n?{value:t}:this.invalid(`.min(${n})`,e)}))}parse(){return this.unshift((({value:t})=>({value:p(t)?parseFloat(t.toString()):t})))}}class le extends se{constructor(t){super("object"),this.push((({value:e})=>b(e)?{value:e}:this.invalid("",t)))}keys(t,e){const r=t;return r?this.push((({value:t})=>{const s=[],n={};return Object.keys(r).forEach((i=>{var a;const u=r[i];u.name=null!==(a=u.source)&&void 0!==a?a:i;const{error:l,message:o,value:h}=u.validate(t[u.name],n);!e&&o&&(e=o),l&&s.push(l),p(h)&&(n[i]=h)})),s.length?this.invalid("{ "+s.join(", ")+" }",e):{value:n}})):this}parse(t){return this.unshift((({value:e})=>{if(!b(e))try{e=JSON.parse(e)}catch(e){return this.invalid("",t)}return{value:e}}))}}class oe extends se{constructor(t){super("string"),this.push((({value:e})=>C(e)?{value:e}:this.invalid("",t)))}between(t,e,r){return this.min(t,r),this.max(e,r)}email(t){const e=/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i;return this.push((({value:r})=>e.test(r)?{value:r}:this.invalid(".email",t)))}length(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>{const n=r({value:t,context:s});return t.length==n?{value:t}:this.invalid(`.length(${n})`,e)}))}lowerCase(){return this.push((({value:t})=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((({value:t})=>({value:Mt(t)})))}max(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>{const n=r({value:t,context:s});return t.length<=n?{value:t}:this.invalid(`.max(${n})`,e)}))}mask(t){const e=this.fn(t);return this.push((({value:t,context:r})=>({value:Rt(e({value:t,context:r}),t)})))}min(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>{const n=r({value:t,context:s});return t.length>=n?{value:t}:this.invalid(`.min(${n})`,e)}))}parse(){return this.unshift((({value:t})=>({value:b(t)?JSON.stringify(t):p(t)?t.toString():t})))}required(t){return this.push((({value:e})=>e.length?{value:e}:this.invalid(".required",t)))}regex(t,e){return this.push((({value:r})=>t.test(r)?{value:r}:this.invalid(".regex",e)))}replace(t,e){const r=this.fn(t);return this.push((({value:t,context:s})=>({value:t.replace(r({value:t,context:s}),((...t)=>m(e)?e(...t):e))})))}trim(){return this.push((({value:t})=>({value:t.trim()})))}upperCase(){return this.push((({value:t})=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((({value:t})=>({value:Zt(t)})))}}class he{static array(t,e){return new ne(e).items(t)}static boolean(t){return new ie(t)}static date(t){return new ae(t)}static number(t){return new ue(t)}static object(t,e){return new le(e).keys(t)}static ref(t,e){return({context:r})=>r&&p(r[t])?r[t]:e}static string(t){return new oe(t)}test(t,...e){return!C(he.validate(t,...e).error)}testAsync(t,...e){return xt(this,void 0,void 0,(function*(){return!C((yield he.validateAsync(t,...e)).error)}))}static validate(t,...e){let r={value:t};for(let t=0;t<e.length;t++)if(r=e[t].validate(r.value),r.error)return{value:r.value,error:r.error};return{value:r.value}}static validateAsync(t,...e){return xt(this,void 0,void 0,(function*(){let r={value:t};for(let t=0;t<e.length;t++)if(r=yield e[t].validateAsync(r.value),r.error)return{value:r.value,error:r.error};return{value:r.value}}))}}export{gt as EventEmitter,Q as Hash,Ht as RandomString,he as Validation,se as ValidationBase,Qt as allowed,E as cache,_t as camelCase,t as chunkArray,r as clamp,B as colorPalette,e as countArray,At as cutText,lt as dateFormat,ot as dateRange,mt as debounce,Dt as decodeHTML,Vt as denied,q as each,Et as encodeHTML,te as forEachAsync,L as forEachIndex,U as forEachKey,ee as forEachSeries,N as forLoop,Y as hexToRgb,R as hslToRgb,h as isArray,c as isBoolean,Lt as isCharacter,v as isDate,p as isDef,Ut as isDigit,bt as isEmpty,kt as isFalse,f as isFloat,m as isFunction,g as isInteger,qt as isLetter,x as isMap,Ot as isNotEmpty,y as isNull,d as isNumber,b as isObject,w as isPlainObject,S as isRegExp,O as isScalar,j as isSet,Nt as isSpecialChar,C as isString,M as isSymbol,St as isTrue,_ as isUndefined,s as lerp,yt as loop,Mt as lowerCamelCase,F as map,Rt as mask,Gt as maskMoney,Wt as maskReverse,Kt as maskTest,Xt as merge,dt as msToString,jt as noop,ft as parseMs,Ft as parseSize,n as random,Tt as randomCase,i as randomElement,a as rangeArray,P as rgbToHex,K as rgbToHsl,Z as shadeColor,u as shuffleArray,Yt as shuffleString,st as simpleID,re as sleep,Pt as stringReverse,z as tintColor,A as toArray,D as toBool,o as type,I as uniqueArray,nt as uniqueID,Zt as upperCamelCase,it as uuidv4};
|
package/dist/index.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.4.
|
|
2
|
+
* @andrewcaires/utils.js v0.4.4
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) 2025 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","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),u=t=>"null"!=n(t),l=t=>"number"==n(t),o=t=>"function"==typeof t,c=t=>"object"==n(t),p=t=>c(t)&&t.constructor===Object,m=["boolean","number","string"],d=t=>"string"==typeof t,v=t=>void 0===t,f=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},y=(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)]))),_=t=>parseInt(null!=t?t:"0",16)||0,x=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(_))||[]},b=t=>("0"+t.toString(16)).slice(-2),w=(t,e,s)=>"#"+b(t)+b(e)+b(s),k=(t,s)=>{const r=x(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return w(i,n,a)},S=(t,s)=>{const r=x(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return w(i,n,a)},C={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},O=(t,e,s,r,i)=>0===t?0:e===s?(r-i)/t%6:e===r?(i-s)/t+2:e===i?(s-r)/t+4:0,j=(t,e)=>0===t?0:t/(1-Math.abs(2*e-1));class M{constructor(t){this.key=this.shuffle(M.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[M.dictionary.indexOf(t[s]),M.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return M.dictionary[(s?t-e+M.dictionary.length:t+e)%M.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}M.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const A=t=>Math.random().toString(16).slice(t),E=()=>A(-3),I=()=>A(-4),D=()=>I()+I(),T=()=>D()+D(),L=t=>("0"+t.toString()).slice(-2),F={"%y":t=>L(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>L(t.getMonth()+1),"%d":t=>L(t.getDate()),"%H":t=>L(t.getHours()),"%M":t=>L(t.getMinutes()),"%S":t=>L(t.getSeconds())},N=(t,e)=>(e=null!=e?e:new Date,t.replace(/%[yYmdHMS]/g,(t=>F[t]?F[t](e):t.slice(1)))),U=36e5,q=24*U,H={ms:1,s:1e3,m:6e4,h:U,d:q,w:7*q,y:315576e5},R=["y","d","h","m","s","ms"];function P(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{u(r.next(t))}catch(t){n(t)}}function h(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){t.done?i(t.value):function(t){return t instanceof s?t:new s((function(e){e(t)}))}(t.value).then(a,h)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const B=t=>{var e;return 0==(null!==(e=null==t?void 0:t.length)&&void 0!==e?e:0)},Y=["",0,"0",null,!1,void 0],z=t=>-1==Y.indexOf(t),J=()=>{},Z=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),$=t=>Z(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),V=$,W=/&(amp|apos|lt|gt|nbsp|quot);/g,K={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},G=t=>/^[\d]$/.test(t),Q=t=>/^[A-Za-z]$/.test(t),X=t=>/^[^\w]$/.test(t),tt=t=>r(t.split("")).join("");class et{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 et).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new et).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=tt(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 et).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new et).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new et).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new et).upper()}}const st=t=>t.split("").reverse().join(""),rt=t=>Z(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),nt={"#":t=>t.value,0:t=>it(t,G),9:t=>it(t,G),A:t=>it(t,Q),Z:t=>it(t,Q),L:t=>it(t,Q).toLowerCase(),U:t=>it(t,Q).toUpperCase(),S:t=>it(t,X)},at=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return v(n)||v(a)?"":(t=>nt[t.key]?nt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+at(t)},ht=(t,e)=>at({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),ut=(t,e)=>st(ht(st(t),st(e))),lt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),ot=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(p(t)&&p(s))for(const e in s)p(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=ot(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return ot(t,...e)};class ct{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=u(t);return{break:!e,value:e?t:o(this._value)?this._value():this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}from(t){return this.source=t,this}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>u(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!d(this.validate(t).error)}testAsync(t){return P(this,void 0,void 0,(function*(){return!d((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return P(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class pt extends ct{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 mt extends ct{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:u(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class dt extends ct{constructor(){super(),this._type="date",this._format="",this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"]"):{value:t}))}format(t){return this._format=t,this.push((t=>({value:N(this._format,new Date(t))})))}parse(){return this.unshift((t=>(d(t)&&t.length<=10&&(t+=" 12:00"),{value:new Date(t)})))}}class vt extends ct{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>l(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:u(t)?parseFloat(t):t})))}}class ft extends ct{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(c(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{var i;const n=this._keys[r];n.name=null!==(i=n.source)&&void 0!==i?i:r;const{error:a,value:h}=n.validate(t[n.name]);a&&e.push(a),u(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!c(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?lt(e,t):t}}))}}class yt extends ct{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>d(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:$(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:ht(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:c(t)?JSON.stringify(t):u(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:rt(t)})))}}class gt{static array(t){return(new pt).items(t)}static boolean(){return new mt}static date(){return new dt}static number(){return new vt}static object(t){return(new ft).keys(t)}static string(){return new yt}test(t,...e){return!d(gt.validate(t,...e).error)}testAsync(t,...e){return P(this,void 0,void 0,(function*(){return!d((yield gt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return P(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},t.Hash=M,t.RandomString=et,t.Validation=gt,t.ValidationBase=ct,t.allowed=lt,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=V,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:C,(e=>0==e?t:e>0?S(t,e):k(t,1- -1*e))),t.countArray=t=>{const e={};return t.forEach((t=>{const s=t.toString();e[s]=(e[s]||0)+1})),e},t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=N,t.dateRange=(t,e)=>{const s=new Date(t),r=[];for(;t<e;)s.setDate(s.getDate()+1),r.push(new Date(s));return r},t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(W,((t,e)=>K[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>lt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?f(t,e,s):c(t)?y(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>P(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(J)})),t.forEachIndex=f,t.forEachKey=y,t.forEachSeries=(t,e,s)=>P(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(J)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=x,t.hslToRgb=(t,e,s)=>{t/=60;const r=(1-Math.abs(2*s-1))*e,i=r*(1-Math.abs(t%2-1));let n=[0,0,0];t<=1?n=[r,i,0]:t<=2?n=[i,r,0]:t<=3?n=[0,r,i]:t<=4?n=[0,i,r]:t<=5?n=[i,0,r]:t<=6&&(n=[r,0,i]);const a=s-.5*r;return[Math.round(255*(n[0]+a)),Math.round(255*(n[1]+a)),Math.round(255*(n[2]+a))]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDate=t=>"date"==n(t),t.isDef=u,t.isDigit=G,t.isEmpty=B,t.isFalse=t=>!z(t),t.isFloat=t=>l(t)&&!!(t%1),t.isFunction=o,t.isInteger=t=>l(t)&&!(t%1),t.isLetter=Q,t.isMap=t=>"map"==n(t),t.isNotEmpty=t=>!B(t),t.isNull=t=>null===t,t.isNumber=l,t.isObject=c,t.isPlainObject=p,t.isRegExp=t=>"regexp"==n(t),t.isScalar=t=>-1!=m.indexOf(n(t)),t.isSet=t=>"set"==n(t),t.isSpecialChar=X,t.isString=d,t.isSymbol=t=>"symbol"==typeof t,t.isTrue=z,t.isUndefined=v,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>P(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=$,t.map=g,t.mask=ht,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),ut(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=ut,t.maskTest=(t,e)=>t.length==ht(t,e).length,t.merge=ot,t.msToString=t=>{const e=[];return R.forEach((s=>{const r=H[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=J,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(l(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.randomElement=t=>t.length?t[s(0,t.length-1)]:null,t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=w,t.rgbToHsl=(t,e,s)=>{const[r,i,n]=((...t)=>t.map((t=>t/255)))(t,e,s),a=Math.max(r,i,n),h=Math.min(r,i,n),u=a-h,l=(h+a)/2;return[60*O(u,a,r,i,n),j(u,l),l]},t.shadeColor=k,t.shuffleArray=r,t.shuffleString=tt,t.simpleID=()=>E()+I(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=st,t.tintColor=S,t.toArray=t=>a(t)?t:u(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueArray=t=>t.filter(((t,e,s)=>s.indexOf(t)===e)),t.uniqueID=()=>T()+T()+T()+T(),t.upperCamelCase=rt,t.uuidv4=()=>D()+"-"+I()+"-4"+E()+"-"+I()+"-"+(D()+I()),t}({});
|
|
7
|
+
var UtilsJS=function(e){"use strict";const t=(e,t,r)=>Math.max(t,Math.min(r,e)),r=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),s=e=>{for(let t=e.length-1;t>0;t--){const s=r(0,t);[e[t],e[s]]=[e[s],e[t]]}return e},n={},a=e=>null==e?"null":n[n.toString.call(e)]||"object";["Array","AsyncFunction","Boolean","Date","Function","Map","Number","Object","RegExp","Set","String"].forEach((e=>n["[object "+e+"]"]=e.toLowerCase()));const i=e=>"array"==a(e),u=e=>"boolean"==a(e),l=e=>"null"!=a(e),o=e=>"number"==a(e),h=e=>o(e)&&!!(e%1),c=e=>"function"==typeof e,v=e=>o(e)&&!(e%1),p=e=>"object"==a(e),d=e=>p(e)&&e.constructor===Object,m=["boolean","number","string"],f=e=>"string"==typeof e,g=e=>void 0===e,y=[1,"1",!0,"true"],x=e=>-1!=y.indexOf(e),b=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},w=(e,t,r)=>{let s="";const n=Object.keys(e);for(let a=0;a<n.length&&(s=n[a],!1!==t.call(r,e[s],s,e));a++);return e},S=(e,t,r)=>Object.fromEntries(Object.entries(e).map((([s,n])=>[s,t.call(r,n,s,e)]))),C=e=>parseInt(null!=e?e:"0",16)||0,k=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map(C))||[]},O=e=>("0"+e.toString(16)).slice(-2),j=(e,t,r)=>"#"+O(e)+O(t)+O(r),M=(e,r)=>{const s=k(e);r=t(r,0,1);const[n,a,i]=s.map((e=>Math.round(e*r)));return j(n,a,i)},A=(e,r)=>{const s=k(e);r=t(r,0,1);const[n,a,i]=s.map((e=>Math.round(e+(255-e)*r)));return j(n,a,i)},E={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},I=(e,t,r,s,n)=>0===e?0:t===r?(s-n)/e%6:t===s?(n-r)/e+2:t===n?(r-s)/e+4:0,_=(e,t)=>0===e?0:e/(1-Math.abs(2*t-1));class D{constructor(e){this.key=this.shuffle(D.dictionary,e,!1)}encrypt(e){return this.shuffle(e,this.key,!1)}decrypt(e){return this.shuffle(e,this.key,!0)}index(e,t,r){return[D.dictionary.indexOf(e[r]),D.dictionary.indexOf(t[r%t.length])]}result(e,t,r){return D.dictionary[(r?e-t+D.dictionary.length:e+t)%D.dictionary.length]}shuffle(e,t,r){let s="";for(let n=0;n<e.length;n++){const[a,i]=this.index(e,t,n);s+=-1===a||-1===i?e[n]:this.result(a,i,r)}return s}}D.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const T=e=>Math.random().toString(16).slice(e),L=()=>T(-3),$=()=>T(-4),F=()=>$()+$(),N=()=>F()+F(),U=e=>("0"+e.toString()).slice(-2),q={"%y":e=>U(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>U(e.getMonth()+1),"%d":e=>U(e.getDate()),"%H":e=>U(e.getHours()),"%M":e=>U(e.getMinutes()),"%S":e=>U(e.getSeconds())},H=(e,t)=>(t=null!=t?t:new Date,e.replace(/%[yYmdHMS]/g,(e=>q[e]?q[e](t):e.slice(1)))),R=36e5,P=24*R,B={ms:1,s:1e3,m:6e4,h:R,d:P,w:7*P,y:315576e5},Y=["y","d","h","m","s","ms"];function z(e,t,r,s){return new(r||(r=Promise))((function(n,a){function i(e){try{l(s.next(e))}catch(e){a(e)}}function u(e){try{l(s.throw(e))}catch(e){a(e)}}function l(e){e.done?n(e.value):function(e){return e instanceof r?e:new r((function(t){t(e)}))}(e.value).then(i,u)}l((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const J=e=>{var t;return 0==(null!==(t=null==e?void 0:e.length)&&void 0!==t?t:0)},Z=["",0,"0",null,!1,void 0],V=e=>-1==Z.indexOf(e),K=()=>{},W=(e,t,r)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,null!=r?r:""),G=(e,t)=>W(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase()))),t),Q=G,X=/&(amp|apos|lt|gt|nbsp|quot);/g,ee={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},te=e=>/^[\d]$/.test(e),re=e=>/^[A-Za-z]$/.test(e),se=e=>/^[^\w]$/.test(e),ne=e=>s(e.split("")).join("");class ae{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 ae).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new ae).lower()}make(e){let t="";this.hasSpace&&(t+=this._space),this.hasNumbers&&(t+=this._numbers),this.hasSymbols&&(t+=this._symbols),this.hasUpper&&!this.hasChars&&(t+=this._upper),this.hasLower&&!this.hasChars&&(t+=this._lower),this.hasChars&&(t+=this._chars);let s="";if(e&&t.length){t=ne(t);for(let n=0;n<e;n++)s+=t.charAt(r(0,t.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new ae).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new ae).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new ae).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new ae).upper()}}const ie=e=>e.split("").reverse().join(""),ue=(e,t)=>W(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r))),t),le=(e,t)=>t(e.value)?e.value:(e=>(e.maskIndex--,""))(e),oe={"#":e=>e.value,0:e=>le(e,te),9:e=>le(e,te),A:e=>le(e,re),Z:e=>le(e,re),L:e=>le(e,re).toLowerCase(),U:e=>le(e,re).toUpperCase(),S:e=>le(e,se)},he=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:n}=e;e.key=t[r],e.value=s[n];const{key:a,value:i}=e;return g(a)||g(i)?"":(e=>oe[e.key]?oe[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+he(e)},ce=(e,t)=>he({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),ve=(e,t)=>ie(ce(ie(e),ie(t))),pe=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{}),de=(e,...t)=>{if(!t.length)return e;const r=t.shift();if(d(e)&&d(r))for(const t in r)d(r[t])?(e[t]||(e=Object.assign(Object.assign({},e),{[t]:{}})),e[t]=de(e[t],r[t])):e=Object.assign(Object.assign({},e),{[t]:r[t]});return de(e,...t)};class me{constructor(e){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[],this._type=e}empty(e){const t=e;return this.unshift((({value:e})=>{const r=l(e);return{break:!r,value:r?e:c(t)?t():t}}))}equal(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e==n?{value:e}:this.invalid(`.equal(${n})`,t)}))}error(e){return f(e)&&(this._error=e),this}filter(...e){const t=e;return this.unshift((({value:e})=>{const r=-1==t.indexOf(e);return{break:!r,value:r?e:void 0}}))}fn(e){return c(e)?e:()=>e}from(e){return this.source=e,this}invalid(e,t){return e=`[${this._type}]${e}`,{error:this._error?this._error:this.name?this.name+":"+e:e,message:t}}parse(){return this.unshift((({value:e})=>({value:e})))}push(e){return this.rules.push(e),this}required(e){return this.unshift((({value:t})=>l(t)?{value:t}:this.invalid(".required",e)))}test(e){return!f(this.validate(e).error)}testAsync(e){return z(this,void 0,void 0,(function*(){return!f((yield this.validateAsync(e)).error)}))}unshift(e){return this.rules.unshift(e),this}validate(e,t){var r;if(this.custom.length)return this.invalid("[async]");let s={value:e};for(let e=0;e<this.rules.length;e++){if(s=this.rules[e]({value:s.value,context:t}),null===(r=s.error)||void 0===r?void 0:r.length)return Object.assign({error:s.error},s.message?{message:s.message}:{});if(s.break)return{value:s.value}}return{value:s.value}}validateAsync(e,t){return z(this,void 0,void 0,(function*(){let r=this.validate(e,t);if(r.error)return{error:r.error};for(let e=0;e<this.custom.length;e++){if(r=yield this.custom[e]({value:r.value}),r.error)return{error:r.error};if(r.break)return{value:r.value}}return{value:r.value}}))}}class fe extends me{constructor(e){super("array"),this.push((({value:t})=>i(t)?{value:t}:this.invalid("",e)))}between(e,t,r){return this.min(e,r),this.max(t,r)}items(e,t){const r=e;return r?this.push((({value:e})=>{r.name="";for(let s=0;s<e.length;s++){const{error:n,value:a}=r.validate(e[s],e);if(n)return this.invalid("< "+n+"["+s+"] >",t);e[s]=a}return{value:e}})):this}length(e,t){const r=e;return this.push((({value:e})=>e.length==r?{value:e}:this.invalid(".length("+r+")",t)))}max(e,t){const r=e;return this.push((({value:e})=>e.length<=r?{value:e}:this.invalid(".max("+r+")",t)))}min(e,t){const r=e;return this.push((({value:e})=>e.length>=r?{value:e}:this.invalid(".min("+r+")",t)))}}class ge extends me{constructor(e){super("boolean"),this.push((({value:t})=>u(t)?{value:t}:this.invalid("",e)))}parse(){return this.unshift((({value:e})=>({value:l(e)?x(e):e})))}}class ye extends me{constructor(e){super("date"),this.push((({value:t})=>f(t)&&t.length>=9&&!isNaN(new Date(t).getTime())?{value:t}:this.invalid("",e)))}format(e){const t=e;return this.push((({value:e})=>({value:H(t,new Date(e))})))}parse(){return this.unshift((({value:e})=>({value:new Date(f(e)&&e.length<=10?`${e} 12:00`:e)})))}}class xe extends me{constructor(e){super("number"),this.push((({value:t})=>o(t)?{value:t}:this.invalid("",e)))}between(e,t,r){return this.min(e,r),this.max(t,r)}float(e){return this.push((({value:t})=>h(t)?{value:t}:this.invalid(".float",e)))}integer(e){return this.push((({value:t})=>v(t)?{value:t}:this.invalid(".integer",e)))}max(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e<=n?{value:e}:this.invalid(`.max(${n})`,t)}))}min(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e>=n?{value:e}:this.invalid(`.min(${n})`,t)}))}parse(){return this.unshift((({value:e})=>({value:l(e)?parseFloat(e.toString()):e})))}}class be extends me{constructor(e){super("object"),this.push((({value:t})=>p(t)?{value:t}:this.invalid("",e)))}keys(e,t){const r=e;return r?this.push((({value:e})=>{const s=[],n={};return Object.keys(r).forEach((a=>{var i;const u=r[a];u.name=null!==(i=u.source)&&void 0!==i?i:a;const{error:o,message:h,value:c}=u.validate(e[u.name],n);!t&&h&&(t=h),o&&s.push(o),l(c)&&(n[a]=c)})),s.length?this.invalid("{ "+s.join(", ")+" }",t):{value:n}})):this}parse(e){return this.unshift((({value:t})=>{if(!p(t))try{t=JSON.parse(t)}catch(t){return this.invalid("",e)}return{value:t}}))}}class we extends me{constructor(e){super("string"),this.push((({value:t})=>f(t)?{value:t}:this.invalid("",e)))}between(e,t,r){return this.min(e,r),this.max(t,r)}email(e){const t=/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i;return this.push((({value:r})=>t.test(r)?{value:r}:this.invalid(".email",e)))}length(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e.length==n?{value:e}:this.invalid(`.length(${n})`,t)}))}lowerCase(){return this.push((({value:e})=>({value:e.toLowerCase()})))}lowerCamelCase(){return this.push((({value:e})=>({value:G(e)})))}max(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e.length<=n?{value:e}:this.invalid(`.max(${n})`,t)}))}mask(e){const t=this.fn(e);return this.push((({value:e,context:r})=>({value:ce(t({value:e,context:r}),e)})))}min(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>{const n=r({value:e,context:s});return e.length>=n?{value:e}:this.invalid(`.min(${n})`,t)}))}parse(){return this.unshift((({value:e})=>({value:p(e)?JSON.stringify(e):l(e)?e.toString():e})))}required(e){return this.push((({value:t})=>t.length?{value:t}:this.invalid(".required",e)))}regex(e,t){return this.push((({value:r})=>e.test(r)?{value:r}:this.invalid(".regex",t)))}replace(e,t){const r=this.fn(e);return this.push((({value:e,context:s})=>({value:e.replace(r({value:e,context:s}),((...e)=>c(t)?t(...e):t))})))}trim(){return this.push((({value:e})=>({value:e.trim()})))}upperCase(){return this.push((({value:e})=>({value:e.toUpperCase()})))}upperCamelCase(){return this.push((({value:e})=>({value:ue(e)})))}}class Se{static array(e,t){return new fe(t).items(e)}static boolean(e){return new ge(e)}static date(e){return new ye(e)}static number(e){return new xe(e)}static object(e,t){return new be(t).keys(e)}static ref(e,t){return({context:r})=>r&&l(r[e])?r[e]:t}static string(e){return new we(e)}test(e,...t){return!f(Se.validate(e,...t).error)}testAsync(e,...t){return z(this,void 0,void 0,(function*(){return!f((yield Se.validateAsync(e,...t)).error)}))}static validate(e,...t){let r={value:e};for(let e=0;e<t.length;e++)if(r=t[e].validate(r.value),r.error)return{value:r.value,error:r.error};return{value:r.value}}static validateAsync(e,...t){return z(this,void 0,void 0,(function*(){let r={value:e};for(let e=0;e<t.length;e++)if(r=yield t[e].validateAsync(r.value),r.error)return{value:r.value,error:r.error};return{value:r.value}}))}}return e.EventEmitter=class{constructor(){this.events=new Map}on(e,t){const r=this.events.get(e);return r?r.push(t):this.events.set(e,[t]),{dispose:()=>this.off(e,t)}}once(e,t){const r=s=>{t(s),this.off(e,r)};return this.on(e,r),{dispose:()=>this.off(e,r)}}off(e,t){const r=this.events.get(e);if(t){if(r){const e=r.indexOf(t);e>=0&&r.splice(e,1)}}else this.events.set(e,[])}emit(e,t){const r=this.events.get(e);r&&r.forEach((e=>e(t)))}},e.Hash=D,e.RandomString=ae,e.Validation=Se,e.ValidationBase=me,e.allowed=pe,e.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},e.camelCase=Q,e.chunkArray=(e,t)=>{const r=[];for(let s=0;s<e.length;s+=t)r.push(e.slice(s,s+t));return r},e.clamp=t,e.colorPalette=(e,t)=>S(null!=t?t:E,(t=>0==t?e:t>0?A(e,t):M(e,1- -1*t))),e.countArray=e=>{const t={};return e.forEach((e=>{const r=e.toString();t[r]=(t[r]||0)+1})),t},e.cutText=(e,t)=>{if(e.length>t){const r=e.substring(0,t).split(" ");return r.pop(),r.join(" ")+"..."}return e},e.dateFormat=H,e.dateRange=(e,t)=>{const r=new Date(e),s=[];for(;e<t;)r.setDate(r.getDate()+1),s.push(new Date(r));return s},e.debounce=(e,t)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...n){s(),r=setTimeout((()=>e(...n)),t)}}},e.decodeHTML=e=>e.replace(X,((e,t)=>ee[t])).replace(/&#(\d+);/gi,((e,t)=>String.fromCharCode(parseInt(t,10)))),e.denied=(e,t)=>pe(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,r)=>i(e)?b(e,t,r):p(e)?w(e,t,r):e,e.encodeHTML=e=>e.replace(/[^0-9A-Za-z ]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>z(void 0,void 0,void 0,(function*(){yield Promise.allSettled(e.map(((s,n)=>t.call(r,s,n,e)))).catch(K)})),e.forEachIndex=b,e.forEachKey=w,e.forEachSeries=(e,t,r)=>z(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e).catch(K)})),e.forLoop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.hexToRgb=k,e.hslToRgb=(e,t,r)=>{e/=60;const s=(1-Math.abs(2*r-1))*t,n=s*(1-Math.abs(e%2-1));let a=[0,0,0];e<=1?a=[s,n,0]:e<=2?a=[n,s,0]:e<=3?a=[0,s,n]:e<=4?a=[0,n,s]:e<=5?a=[n,0,s]:e<=6&&(a=[s,0,n]);const i=r-.5*s;return[Math.round(255*(a[0]+i)),Math.round(255*(a[1]+i)),Math.round(255*(a[2]+i))]},e.isArray=i,e.isBoolean=u,e.isCharacter=e=>/^[\w]$/.test(e),e.isDate=e=>"date"==a(e),e.isDef=l,e.isDigit=te,e.isEmpty=J,e.isFalse=e=>!V(e),e.isFloat=h,e.isFunction=c,e.isInteger=v,e.isLetter=re,e.isMap=e=>"map"==a(e),e.isNotEmpty=e=>!J(e),e.isNull=e=>null===e,e.isNumber=o,e.isObject=p,e.isPlainObject=d,e.isRegExp=e=>"regexp"==a(e),e.isScalar=e=>-1!=m.indexOf(a(e)),e.isSet=e=>"set"==a(e),e.isSpecialChar=se,e.isString=f,e.isSymbol=e=>"symbol"==typeof e,e.isTrue=V,e.isUndefined=g,e.lerp=(e,r,s)=>e+(r-e)*t(s,0,1),e.loop=(e,t)=>{let r=!1;const s=()=>z(void 0,void 0,void 0,(function*(){r&&(yield e(),setTimeout(s,t))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},e.lowerCamelCase=G,e.map=S,e.mask=ce,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\D/g,"")).toString(),ve(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=ve,e.maskTest=(e,t)=>e.length==ce(e,t).length,e.merge=de,e.msToString=e=>{const t=[];return Y.forEach((r=>{const s=B[r],n=(e-e%s)/s;n&&(e-=n*s,t.push(n.toString()+r))})),t.join(" ")},e.noop=K,e.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*B[r[2]]||0;return s},e.parseSize=e=>{if(o(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},e.random=r,e.randomCase=e=>e.split("").map((e=>Math.random()<.5?e.toUpperCase():e.toLowerCase())).join(""),e.randomElement=e=>e.length?e[r(0,e.length-1)]:null,e.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),e.rgbToHex=j,e.rgbToHsl=(e,t,r)=>{const[s,n,a]=((...e)=>e.map((e=>e/255)))(e,t,r),i=Math.max(s,n,a),u=Math.min(s,n,a),l=i-u,o=(u+i)/2;return[60*I(l,i,s,n,a),_(l,o),o]},e.shadeColor=M,e.shuffleArray=s,e.shuffleString=ne,e.simpleID=()=>L()+$(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=ie,e.tintColor=A,e.toArray=e=>i(e)?e:l(e)?[e]:[],e.toBool=x,e.type=a,e.uniqueArray=e=>e.filter(((e,t,r)=>r.indexOf(e)===t)),e.uniqueID=()=>N()+N()+N()+N(),e.upperCamelCase=ue,e.uuidv4=()=>F()+"-"+$()+"-4"+L()+"-"+$()+"-"+(F()+$()),e}({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/utils.js",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "JavaScript utility library for web and nodejs development",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs.js",
|
|
@@ -44,15 +44,15 @@
|
|
|
44
44
|
},
|
|
45
45
|
"homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@rollup/plugin-commonjs": "^28.0.
|
|
47
|
+
"@rollup/plugin-commonjs": "^28.0.2",
|
|
48
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
49
|
-
"@rollup/plugin-typescript": "^12.1.
|
|
50
|
-
"@types/node": "^22.
|
|
51
|
-
"eslint": "^9.
|
|
52
|
-
"rollup": "^4.
|
|
49
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
50
|
+
"@types/node": "^22.13.1",
|
|
51
|
+
"eslint": "^9.20.0",
|
|
52
|
+
"rollup": "^4.34.6",
|
|
53
53
|
"rollup-plugin-dts": "^6.1.1",
|
|
54
54
|
"tslib": "^2.8.1",
|
|
55
55
|
"tsx": "^4.19.2",
|
|
56
|
-
"typescript-eslint": "^8.
|
|
56
|
+
"typescript-eslint": "^8.23.0"
|
|
57
57
|
}
|
|
58
58
|
}
|