@andrewcaires/utils.js 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +2 -2
- package/dist/index.d.ts +21 -6
- package/dist/index.esm.js +2 -2
- package/dist/index.min.js +2 -2
- package/package.json +9 -7
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.3
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
"use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),l=t=>"object"==i(t),u=t=>l(t)&&t.constructor===Object,p=t=>"string"==i(t),c=t=>void 0===t,m=t=>("0"+t).slice(-2),d=t=>parseInt(t,16)||0,x=(e,s)=>{return(r=d(e)+s,t(r,0,255)).toString(16);var r};class v{constructor(t){this.key=this.shuffle(v.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[v.dictionary.indexOf(t[s]),v.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return v.dictionary[(s?t-e+v.dictionary.length:t+e)%v.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}}v.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const f=t=>Math.random().toString(16).slice(t),y=()=>f(-3),g=()=>f(-4),_=()=>g()+g(),b=()=>_()+_(),w=t=>("0"+t.toString()).slice(-2),k={"%y":t=>w(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>w(t.getMonth()+1),"%d":t=>w(t.getDate()),"%H":t=>w(t.getHours()),"%M":t=>w(t.getMinutes()),"%S":t=>w(t.getSeconds())},S=36e5,C=24*S,j={ms:1,s:1e3,m:6e4,h:S,d:C,w:7*C,y:315576e5},O=["y","d","h","m","s","ms"];function A(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{o(r.next(t))}catch(t){n(t)}}function h(t){try{o(r.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}o((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const I=()=>{},M=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},E=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},L=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),T=t=>L(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),N=T,U=/&(amp|apos|lt|gt|nbsp|quot);/g,F={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},q=t=>/^[\d]$/.test(t),H=t=>/^[A-Za-z]$/.test(t),D=t=>/^[^\w]$/.test(t),B=t=>s(t.split("")).join("");class P{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new P).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new P).lower()}make(t){let s="";this.hasSpace&&(s+=this._space),this.hasNumbers&&(s+=this._numbers),this.hasSymbols&&(s+=this._symbols),this.hasUpper&&!this.hasChars&&(s+=this._upper),this.hasLower&&!this.hasChars&&(s+=this._lower),this.hasChars&&(s+=this._chars);let r="";if(t&&s.length){s=B(s);for(let i=0;i<t;i++)r+=s.charAt(e(0,s.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new P).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new P).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new P).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new P).upper()}}const R=t=>t.split("").reverse().join(""),Y=t=>L(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),z=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),Z={"#":t=>t.value,0:t=>z(t,q),9:t=>z(t,q),A:t=>z(t,H),Z:t=>z(t,H),L:t=>z(t,H).toLowerCase(),U:t=>z(t,H).toUpperCase(),S:t=>z(t,D)},$=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>Z[t.key]?Z[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+$(t)},J=(t,e)=>$({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),V=(t,e)=>R(J(R(t),R(e))),W=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),K=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(u(t)&&u(s))for(const e in s)u(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=K(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return K(t,...e)};class G{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=h(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return A(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return A(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class Q extends G{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 X extends G{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:h(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class tt extends G{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:h(t)?parseFloat(t):t})))}}class et extends G{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?W(e,t):t}}))}}class st extends G{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:T(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:J(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?JSON.stringify(t):h(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:Y(t)})))}}class rt{static array(t){return(new Q).items(t)}static boolean(){return new X}static number(){return new tt}static object(t){return(new et).keys(t)}static string(){return new st}test(t,...e){return!p(rt.validate(t,...e).error)}testAsync(t,...e){return A(this,void 0,void 0,(function*(){return!p((yield rt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return A(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}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=v,exports.RandomString=P,exports.Validation=rt,exports.ValidationBase=G,exports.allowed=W,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=N,exports.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},exports.clamp=t,exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>k[e]?k[e](t):e.slice(1))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(U,((t,e)=>F[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>W(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?M(t,e,s):l(t)?E(t,e,s):t,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>A(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(I)})),exports.forEachIndex=M,exports.forEachKey=E,exports.forEachSeries=(t,e,s)=>A(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(I)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>d(t))))||[]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=q,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=H,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=u,exports.isString=p,exports.isSymbol=D,exports.isUndefined=c,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>A(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=T,exports.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},exports.mask=J,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),V(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=V,exports.merge=K,exports.msToString=t=>{const e=[];return O.forEach((s=>{const r=j[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=I,exports.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*j[s[2]]||0;return r},exports.parseSize=t=>{if(o(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.randomElement=t=>t.length?t[e(0,t.length-1)]:null,exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=(t,e,s)=>"#"+m(t.toString(16))+m(e.toString(16))+m(s.toString(16)),exports.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>m(x(t,e)))),exports.shuffleArray=s,exports.shuffleString=B,exports.simpleID=()=>y()+g(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=R,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>b()+b()+b()+b(),exports.upperCamelCase=Y,exports.uuidv4=()=>_()+"-"+g()+"-4"+y()+"-"+g()+"-"+(_()+g());
|
|
7
|
+
"use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),l=t=>"object"==i(t),u=t=>l(t)&&t.constructor===Object,p=t=>"string"==i(t),c=t=>void 0===t,m=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},d=(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},x=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),v=t=>parseInt(null!=t?t:"0",16)||0,f=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(v))||[]},y=t=>("0"+t.toString(16)).slice(-2),g=(t,e,s)=>"#"+y(t)+y(e)+y(s),_=(e,s)=>{const r=f(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return g(i,n,a)},b=(e,s)=>{const r=f(e);s=t(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return g(i,n,a)},w={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71};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,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}k.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const C=t=>Math.random().toString(16).slice(t),S=()=>C(-3),j=()=>C(-4),O=()=>j()+j(),A=()=>O()+O(),I=t=>("0"+t.toString()).slice(-2),M={"%y":t=>I(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>I(t.getMonth()+1),"%d":t=>I(t.getDate()),"%H":t=>I(t.getHours()),"%M":t=>I(t.getMinutes()),"%S":t=>I(t.getSeconds())},E=36e5,L=24*E,T={ms:1,s:1e3,m:6e4,h:E,d:L,w:7*L,y:315576e5},N=["y","d","h","m","s","ms"];function U(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{o(r.next(t))}catch(t){n(t)}}function h(t){try{o(r.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}o((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const F=()=>{},q=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),H=t=>q(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),D=H,B=/&(amp|apos|lt|gt|nbsp|quot);/g,P={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},R=t=>/^[\d]$/.test(t),Y=t=>/^[A-Za-z]$/.test(t),z=t=>/^[^\w]$/.test(t),Z=t=>s(t.split("")).join("");class ${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 $).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new $).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=Z(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 $).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new $).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new $).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new $).upper()}}const J=t=>t.split("").reverse().join(""),V=t=>q(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),W=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),K={"#":t=>t.value,0:t=>W(t,R),9:t=>W(t,R),A:t=>W(t,Y),Z:t=>W(t,Y),L:t=>W(t,Y).toLowerCase(),U:t=>W(t,Y).toUpperCase(),S:t=>W(t,z)},G=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>K[t.key]?K[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+G(t)},Q=(t,e)=>G({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),X=(t,e)=>J(Q(J(t),J(e))),tt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),et=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(u(t)&&u(s))for(const e in s)u(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=et(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return et(t,...e)};class st{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=h(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return U(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return U(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 rt extends st{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 it extends st{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:h(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class nt extends st{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:h(t)?parseFloat(t):t})))}}class at extends st{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?tt(e,t):t}}))}}class ht extends st{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:H(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:Q(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?JSON.stringify(t):h(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:V(t)})))}}class ot{static array(t){return(new rt).items(t)}static boolean(){return new it}static number(){return new nt}static object(t){return(new at).keys(t)}static string(){return new ht}test(t,...e){return!p(ot.validate(t,...e).error)}testAsync(t,...e){return U(this,void 0,void 0,(function*(){return!p((yield ot.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 U(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=k,exports.RandomString=$,exports.Validation=ot,exports.ValidationBase=st,exports.allowed=tt,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=D,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.cssColor=(t,e)=>x(null!=e?e:w,(e=>0==e?t:e>0?b(t,e):_(t,1- -1*e))),exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>M[e]?M[e](t):e.slice(1))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(B,((t,e)=>P[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>tt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?m(t,e,s):l(t)?d(t,e,s):t,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>U(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(F)})),exports.forEachIndex=m,exports.forEachKey=d,exports.forEachSeries=(t,e,s)=>U(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(F)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hexToRgb=f,exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=R,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=Y,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=u,exports.isString=p,exports.isSymbol=z,exports.isUndefined=c,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>U(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=H,exports.map=x,exports.mask=Q,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),X(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=X,exports.maskTest=(t,e)=>t.length==Q(t,e).length,exports.merge=et,exports.msToString=t=>{const e=[];return N.forEach((s=>{const r=T[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=F,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])*T[s[2]]||0;return r},exports.parseSize=t=>{if(o(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.randomElement=t=>t.length?t[e(0,t.length-1)]:null,exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=g,exports.shadeColor=_,exports.shuffleArray=s,exports.shuffleString=Z,exports.simpleID=()=>S()+j(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=J,exports.tintColor=b,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>A()+A()+A()+A(),exports.upperCamelCase=V,exports.uuidv4=()=>O()+"-"+j()+"-4"+S()+"-"+j()+"-"+(O()+j());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.3
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
@@ -30,13 +30,18 @@ type TypeArrayAny = TypeArray<any>;
|
|
|
30
30
|
type TypeArrayString = TypeArray<string>;
|
|
31
31
|
type TypeArrayNumber = TypeArray<number>;
|
|
32
32
|
type TypeArrayFunction = TypeArray<TypeCallbackFunction>;
|
|
33
|
+
type TypeNullable<T> = T | null;
|
|
33
34
|
type TypeObject<T> = {
|
|
34
35
|
[key: string]: T;
|
|
35
36
|
};
|
|
36
37
|
type TypeObjectAny = TypeObject<any>;
|
|
37
|
-
type
|
|
38
|
-
type TypeObjectNumber = TypeObject<number>;
|
|
38
|
+
type TypeObjectBoolean = TypeObject<boolean>;
|
|
39
39
|
type TypeObjectFunction = TypeObject<TypeCallbackFunction>;
|
|
40
|
+
type TypeObjectNumber = TypeObject<number>;
|
|
41
|
+
type TypeObjectNullable<T, K = void> = {
|
|
42
|
+
[P in keyof T]?: P extends K ? T[P] : TypeNullable<T[P]>;
|
|
43
|
+
};
|
|
44
|
+
type TypeObjectString = TypeObject<string>;
|
|
40
45
|
type TypeCallback<T, K, O, R> = (value: T, key: K, object: O) => R;
|
|
41
46
|
type TypeCallbackArray<T> = TypeCallback<T, number, TypeArray<T>, any>;
|
|
42
47
|
type TypeCallbackMap<T> = TypeCallback<T, number, TypeArray<T>, T>;
|
|
@@ -64,9 +69,15 @@ type CacheCallback = (key: any) => TypeObjectAny;
|
|
|
64
69
|
|
|
65
70
|
declare const cache: () => CacheCallback;
|
|
66
71
|
|
|
72
|
+
declare const cssColor: (color: string, variants?: TypeObjectNumber) => TypeObjectString;
|
|
73
|
+
|
|
67
74
|
declare const hexToRgb: (color: string) => Array<number>;
|
|
75
|
+
|
|
68
76
|
declare const rgbToHex: (r: number, g: number, b: number) => string;
|
|
69
|
-
|
|
77
|
+
|
|
78
|
+
declare const shadeColor: (color: string, intensity: number) => string;
|
|
79
|
+
|
|
80
|
+
declare const tintColor: (color: string, intensity: number) => string;
|
|
70
81
|
|
|
71
82
|
declare class Hash {
|
|
72
83
|
private static readonly dictionary;
|
|
@@ -138,7 +149,7 @@ type LoopCallback = (index: number, count: number) => any;
|
|
|
138
149
|
|
|
139
150
|
declare const forLoop: (count: number, callback: LoopCallback, thisArg?: any) => number;
|
|
140
151
|
|
|
141
|
-
declare const map: <T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<
|
|
152
|
+
declare const map: <T, R = T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<R>;
|
|
142
153
|
|
|
143
154
|
declare const mask: (mask: string, text: string) => string;
|
|
144
155
|
|
|
@@ -146,6 +157,8 @@ declare const maskMoney: (value: string, text: string, decimal: number) => strin
|
|
|
146
157
|
|
|
147
158
|
declare const maskReverse: (value: string, text: string) => string;
|
|
148
159
|
|
|
160
|
+
declare const maskTest: (value: string, text: string) => boolean;
|
|
161
|
+
|
|
149
162
|
declare const clamp: (value: number, min: number, max: number) => number;
|
|
150
163
|
|
|
151
164
|
declare const lerp: (value1: number, value2: number, amount: number) => number;
|
|
@@ -153,7 +166,9 @@ declare const lerp: (value1: number, value2: number, amount: number) => number;
|
|
|
153
166
|
declare const random: (min: number, max: number) => number;
|
|
154
167
|
|
|
155
168
|
declare const allowed: (allowed: Array<string>, target: TypeObjectAny) => TypeObjectAny;
|
|
169
|
+
|
|
156
170
|
declare const denied: (denied: Array<string>, target: TypeObjectAny) => TypeObjectAny;
|
|
171
|
+
|
|
157
172
|
declare const merge: (target: TypeObjectAny, ...sources: Array<TypeObjectAny>) => TypeObjectAny;
|
|
158
173
|
|
|
159
174
|
type ForEachCallback<T> = TypeCallback<T, number, Array<T>, Promise<void>>;
|
|
@@ -325,4 +340,4 @@ declare class Validation {
|
|
|
325
340
|
|
|
326
341
|
type ValidationCallbackAsync = (value: any) => Promise<IValidationResult>;
|
|
327
342
|
|
|
328
|
-
export { type CacheCallback, type EachCallback, type EachValue, type EventCallback, EventEmitter, type EventList, type ForEachCallback, Hash, type IDebounceCallbacks, type IEventDisposable, type ILoopCallbacks, type IValidationResult, RandomString, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeObject, type TypeObjectAny, type TypeObjectFunction, type TypeObjectNumber, type TypeObjectString, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
|
343
|
+
export { type CacheCallback, type EachCallback, type EachValue, type EventCallback, EventEmitter, type EventList, type ForEachCallback, Hash, type IDebounceCallbacks, type IEventDisposable, type ILoopCallbacks, type IValidationResult, RandomString, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeNullable, type TypeObject, type TypeObjectAny, type TypeObjectBoolean, type TypeObjectFunction, type TypeObjectNullable, type TypeObjectNumber, type TypeObjectString, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cssColor, cutText, dateFormat, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, randomCase, randomElement, rangeArray, rgbToHex, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, tintColor, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.3
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
const t=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,s,r)=>t+(s-t)*e(r,0,1),r=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),i=t=>t.length?t[r(0,t.length-1)]:null,n=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),h=t=>{for(let e=t.length-1;e>0;e--){const s=r(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},a={},u=t=>null==t?"null":a[a.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>a["[object "+t+"]"]=t.toLowerCase()));const l=t=>"array"==u(t),o=t=>"boolean"==u(t),c=t=>"null"!=u(t),p=t=>"number"==u(t),m=t=>p(t)&&!!(t%1),v=t=>"function"==u(t),d=t=>p(t)&&!(t%1),f=t=>null===t,y=t=>"object"==u(t),g=t=>y(t)&&t.constructor===Object,_=t=>"string"==u(t),x=t=>void 0===t,b=t=>l(t)?t:c(t)?[t]:[],w=t=>"true"===t||"1"===t,k=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},S=t=>("0"+t).slice(-2),j=t=>parseInt(t,16)||0,C=(t,s)=>{return(r=j(t)+s,e(r,0,255)).toString(16);var r},O=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>j(t))))||[]},A=(t,e,s)=>"#"+S(t.toString(16))+S(e.toString(16))+S(s.toString(16)),I=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>S(C(t,e))));class M{constructor(t){this.key=this.shuffle(M.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[M.dictionary.indexOf(t[s]),M.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return M.dictionary[(s?t-e+M.dictionary.length:t+e)%M.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,h]=this.index(t,e,i);r+=-1===n||-1===h?t[i]:this.result(n,h,s)}return r}}M.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const L=t=>Math.random().toString(16).slice(t),U=()=>L(-3),N=()=>L(-4),q=()=>N()+N(),E=()=>q()+q(),F=()=>U()+N(),T=()=>E()+E()+E()+E(),Y=()=>q()+"-"+N()+"-4"+U()+"-"+N()+"-"+(q()+N()),H=t=>("0"+t.toString()).slice(-2),P={"%y":t=>H(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>H(t.getMonth()+1),"%d":t=>H(t.getDate()),"%H":t=>H(t.getHours()),"%M":t=>H(t.getMinutes()),"%S":t=>H(t.getSeconds())},Z=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>P[e]?P[e](t):e.slice(1))),$=36e5,z=24*$,D={ms:1,s:1e3,m:6e4,h:$,d:z,w:7*z,y:315576e5},J=["y","d","h","m","s","ms"],W=t=>{const e=[];return J.forEach((s=>{const r=D[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},B=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*D[s[2]]||0;return r},G=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class K{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}}function Q(t,e,s,r){return new(s||(s=Promise))((function(i,n){function h(t){try{u(r.next(t))}catch(t){n(t)}}function a(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const R=(t,e)=>{let s=!1;const r=()=>Q(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},V=()=>{},X=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},tt=(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},et=(t,e,s)=>l(t)?X(t,e,s):y(t)?tt(t,e,s):t,st=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},rt=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},it=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),nt=t=>it(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),ht=nt,at=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},ut=/&(amp|apos|lt|gt|nbsp|quot);/g,lt={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},ot=t=>t.replace(ut,((t,e)=>lt[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),ct=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),pt=t=>/^[\w]$/.test(t),mt=t=>/^[\d]$/.test(t),vt=t=>/^[A-Za-z]$/.test(t),dt=t=>/^[^\w]$/.test(t),ft=t=>{if(p(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},yt=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),gt=t=>h(t.split("")).join("");class _t{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 _t).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new _t).lower()}make(t){let e="";this.hasSpace&&(e+=this._space),this.hasNumbers&&(e+=this._numbers),this.hasSymbols&&(e+=this._symbols),this.hasUpper&&!this.hasChars&&(e+=this._upper),this.hasLower&&!this.hasChars&&(e+=this._lower),this.hasChars&&(e+=this._chars);let s="";if(t&&e.length){e=gt(e);for(let i=0;i<t;i++)s+=e.charAt(r(0,e.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new _t).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new _t).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new _t).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new _t).upper()}}const xt=t=>t.split("").reverse().join(""),bt=t=>it(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),wt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),kt={"#":t=>t.value,0:t=>wt(t,mt),9:t=>wt(t,mt),A:t=>wt(t,vt),Z:t=>wt(t,vt),L:t=>wt(t,vt).toLowerCase(),U:t=>wt(t,vt).toUpperCase(),S:t=>wt(t,dt)},St=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:h}=t;return x(n)||x(h)?"":(t=>kt[t.key]?kt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+St(t)},jt=(t,e)=>St({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),Ct=(t,e)=>xt(jt(xt(t),xt(e))),Ot=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),Ct(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),At=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),It=(t,e)=>At(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Mt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(g(t)&&g(s))for(const e in s)g(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Mt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Mt(t,...e)},Lt=(t,e,s)=>Q(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(V)})),Ut=(t,e,s)=>Q(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(V)})),Nt=t=>new Promise((e=>setTimeout(e,t)));class qt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=c(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!_(this.validate(t).error)}testAsync(t){return Q(this,void 0,void 0,(function*(){return!_((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return Q(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class Et extends qt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(l(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class Ft extends qt{constructor(){super(),this._type="boolean",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:c(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Tt extends qt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:c(t)?parseFloat(t):t})))}}class Yt extends qt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(y(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),c(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!y(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?At(e,t):t}}))}}class Ht extends qt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>_(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:nt(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:jt(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:y(t)?JSON.stringify(t):c(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:bt(t)})))}}class Pt{static array(t){return(new Et).items(t)}static boolean(){return new Ft}static number(){return new Tt}static object(t){return(new Yt).keys(t)}static string(){return new Ht}test(t,...e){return!_(Pt.validate(t,...e).error)}testAsync(t,...e){return Q(this,void 0,void 0,(function*(){return!_((yield Pt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return Q(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}export{K as EventEmitter,M as Hash,_t as RandomString,Pt as Validation,qt as ValidationBase,At as allowed,k as cache,ht as camelCase,t as chunkArray,e as clamp,at as cutText,Z as dateFormat,G as debounce,ot as decodeHTML,It as denied,et as each,ct as encodeHTML,Lt as forEachAsync,X as forEachIndex,tt as forEachKey,Ut as forEachSeries,st as forLoop,O as hexToRgb,l as isArray,o as isBoolean,pt as isCharacter,c as isDef,mt as isDigit,m as isFloat,v as isFunction,d as isInteger,vt as isLetter,f as isNull,p as isNumber,y as isObject,g as isPlainObject,_ as isString,dt as isSymbol,x as isUndefined,s as lerp,R as loop,nt as lowerCamelCase,rt as map,jt as mask,Ot as maskMoney,Ct as maskReverse,Mt as merge,W as msToString,V as noop,B as parseMs,ft as parseSize,r as random,yt as randomCase,i as randomElement,n as rangeArray,A as rgbToHex,I as shadeColor,h as shuffleArray,gt as shuffleString,F as simpleID,Nt as sleep,xt as stringReverse,b as toArray,w as toBool,u as type,T as uniqueID,bt as upperCamelCase,Y as uuidv4};
|
|
7
|
+
const t=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,s,r)=>t+(s-t)*e(r,0,1),r=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),i=t=>t.length?t[r(0,t.length-1)]:null,n=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),h=t=>{for(let e=t.length-1;e>0;e--){const s=r(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},a={},u=t=>null==t?"null":a[a.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>a["[object "+t+"]"]=t.toLowerCase()));const l=t=>"array"==u(t),o=t=>"boolean"==u(t),c=t=>"null"!=u(t),p=t=>"number"==u(t),m=t=>p(t)&&!!(t%1),d=t=>"function"==u(t),v=t=>p(t)&&!(t%1),f=t=>null===t,y=t=>"object"==u(t),g=t=>y(t)&&t.constructor===Object,_=t=>"string"==u(t),x=t=>void 0===t,b=t=>l(t)?t:c(t)?[t]:[],w=t=>"true"===t||"1"===t,k=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},j=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},S=(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},C=(t,e,s)=>l(t)?j(t,e,s):y(t)?S(t,e,s):t,O=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},A=(t,e,s)=>Object.fromEntries(Object.entries(t).map((([r,i])=>[r,e.call(s,i,r,t)]))),I=t=>parseInt(null!=t?t:"0",16)||0,M=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map(I))||[]},L=t=>("0"+t.toString(16)).slice(-2),U=(t,e,s)=>"#"+L(t)+L(e)+L(s),N=(t,s)=>{const r=M(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t*s)));return U(i,n,h)},E=(t,s)=>{const r=M(t);s=e(s,0,1);const[i,n,h]=r.map((t=>Math.round(t+(255-t)*s)));return U(i,n,h)},q={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71},F=(t,e)=>A(null!=e?e:q,(e=>0==e?t:e>0?E(t,e):N(t,1- -1*e)));class T{constructor(t){this.key=this.shuffle(T.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[T.dictionary.indexOf(t[s]),T.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return T.dictionary[(s?t-e+T.dictionary.length:t+e)%T.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}}T.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const Y=t=>Math.random().toString(16).slice(t),H=()=>Y(-3),P=()=>Y(-4),Z=()=>P()+P(),$=()=>Z()+Z(),z=()=>H()+P(),D=()=>$()+$()+$()+$(),J=()=>Z()+"-"+P()+"-4"+H()+"-"+P()+"-"+(Z()+P()),W=t=>("0"+t.toString()).slice(-2),B={"%y":t=>W(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>W(t.getMonth()+1),"%d":t=>W(t.getDate()),"%H":t=>W(t.getHours()),"%M":t=>W(t.getMinutes()),"%S":t=>W(t.getSeconds())},G=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>B[e]?B[e](t):e.slice(1))),K=36e5,Q=24*K,R={ms:1,s:1e3,m:6e4,h:K,d:Q,w:7*Q,y:315576e5},V=["y","d","h","m","s","ms"],X=t=>{const e=[];return V.forEach((s=>{const r=R[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},tt=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*R[s[2]]||0;return r},et=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class st{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 rt(t,e,s,r){return new(s||(s=Promise))((function(i,n){function h(t){try{u(r.next(t))}catch(t){n(t)}}function a(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(h,a)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const it=(t,e)=>{let s=!1;const r=()=>rt(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},nt=()=>{},ht=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),at=t=>ht(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),ut=at,lt=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},ot=/&(amp|apos|lt|gt|nbsp|quot);/g,ct={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},pt=t=>t.replace(ot,((t,e)=>ct[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),mt=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),dt=t=>/^[\w]$/.test(t),vt=t=>/^[\d]$/.test(t),ft=t=>/^[A-Za-z]$/.test(t),yt=t=>/^[^\w]$/.test(t),gt=t=>{if(p(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},_t=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),xt=t=>h(t.split("")).join("");class bt{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 bt).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new bt).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=xt(e);for(let i=0;i<t;i++)s+=e.charAt(r(0,e.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new bt).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new bt).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new bt).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new bt).upper()}}const wt=t=>t.split("").reverse().join(""),kt=t=>ht(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),jt=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),St={"#":t=>t.value,0:t=>jt(t,vt),9:t=>jt(t,vt),A:t=>jt(t,ft),Z:t=>jt(t,ft),L:t=>jt(t,ft).toLowerCase(),U:t=>jt(t,ft).toUpperCase(),S:t=>jt(t,yt)},Ct=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:h}=t;return x(n)||x(h)?"":(t=>St[t.key]?St[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Ct(t)},Ot=(t,e)=>Ct({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),At=(t,e)=>wt(Ot(wt(t),wt(e))),It=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),At(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),Mt=(t,e)=>t.length==Ot(t,e).length,Lt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Ut=(t,e)=>Lt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Nt=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(g(t)&&g(s))for(const e in s)g(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Nt(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Nt(t,...e)},Et=(t,e,s)=>rt(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(nt)})),qt=(t,e,s)=>rt(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(nt)})),Ft=t=>new Promise((e=>setTimeout(e,t)));class Tt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=c(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>c(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!_(this.validate(t).error)}testAsync(t){return rt(this,void 0,void 0,(function*(){return!_((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return rt(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 Yt extends Tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(l(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class Ht extends Tt{constructor(){super(),this._type="boolean",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:c(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Pt extends Tt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:c(t)?parseFloat(t):t})))}}class Zt extends Tt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(y(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),c(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!y(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Lt(e,t):t}}))}}class $t extends Tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>_(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:at(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:Ot(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:y(t)?JSON.stringify(t):c(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:kt(t)})))}}class zt{static array(t){return(new Yt).items(t)}static boolean(){return new Ht}static number(){return new Pt}static object(t){return(new Zt).keys(t)}static string(){return new $t}test(t,...e){return!_(zt.validate(t,...e).error)}testAsync(t,...e){return rt(this,void 0,void 0,(function*(){return!_((yield zt.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 rt(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{st as EventEmitter,T as Hash,bt as RandomString,zt as Validation,Tt as ValidationBase,Lt as allowed,k as cache,ut as camelCase,t as chunkArray,e as clamp,F as cssColor,lt as cutText,G as dateFormat,et as debounce,pt as decodeHTML,Ut as denied,C as each,mt as encodeHTML,Et as forEachAsync,j as forEachIndex,S as forEachKey,qt as forEachSeries,O as forLoop,M as hexToRgb,l as isArray,o as isBoolean,dt as isCharacter,c as isDef,vt as isDigit,m as isFloat,d as isFunction,v as isInteger,ft as isLetter,f as isNull,p as isNumber,y as isObject,g as isPlainObject,_ as isString,yt as isSymbol,x as isUndefined,s as lerp,it as loop,at as lowerCamelCase,A as map,Ot as mask,It as maskMoney,At as maskReverse,Mt as maskTest,Nt as merge,X as msToString,nt as noop,tt as parseMs,gt as parseSize,r as random,_t as randomCase,i as randomElement,n as rangeArray,U as rgbToHex,N as shadeColor,h as shuffleArray,xt as shuffleString,z as simpleID,Ft as sleep,wt as stringReverse,E as tintColor,b as toArray,w as toBool,u as type,D as uniqueID,kt as upperCamelCase,J as uuidv4};
|
package/dist/index.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.3.
|
|
2
|
+
* @andrewcaires/utils.js v0.3.3
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),l=t=>"null"!=n(t),u=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=t=>"string"==n(t),m=t=>void 0===t,d=t=>("0"+t).slice(-2),v=t=>parseInt(t,16)||0,f=(t,s)=>{return(r=v(t)+s,e(r,0,255)).toString(16);var r};class y{constructor(t){this.key=this.shuffle(y.dictionary,t,!1)}encrypt(t){return this.shuffle(t,this.key,!1)}decrypt(t){return this.shuffle(t,this.key,!0)}index(t,e,s){return[y.dictionary.indexOf(t[s]),y.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return y.dictionary[(s?t-e+y.dictionary.length:t+e)%y.dictionary.length]}shuffle(t,e,s){let r="";for(let i=0;i<t.length;i++){const[n,a]=this.index(t,e,i);r+=-1===n||-1===a?t[i]:this.result(n,a,s)}return r}}y.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const g=t=>Math.random().toString(16).slice(t),_=()=>g(-3),x=()=>g(-4),b=()=>x()+x(),w=()=>b()+b(),k=t=>("0"+t.toString()).slice(-2),S={"%y":t=>k(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>k(t.getMonth()+1),"%d":t=>k(t.getDate()),"%H":t=>k(t.getHours()),"%M":t=>k(t.getMinutes()),"%S":t=>k(t.getSeconds())},C=36e5,j=24*C,O={ms:1,s:1e3,m:6e4,h:C,d:j,w:7*j,y:315576e5},A=["y","d","h","m","s","ms"];function I(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{l(r.next(t))}catch(t){n(t)}}function h(t){try{l(r.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}l((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const M=()=>{},E=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},L=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},T=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),U=t=>T(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),N=U,F=/&(amp|apos|lt|gt|nbsp|quot);/g,q={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},H=t=>/^[\d]$/.test(t),D=t=>/^[A-Za-z]$/.test(t),B=t=>/^[^\w]$/.test(t),P=t=>r(t.split("")).join("");class R{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new R).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new R).lower()}make(t){let 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=P(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 R).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new R).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new R).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new R).upper()}}const Y=t=>t.split("").reverse().join(""),z=t=>T(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),J=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),Z={"#":t=>t.value,0:t=>J(t,H),9:t=>J(t,H),A:t=>J(t,D),Z:t=>J(t,D),L:t=>J(t,D).toLowerCase(),U:t=>J(t,D).toUpperCase(),S:t=>J(t,B)},$=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return m(n)||m(a)?"":(t=>Z[t.key]?Z[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+$(t)},V=(t,e)=>$({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),W=(t,e)=>Y(V(Y(t),Y(e))),K=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),G=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=G(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return G(t,...e)};class Q{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=l(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>l(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return I(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return I(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class X extends Q{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 tt extends Q{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:l(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class et extends Q{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>u(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?parseFloat(t):t})))}}class st extends Q{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),l(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?K(e,t):t}}))}}class rt extends Q{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:U(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:V(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?JSON.stringify(t):l(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:z(t)})))}}class it{static array(t){return(new X).items(t)}static boolean(){return new tt}static number(){return new et}static object(t){return(new st).keys(t)}static string(){return new rt}test(t,...e){return!p(it.validate(t,...e).error)}testAsync(t,...e){return I(this,void 0,void 0,(function*(){return!p((yield it.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return I(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},t.Hash=y,t.RandomString=R,t.Validation=it,t.ValidationBase=Q,t.allowed=K,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=N,t.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},t.clamp=e,t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>S[e]?S[e](t):e.slice(1))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(F,((t,e)=>q[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>K(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?E(t,e,s):o(t)?L(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>I(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(M)})),t.forEachIndex=E,t.forEachKey=L,t.forEachSeries=(t,e,s)=>I(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(M)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>v(t))))||[]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=l,t.isDigit=H,t.isFloat=t=>u(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>u(t)&&!(t%1),t.isLetter=D,t.isNull=t=>null===t,t.isNumber=u,t.isObject=o,t.isPlainObject=c,t.isString=p,t.isSymbol=B,t.isUndefined=m,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>I(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=U,t.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},t.mask=V,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),W(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=W,t.merge=G,t.msToString=t=>{const e=[];return A.forEach((s=>{const r=O[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=M,t.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*O[s[2]]||0;return r},t.parseSize=t=>{if(u(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.randomElement=t=>t.length?t[s(0,t.length-1)]:null,t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=(t,e,s)=>"#"+d(t.toString(16))+d(e.toString(16))+d(s.toString(16)),t.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>d(f(t,e)))),t.shuffleArray=r,t.shuffleString=P,t.simpleID=()=>_()+x(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=Y,t.toArray=t=>a(t)?t:l(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>w()+w()+w()+w(),t.upperCamelCase=z,t.uuidv4=()=>b()+"-"+x()+"-4"+_()+"-"+x()+"-"+(b()+x()),t}({});
|
|
7
|
+
var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),u=t=>"null"!=n(t),l=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=t=>"string"==n(t),m=t=>void 0===t,d=(t,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),x=(t,e,s)=>"#"+_(t)+_(e)+_(s),b=(t,s)=>{const r=g(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t*s)));return x(i,n,a)},w=(t,s)=>{const r=g(t);s=e(s,0,1);const[i,n,a]=r.map((t=>Math.round(t+(255-t)*s)));return x(i,n,a)},k={50:.95,100:.9,200:.75,300:.6,400:.3,500:0,600:-.1,700:-.25,800:-.4,900:-.55,950:-.71};class C{constructor(t){this.key=this.shuffle(C.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[C.dictionary.indexOf(t[s]),C.dictionary.indexOf(e[s%e.length])]}result(t,e,s){return C.dictionary[(s?t-e+C.dictionary.length:t+e)%C.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}}C.dictionary="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";const S=t=>Math.random().toString(16).slice(t),j=()=>S(-3),O=()=>S(-4),A=()=>O()+O(),I=()=>A()+A(),M=t=>("0"+t.toString()).slice(-2),E={"%y":t=>M(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>M(t.getMonth()+1),"%d":t=>M(t.getDate()),"%H":t=>M(t.getHours()),"%M":t=>M(t.getMinutes()),"%S":t=>M(t.getSeconds())},L=36e5,T=24*L,U={ms:1,s:1e3,m:6e4,h:L,d:T,w:7*T,y:315576e5},N=["y","d","h","m","s","ms"];function F(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{u(r.next(t))}catch(t){n(t)}}function h(t){try{u(r.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}u((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const q=()=>{},H=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),D=t=>H(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),B=D,P=/&(amp|apos|lt|gt|nbsp|quot);/g,R={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},Y=t=>/^[\d]$/.test(t),z=t=>/^[A-Za-z]$/.test(t),J=t=>/^[^\w]$/.test(t),Z=t=>r(t.split("")).join("");class ${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 $).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new $).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=Z(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 $).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new $).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new $).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new $).upper()}}const V=t=>t.split("").reverse().join(""),W=t=>H(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),K=(t,e)=>e(t.value)?t.value:(t=>(t.maskIndex--,""))(t),G={"#":t=>t.value,0:t=>K(t,Y),9:t=>K(t,Y),A:t=>K(t,z),Z:t=>K(t,z),L:t=>K(t,z).toLowerCase(),U:t=>K(t,z).toUpperCase(),S:t=>K(t,J)},Q=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return m(n)||m(a)?"":(t=>G[t.key]?G[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+Q(t)},X=(t,e)=>Q({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),tt=(t,e)=>V(X(V(t),V(e))),et=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),st=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=st(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return st(t,...e)};class rt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=u(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>u(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return F(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return F(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class it extends rt{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 nt extends rt{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 at extends rt{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 ht extends rt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),u(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?et(e,t):t}}))}}class ut extends rt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:D(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:X(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?JSON.stringify(t):u(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:W(t)})))}}class lt{static array(t){return(new it).items(t)}static boolean(){return new nt}static number(){return new at}static object(t){return(new ht).keys(t)}static string(){return new ut}test(t,...e){return!p(lt.validate(t,...e).error)}testAsync(t,...e){return F(this,void 0,void 0,(function*(){return!p((yield lt.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 F(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=C,t.RandomString=$,t.Validation=lt,t.ValidationBase=rt,t.allowed=et,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=B,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.cssColor=(t,e)=>f(null!=e?e:k,(e=>0==e?t:e>0?w(t,e):b(t,1- -1*e))),t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>E[e]?E[e](t):e.slice(1))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(P,((t,e)=>R[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>et(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?d(t,e,s):o(t)?v(t,e,s):t,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>F(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(q)})),t.forEachIndex=d,t.forEachKey=v,t.forEachSeries=(t,e,s)=>F(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(q)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hexToRgb=g,t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=u,t.isDigit=Y,t.isFloat=t=>l(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>l(t)&&!(t%1),t.isLetter=z,t.isNull=t=>null===t,t.isNumber=l,t.isObject=o,t.isPlainObject=c,t.isString=p,t.isSymbol=J,t.isUndefined=m,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>F(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=D,t.map=f,t.mask=X,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),tt(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=tt,t.maskTest=(t,e)=>t.length==X(t,e).length,t.merge=st,t.msToString=t=>{const e=[];return N.forEach((s=>{const r=U[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=q,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])*U[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=x,t.shadeColor=b,t.shuffleArray=r,t.shuffleString=Z,t.simpleID=()=>j()+O(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=V,t.tintColor=w,t.toArray=t=>a(t)?t:u(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>I()+I()+I()+I(),t.upperCamelCase=W,t.uuidv4=()=>A()+"-"+O()+"-4"+j()+"-"+O()+"-"+(A()+O()),t}({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/utils.js",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "JavaScript utility library for web and nodejs development",
|
|
5
5
|
"main": "./dist/index.cjs.js",
|
|
6
6
|
"unpkg": "./dist/index.min.js",
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"dist"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "rollup --bundleConfigAsCjs -c ./rollup.config.js"
|
|
23
|
+
"build": "rollup --bundleConfigAsCjs -c ./rollup.config.js",
|
|
24
|
+
"dev": "ts-node-dev ./src/index.ts"
|
|
24
25
|
},
|
|
25
26
|
"repository": {
|
|
26
27
|
"type": "git",
|
|
@@ -42,14 +43,15 @@
|
|
|
42
43
|
},
|
|
43
44
|
"homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
|
|
44
45
|
"devDependencies": {
|
|
46
|
+
"@andrewcaires/node": "^1.1.3",
|
|
45
47
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
46
48
|
"@rollup/plugin-terser": "^0.4.4",
|
|
47
49
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
48
|
-
"@types/node": "^20.14.
|
|
49
|
-
"eslint": "^9.
|
|
50
|
-
"rollup": "^4.18.
|
|
50
|
+
"@types/node": "^20.14.10",
|
|
51
|
+
"eslint": "^9.7.0",
|
|
52
|
+
"rollup": "^4.18.1",
|
|
51
53
|
"rollup-plugin-dts": "^6.1.1",
|
|
52
54
|
"tslib": "^2.6.3",
|
|
53
|
-
"typescript-eslint": "^8.0.0-alpha.
|
|
55
|
+
"typescript-eslint": "^8.0.0-alpha.41"
|
|
54
56
|
}
|
|
55
|
-
}
|
|
57
|
+
}
|