@andrewcaires/utils.js 0.2.27 → 0.2.29
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 +40 -5
- package/dist/index.esm.js +2 -2
- package/dist/index.min.js +2 -2
- package/package.json +8 -8
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.29
|
|
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 e={},t=t=>null==t?"null":e[e.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>e["[object "+t+"]"]=t.toLowerCase()));const r=e=>"array"==t(e),s=e=>"boolean"==t(e),i=e=>"null"!=t(e),n=e=>"number"==t(e),a=e=>"object"==t(e),o=e=>a(e)&&e.constructor===Object,l=e=>"string"==t(e),h=e=>void 0===e,u=(e,t,r)=>Math.max(t,Math.min(r,e)),p=e=>("0"+e).slice(-2),c=e=>parseInt(e,16)||0,v=(e,t)=>{return(r=c(e)+t,u(r,0,255)).toString(16);var r},d=e=>Math.random().toString(16).slice(e),x=()=>d(-3),m=()=>d(-4),g=()=>m()+m(),f=()=>g()+g(),y=e=>("0"+e.toString()).slice(-2),_={"%y":e=>y(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>y(e.getMonth()+1),"%d":e=>y(e.getDate()),"%H":e=>y(e.getHours()),"%M":e=>y(e.getMinutes()),"%S":e=>y(e.getSeconds())},b=36e5,k=24*b,w={ms:1,s:1e3,m:6e4,h:b,d:k,w:7*k,y:315576e5},S=["y","d","h","m","s","ms"];function j(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{l(s.next(e))}catch(e){n(e)}}function o(e){try{l(s.throw(e))}catch(e){n(e)}}function l(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,o)}l((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const C=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},O=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length&&(s=i[n],!1!==t.call(r,e[s],s,e));n++);return e},A=()=>{},I="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",M=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=I.indexOf(e[i]),a=I.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:I[(r?n-a+62:n+a)%62]}return s},E=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),T=e=>E(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),F=T,L=/&(amp|apos|lt|gt|nbsp|quot);/g,q={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},N=e=>/^[\d]$/.test(e),D=e=>/^[A-Za-z]$/.test(e),H=e=>/^[^\w]$/.test(e),U=e=>e.split("").reverse().join(""),P=e=>E(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),B=e=>(e.maskIndex--,""),Y={"#":({value:e})=>e,0:e=>N(e.value)?e.value:B(e),9:e=>N(e.value)?e.value:B(e),A:e=>D(e.value)?e.value:B(e),Z:e=>D(e.value)?e.value:B(e),L:e=>D(e.value)?e.value.toLowerCase():B(e),U:e=>D(e.value)?e.value.toUpperCase():B(e),S:e=>H(e.value)?e.value:B(e)},$=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:i}=e;e.key=t[r],e.value=s[i];const{key:n,value:a}=e;return h(n)||h(a)?"":(e=>Y[e.key]?Y[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+$(e)},z=(e,t)=>$({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),R=z,Z=(e,t)=>U(R(U(e),U(t))),J=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{}),V=(e,...t)=>{if(!t.length)return e;const r=t.shift();if(o(e)&&o(r))for(const t in r)o(r[t])?(e[t]||(e=Object.assign(Object.assign({},e),{[t]:{}})),e[t]=V(e[t],r[t])):e=Object.assign(Object.assign({},e),{[t]:r[t]});return V(e,...t)};class W{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(e){return this._value=e,this.unshift((e=>{const t=i(e);return{break:!t,value:t?e:this._value}}))}error(e){return this._error=e,this}filter(...e){return this._filter=e,this.unshift((e=>{const t=-1==this._filter.indexOf(e);return{break:!t,value:t?e:void 0}}))}invalid(e){return{error:this._error?this._error:this.name?this.name+":"+e:e}}parse(){return this.unshift((e=>({value:e})))}push(e){return this.rules.push(e),this}required(){return this.unshift((e=>i(e)?{value:e}:this.invalid("["+this._type+"].required")))}test(e){return!l(this.validate(e).error)}testAsync(e){return j(this,void 0,void 0,(function*(){return!l((yield this.validateAsync(e)).error)}))}unshift(e){return this.rules.unshift(e),this}validate(e){if(this.custom.length)return this.invalid("[async]");let t={value:e};for(let e=0;e<this.rules.length;e++){if(t=this.rules[e](t.value),t.error)return{error:t.error};if(t.break)return{value:t.value}}return{value:t.value}}validateAsync(e){return j(this,void 0,void 0,(function*(){let t=this.validate(e);if(t.error)return{error:t.error};for(let e=0;e<this.custom.length;e++){if(t=yield this.custom[e](t.value),t.error)return{error:t.error};if(t.break)return{value:t.value}}return{value:t.value}}))}}class K extends W{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((e=>{if(r(e)){if(this._items){this._items.name="";for(let t=0;t<e.length;t++){const{error:r,value:s}=this._items.validate(e[t]);if(r)return this.invalid("["+this._type+"]< "+r+"["+t+"] >");e[t]=s}}return{value:e}}return this.invalid("["+this._type+"]")}))}between(e,t){return this.min(e),this.max(t)}items(e){return this._items=e,this}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}}class G extends W{constructor(){super(),this._type="boolean",this.push((e=>s(e)?{value:e}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((e=>({value:i(e)?-1!=[1,"1",!0,"true"].indexOf(e):e})))}}class Q extends W{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((e=>n(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}max(e){return this._max=e,this.push((e=>e<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:i(e)?parseFloat(e):e})))}}class X extends W{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(a(e)){const t=[],r={};return Object.keys(this._keys).forEach((s=>{const n=this._keys[s];n.name=s;const{error:a,value:o}=n.validate(e[s]);a&&t.push(a),i(o)&&(r[s]=o)})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{if(!a(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?J(t,e):e}}))}}class ee extends W{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>l(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}date(){return this.push((e=>isNaN(new Date(e).getTime())?this.invalid("["+this._type+"].date"):{value:e}))}email(){return this.push((e=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(e)?{value:e}:this.invalid("["+this._type+"].email")))}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((e=>({value:e.toLowerCase()})))}lowerCamelCase(){return this.push((e=>({value:T(e)})))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(e){return this.push((t=>({value:z(e,t)})))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:a(e)?JSON.stringify(e):i(e)?e.toString():e})))}required(){return this.push((e=>e.length?{value:e}:this.invalid("["+this._type+"].required")))}trim(){return this.push((e=>({value:e.trim()})))}upperCase(){return this.push((e=>({value:e.toUpperCase()})))}upperCamelCase(){return this.push((e=>({value:P(e)})))}}class te{static array(e){return(new K).items(e)}static boolean(){return new G}static number(){return new Q}static object(e){return(new X).keys(e)}static string(){return new ee}test(e,...t){return!l(te.validate(e,...t).error)}testAsync(e,...t){return j(this,void 0,void 0,(function*(){return!l((yield te.validateAsync(e,...t)).error)}))}static validate(e,...t){let r={value:e};for(let e=0;e<t.length;e++)if(r=t[e].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(e,...t){return j(this,void 0,void 0,(function*(){let r={value:e};for(let e=0;e<t.length;e++)if(r=yield t[e].validateAsync(r.value),r.error)return{error:r.error};return r}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(e,t){const r=this.events.get(e);return r?r.push(t):this.events.set(e,[t]),{dispose:()=>this.off(e,t)}}once(e,t){const r=s=>{t(s),this.off(e,r)};return this.on(e,r),{dispose:()=>this.off(e,r)}}off(e,t){const r=this.events.get(e);if(t){if(r){const e=r.indexOf(t);e>=0&&r.splice(e,1)}}else this.events.set(e,[])}emit(e,t){const r=this.events.get(e);r&&r.forEach((e=>e(t)))}},exports.Parse=class extends te{},exports.Validation=te,exports.ValidationBase=W,exports.allowed=J,exports.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},exports.camelCase=F,exports.chunkArray=(e,t)=>{const r=[];for(let s=0;s<e.length;s+=t)r.push(e.slice(s,s+t));return r},exports.clamp=u,exports.cutText=(e,t)=>{if(e.length>t){const r=e.substring(0,t).split(" ");return r.pop(),r.join(" ")+"..."}return e},exports.dateFormat=(e,t)=>t.replace(/%[yYmdHMS]/g,(t=>_[t]?_[t](e):t.slice(1))),exports.debounce=(e,t)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...i){s(),r=setTimeout((()=>e(...i)),t)}}},exports.decodeHTML=e=>e.replace(L,((e,t)=>q[t])).replace(/&#(\d+);/gi,((e,t)=>String.fromCharCode(parseInt(t,10)))),exports.denied=(e,t)=>J(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,t,s)=>r(e)?C(e,t,s):a(e)?O(e,t,s):null,exports.encodeHTML=e=>e.replace(/[^0-9A-Za-z ]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>j(void 0,void 0,void 0,(function*(){yield Promise.allSettled(e.map(((s,i)=>t.call(r,s,i,e)))).catch(A)})),exports.forEachIndex=C,exports.forEachKey=O,exports.forEachSeries=(e,t,r)=>j(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e).catch(A)})),exports.forLoop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.hash=e=>(e=M(I,e,!1),{generate:t=>M(t,e,!1),reverse:t=>M(t,e,!0)}),exports.hexToRgb=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map((e=>c(e))))||[]},exports.isArray=r,exports.isBoolean=s,exports.isCharacter=e=>/^[\w]$/.test(e),exports.isDef=i,exports.isDigit=N,exports.isFloat=e=>n(e)&&!!(e%1),exports.isFunction=e=>"function"==t(e),exports.isInteger=e=>n(e)&&!(e%1),exports.isLetter=D,exports.isNull=e=>null===e,exports.isNumber=n,exports.isObject=a,exports.isPlainObject=o,exports.isString=l,exports.isSymbol=H,exports.isUndefined=h,exports.lerp=(e,t,r)=>e+(t-e)*u(r,0,1),exports.loop=(e,t)=>{let r=!1;const s=()=>j(void 0,void 0,void 0,(function*(){r&&(yield e(),setTimeout(s,t))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},exports.lowerCamelCase=T,exports.map=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length;n++)s=i[n],e[s]=t.call(r,e[s],s,e);return e},exports.mask=z,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),Z(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=Z,exports.maskTest=(e,t)=>e.length==R(e,t).length,exports.merge=V,exports.msToString=e=>{const t=[];return S.forEach((r=>{const s=w[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},exports.noop=A,exports.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*w[r[2]]||0;return s},exports.parseSize=e=>{if(n(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},exports.random=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),exports.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),exports.rgbToHex=(e,t,r)=>"#"+p(e.toString(16))+p(t.toString(16))+p(r.toString(16)),exports.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>p(v(e,t)))),exports.shuffleArray=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},exports.simpleID=()=>x()+m(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=U,exports.toArray=e=>r(e)?e:i(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=t,exports.uniqueID=()=>f()+f()+f()+f(),exports.upperCamelCase=P,exports.uuidv4=()=>g()+"-"+m()+"-4"+x()+"-"+m()+"-"+(g()+m());
|
|
7
|
+
"use strict";const t=(t,e,s)=>Math.max(e,Math.min(s,t)),e=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),s=t=>{for(let s=t.length-1;s>0;s--){const r=e(0,s);[t[s],t[r]]=[t[r],t[s]]}return t},r={},i=t=>null==t?"null":r[r.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>r["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),o=t=>"number"==i(t),l=t=>"object"==i(t),u=t=>l(t)&&t.constructor===Object,p=t=>"string"==i(t),c=t=>void 0===t,m=t=>("0"+t).slice(-2),v=t=>parseInt(t,16)||0,d=(e,s)=>{return(r=v(e)+s,t(r,0,255)).toString(16);var r},x=t=>Math.random().toString(16).slice(t),g=()=>x(-3),f=()=>x(-4),y=()=>f()+f(),_=()=>y()+y(),b=t=>("0"+t.toString()).slice(-2),w={"%y":t=>b(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>b(t.getMonth()+1),"%d":t=>b(t.getDate()),"%H":t=>b(t.getHours()),"%M":t=>b(t.getMinutes()),"%S":t=>b(t.getSeconds())},k=36e5,S=24*k,C={ms:1,s:1e3,m:6e4,h:k,d:S,w:7*S,y:315576e5},j=["y","d","h","m","s","ms"];function O(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{o(r.next(t))}catch(t){n(t)}}function h(t){try{o(r.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}o((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const A=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},I=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},M=()=>{},L="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",E=(t,e,s)=>{let r="";for(let i=0;i<t.length;i++){const n=L.indexOf(t[i]),a=L.indexOf(e[i%e.length]);r+=-1===n||-1===a?t[i]:L[(s?n-a+62:n+a)%62]}return r},T=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),N=t=>T(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),U=N,F=/&(amp|apos|lt|gt|nbsp|quot);/g,q={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},D=t=>/^[\d]$/.test(t),H=t=>/^[A-Za-z]$/.test(t),P=t=>/^[^\w]$/.test(t),B=t=>s(t.split("")).join("");class R{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new R).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new R).lower()}make(t){let s="";this.hasSpace&&(s+=this._space),this.hasNumbers&&(s+=this._numbers),this.hasSymbols&&(s+=this._symbols),this.hasUpper&&!this.hasChars&&(s+=this._upper),this.hasLower&&!this.hasChars&&(s+=this._lower),this.hasChars&&(s+=this._chars);let r="";if(t&&s.length){s=B(s);for(let i=0;i<t;i++)r+=s.charAt(e(0,s.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new R).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new R).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new R).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new R).upper()}}const Y=t=>t.split("").reverse().join(""),z=t=>T(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Z=t=>(t.maskIndex--,""),$={"#":({value:t})=>t,0:t=>D(t.value)?t.value:Z(t),9:t=>D(t.value)?t.value:Z(t),A:t=>H(t.value)?t.value:Z(t),Z:t=>H(t.value)?t.value:Z(t),L:t=>H(t.value)?t.value.toLowerCase():Z(t),U:t=>H(t.value)?t.value.toUpperCase():Z(t),S:t=>P(t.value)?t.value:Z(t)},J=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return c(n)||c(a)?"":(t=>$[t.key]?$[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+J(t)},V=(t,e)=>J({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),W=V,K=(t,e)=>Y(W(Y(t),Y(e))),G=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Q=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(u(t)&&u(s))for(const e in s)u(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Q(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Q(t,...e)};class X{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=h(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>h(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return O(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return O(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class tt extends X{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(n(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class et extends X{constructor(){super(),this._type="boolean",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:h(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class st extends X{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>o(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:h(t)?parseFloat(t):t})))}}class rt extends X{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(l(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),h(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!l(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?G(e,t):t}}))}}class it extends X{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:N(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:V(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?JSON.stringify(t):h(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:z(t)})))}}class nt{static array(t){return(new tt).items(t)}static boolean(){return new et}static number(){return new st}static object(t){return(new rt).keys(t)}static string(){return new it}test(t,...e){return!p(nt.validate(t,...e).error)}testAsync(t,...e){return O(this,void 0,void 0,(function*(){return!p((yield nt.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return O(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}exports.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},exports.Parse=class extends nt{},exports.RandomString=R,exports.Validation=nt,exports.ValidationBase=X,exports.allowed=G,exports.cache=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},exports.camelCase=U,exports.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},exports.clamp=t,exports.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>w[e]?w[e](t):e.slice(1))),exports.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},exports.decodeHTML=t=>t.replace(F,((t,e)=>q[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),exports.denied=(t,e)=>G(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,e,s)=>n(t)?A(t,e,s):l(t)?I(t,e,s):null,exports.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,s)=>O(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(M)})),exports.forEachIndex=A,exports.forEachKey=I,exports.forEachSeries=(t,e,s)=>O(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(M)})),exports.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},exports.hash=t=>(t=E(L,t,!1),{generate:e=>E(e,t,!1),reverse:e=>E(e,t,!0)}),exports.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>v(t))))||[]},exports.isArray=n,exports.isBoolean=a,exports.isCharacter=t=>/^[\w]$/.test(t),exports.isDef=h,exports.isDigit=D,exports.isFloat=t=>o(t)&&!!(t%1),exports.isFunction=t=>"function"==i(t),exports.isInteger=t=>o(t)&&!(t%1),exports.isLetter=H,exports.isNull=t=>null===t,exports.isNumber=o,exports.isObject=l,exports.isPlainObject=u,exports.isString=p,exports.isSymbol=P,exports.isUndefined=c,exports.lerp=(e,s,r)=>e+(s-e)*t(r,0,1),exports.loop=(t,e)=>{let s=!1;const r=()=>O(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},exports.lowerCamelCase=N,exports.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},exports.mask=V,exports.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),K(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),exports.maskReverse=K,exports.maskTest=(t,e)=>t.length==W(t,e).length,exports.merge=Q,exports.msToString=t=>{const e=[];return j.forEach((s=>{const r=C[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},exports.noop=M,exports.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*C[s[2]]||0;return r},exports.parseSize=t=>{if(o(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},exports.random=e,exports.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),exports.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),exports.rgbToHex=(t,e,s)=>"#"+m(t.toString(16))+m(e.toString(16))+m(s.toString(16)),exports.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>m(d(t,e)))),exports.shuffleArray=s,exports.shuffleString=B,exports.simpleID=()=>g()+f(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=Y,exports.toArray=t=>n(t)?t:h(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=i,exports.uniqueID=()=>_()+_()+_()+_(),exports.upperCamelCase=z,exports.uuidv4=()=>y()+"-"+f()+"-4"+g()+"-"+f()+"-"+(y()+f());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.29
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2024 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
7
|
declare const chunkArray: <T>(array: Array<T>, length: number) => Array<Array<T>>;
|
|
8
8
|
|
|
9
|
-
declare const rangeArray: (start: number, end: number, step?: number) => number
|
|
9
|
+
declare const rangeArray: (start: number, end: number, step?: number) => Array<number>;
|
|
10
10
|
|
|
11
|
-
declare const shuffleArray: (array: Array<
|
|
11
|
+
declare const shuffleArray: <T>(array: Array<T>) => Array<T>;
|
|
12
12
|
|
|
13
13
|
declare const toArray: (value: any) => Array<any>;
|
|
14
14
|
|
|
@@ -137,8 +137,11 @@ declare const denied: (denied: Array<string>, target: TypeObjectAny) => TypeObje
|
|
|
137
137
|
declare const merge: (target: TypeObjectAny, ...sources: Array<TypeObjectAny>) => TypeObjectAny;
|
|
138
138
|
|
|
139
139
|
type ForEachCallback<T> = TypeCallback<T, number, Array<T>, Promise<void>>;
|
|
140
|
+
|
|
140
141
|
declare const forEachAsync: <T>(array: Array<T>, callback: ForEachCallback<T>, thisArg?: any) => Promise<void>;
|
|
142
|
+
|
|
141
143
|
declare const forEachSeries: <T>(array: Array<T>, callback: ForEachCallback<T>, thisArg?: any) => Promise<void>;
|
|
144
|
+
|
|
142
145
|
declare const sleep: (ms: number) => Promise<void>;
|
|
143
146
|
|
|
144
147
|
declare const parseSize: (size: number | string) => string;
|
|
@@ -147,7 +150,7 @@ declare const camelCase: (str: string) => string;
|
|
|
147
150
|
|
|
148
151
|
declare const cutText: (str: string, length: number) => string;
|
|
149
152
|
|
|
150
|
-
declare const decodeHTML: (
|
|
153
|
+
declare const decodeHTML: (str: string) => string;
|
|
151
154
|
|
|
152
155
|
declare const encodeHTML: (str: string) => string;
|
|
153
156
|
|
|
@@ -161,6 +164,38 @@ declare const isSymbol: (str: string) => boolean;
|
|
|
161
164
|
|
|
162
165
|
declare const lowerCamelCase: (str: string) => string;
|
|
163
166
|
|
|
167
|
+
declare const randomCase: (str: string) => string;
|
|
168
|
+
|
|
169
|
+
declare class RandomString {
|
|
170
|
+
private hasSpace;
|
|
171
|
+
private readonly _space;
|
|
172
|
+
private hasNumbers;
|
|
173
|
+
private readonly _numbers;
|
|
174
|
+
private hasSymbols;
|
|
175
|
+
private readonly _symbols;
|
|
176
|
+
private hasUpper;
|
|
177
|
+
private readonly _upper;
|
|
178
|
+
private hasLower;
|
|
179
|
+
private readonly _lower;
|
|
180
|
+
private hasChars;
|
|
181
|
+
private readonly _chars;
|
|
182
|
+
chars(): RandomString;
|
|
183
|
+
static chars(): RandomString;
|
|
184
|
+
lower(): RandomString;
|
|
185
|
+
static lower(): RandomString;
|
|
186
|
+
make(length: number): string;
|
|
187
|
+
numbers(): RandomString;
|
|
188
|
+
static numbers(): RandomString;
|
|
189
|
+
space(): RandomString;
|
|
190
|
+
static space(): RandomString;
|
|
191
|
+
symbols(): RandomString;
|
|
192
|
+
static symbols(): RandomString;
|
|
193
|
+
upper(): RandomString;
|
|
194
|
+
static upper(): RandomString;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
declare const shuffleString: (str: string) => string;
|
|
198
|
+
|
|
164
199
|
declare const stringReverse: (str: string) => string;
|
|
165
200
|
|
|
166
201
|
declare const upperCamelCase: (str: string) => string;
|
|
@@ -271,4 +306,4 @@ declare class Validation {
|
|
|
271
306
|
declare class Parse extends Validation {
|
|
272
307
|
}
|
|
273
308
|
|
|
274
|
-
export { type CacheCallback, type EachCallback, type EventCallback, EventEmitter, type EventList, type ForEachCallback, type IDebounceCallbacks, type IEventDisposable, type IHash, type ILoopCallbacks, type IValidationResult, type LoopCallback, Parse, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeObject, type TypeObjectAny, type TypeObjectFunction, type TypeObjectNumber, type TypeObjectString, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hash, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, rangeArray, rgbToHex, shadeColor, shuffleArray, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
|
309
|
+
export { type CacheCallback, type EachCallback, type EventCallback, EventEmitter, type EventList, type ForEachCallback, type IDebounceCallbacks, type IEventDisposable, type IHash, type ILoopCallbacks, type IValidationResult, type LoopCallback, Parse, RandomString, type TypeArray, type TypeArrayAny, type TypeArrayFunction, type TypeArrayNumber, type TypeArrayString, type TypeCallback, type TypeCallbackArray, type TypeCallbackFunction, type TypeCallbackMap, type TypeCallbackObject, type TypeObject, type TypeObjectAny, type TypeObjectFunction, type TypeObjectNumber, type TypeObjectString, Validation, ValidationBase, type ValidationCallback, type ValidationCallbackAsync, allowed, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, denied, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, forLoop, hash, hexToRgb, isArray, isBoolean, isCharacter, isDef, isDigit, isFloat, isFunction, isInteger, isLetter, isNull, isNumber, isObject, isPlainObject, isString, isSymbol, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, merge, msToString, noop, parseMs, parseSize, random, randomCase, rangeArray, rgbToHex, shadeColor, shuffleArray, shuffleString, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.29
|
|
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 r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},e=(t,e,r=1)=>Array.from({length:(e-t)/r+1},((e,s)=>t+s*r)),r=t=>{for(let e=t.length-1;e>0;e--){const r=Math.floor(Math.random()*(e+1));[t[e],t[r]]=[t[r],t[e]]}return t},s={},i=t=>null==t?"null":s[s.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>s["[object "+t+"]"]=t.toLowerCase()));const n=t=>"array"==i(t),a=t=>"boolean"==i(t),h=t=>"null"!=i(t),l=t=>"number"==i(t),u=t=>l(t)&&!!(t%1),o=t=>"function"==i(t),c=t=>l(t)&&!(t%1),v=t=>null===t,p=t=>"object"==i(t),d=t=>p(t)&&t.constructor===Object,m=t=>"string"==i(t),g=t=>void 0===t,f=t=>n(t)?t:h(t)?[t]:[],y=t=>"true"===t||"1"===t,_=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},x=(t,e,r)=>Math.max(e,Math.min(r,t)),b=(t,e,r)=>t+(e-t)*x(r,0,1),w=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),k=t=>("0"+t).slice(-2),j=t=>parseInt(t,16)||0,O=(t,e)=>{return(r=j(t)+e,x(r,0,255)).toString(16);var r},S=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>j(t))))||[]},C=(t,e,r)=>"#"+k(t.toString(16))+k(e.toString(16))+k(r.toString(16)),A=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>k(O(t,e)))),I=t=>Math.random().toString(16).slice(t),M=()=>I(-3),q=()=>I(-4),E=()=>q()+q(),F=()=>E()+E(),L=()=>M()+q(),N=()=>F()+F()+F()+F(),T=()=>E()+"-"+q()+"-4"+M()+"-"+q()+"-"+(E()+q()),U=t=>("0"+t.toString()).slice(-2),Y={"%y":t=>U(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>U(t.getMonth()+1),"%d":t=>U(t.getDate()),"%H":t=>U(t.getHours()),"%M":t=>U(t.getMinutes()),"%S":t=>U(t.getSeconds())},$=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>Y[e]?Y[e](t):e.slice(1))),H=36e5,P=24*H,Z={ms:1,s:1e3,m:6e4,h:H,d:P,w:7*P,y:315576e5},z=["y","d","h","m","s","ms"],D=t=>{const e=[];return z.forEach((r=>{const s=Z[r],i=(t-t%s)/s;i&&(t-=i*s,e.push(i.toString()+r))})),e.join(" ")},J=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=e.exec(t));)r.index===e.lastIndex&&e.lastIndex++,s+=parseFloat(r[1])*Z[r[2]]||0;return s},W=(t,e)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...i){s(),r=setTimeout((()=>t(...i)),e)}}};class B{constructor(){this.events=new Map}on(t,e){const r=this.events.get(t);return r?r.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const r=s=>{e(s),this.off(t,r)};return this.on(t,r),{dispose:()=>this.off(t,r)}}off(t,e){const r=this.events.get(t);if(e){if(r){const t=r.indexOf(e);t>=0&&r.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const r=this.events.get(t);r&&r.forEach((t=>t(e)))}}function G(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{l(s.next(t))}catch(t){n(t)}}function h(t){try{l(s.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}l((s=s.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const K=(t,e)=>{let r=!1;const s=()=>G(void 0,void 0,void 0,(function*(){r&&(yield t(),setTimeout(s,e))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},Q=(t,e,r)=>n(t)?R(t,e,r):p(t)?V(t,e,r):null,R=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},V=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length&&(s=i[n],!1!==e.call(r,t[s],s,t));n++);return t},X=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},tt=(t,e,r)=>{let s="";const i=Object.keys(t);for(let n=0;n<i.length;n++)s=i[n],t[s]=e.call(r,t[s],s,t);return t},et=()=>{},rt="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",st=(t,e,r)=>{let s="";for(let i=0;i<t.length;i++){const n=rt.indexOf(t[i]),a=rt.indexOf(e[i%e.length]);s+=-1===n||-1===a?t[i]:rt[(r?n-a+62:n+a)%62]}return s},it=t=>(t=st(rt,t,!1),{generate:e=>st(e,t,!1),reverse:e=>st(e,t,!0)}),nt=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),at=t=>nt(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),ht=at,lt=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},ut=/&(amp|apos|lt|gt|nbsp|quot);/g,ot={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},ct=t=>t.replace(ut,((t,e)=>ot[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),vt=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),pt=t=>/^[\w]$/.test(t),dt=t=>/^[\d]$/.test(t),mt=t=>/^[A-Za-z]$/.test(t),gt=t=>/^[^\w]$/.test(t),ft=t=>t.split("").reverse().join(""),yt=t=>nt(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),_t=t=>(t.maskIndex--,""),xt={"#":({value:t})=>t,0:t=>dt(t.value)?t.value:_t(t),9:t=>dt(t.value)?t.value:_t(t),A:t=>mt(t.value)?t.value:_t(t),Z:t=>mt(t.value)?t.value:_t(t),L:t=>mt(t.value)?t.value.toLowerCase():_t(t),U:t=>mt(t.value)?t.value.toUpperCase():_t(t),S:t=>gt(t.value)?t.value:_t(t)},bt=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:r,text:s,textIndex:i}=t;t.key=e[r],t.value=s[i];const{key:n,value:a}=t;return g(n)||g(a)?"":(t=>xt[t.key]?xt[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+bt(t)},wt=(t,e)=>bt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),kt=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),St(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),jt=wt,Ot=(t,e)=>t.length==jt(t,e).length,St=(t,e)=>ft(jt(ft(t),ft(e))),Ct=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),At=(t,e)=>Ct(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),It=(t,...e)=>{if(!e.length)return t;const r=e.shift();if(d(t)&&d(r))for(const e in r)d(r[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=It(t[e],r[e])):t=Object.assign(Object.assign({},t),{[e]:r[e]});return It(t,...e)},Mt=(t,e,r)=>G(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((s,i)=>e.call(r,s,i,t)))).catch(et)})),qt=(t,e,r)=>G(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t).catch(et)})),Et=t=>new Promise((e=>setTimeout(e,t))),Ft=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"};class Lt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=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!m(this.validate(t).error)}testAsync(t){return G(this,void 0,void 0,(function*(){return!m((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return G(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 Nt extends Lt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(n(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:r,value:s}=this._items.validate(t[e]);if(r)return this.invalid("["+this._type+"]< "+r+"["+e+"] >");t[e]=s}}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 Lt{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 Ut extends Lt{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:h(t)?parseFloat(t):t})))}}class Yt extends Lt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(p(t)){const e=[],r={};return Object.keys(this._keys).forEach((s=>{const i=this._keys[s];i.name=s;const{error:n,value:a}=i.validate(t[s]);n&&e.push(n),h(a)&&(r[s]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!p(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Ct(e,t):t}}))}}class $t extends Lt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>m(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}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:wt(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:p(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:yt(t)})))}}class Ht{static array(t){return(new Nt).items(t)}static boolean(){return new Tt}static number(){return new Ut}static object(t){return(new Yt).keys(t)}static string(){return new $t}test(t,...e){return!m(Ht.validate(t,...e).error)}testAsync(t,...e){return G(this,void 0,void 0,(function*(){return!m((yield Ht.validateAsync(t,...e)).error)}))}static validate(t,...e){let r={value:t};for(let t=0;t<e.length;t++)if(r=e[t].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(t,...e){return G(this,void 0,void 0,(function*(){let r={value:t};for(let t=0;t<e.length;t++)if(r=yield e[t].validateAsync(r.value),r.error)return{error:r.error};return r}))}}class Pt extends Ht{}export{B as EventEmitter,Pt as Parse,Ht as Validation,Lt as ValidationBase,Ct as allowed,_ as cache,ht as camelCase,t as chunkArray,x as clamp,lt as cutText,$ as dateFormat,W as debounce,ct as decodeHTML,At as denied,Q as each,vt as encodeHTML,Mt as forEachAsync,R as forEachIndex,V as forEachKey,qt as forEachSeries,X as forLoop,it as hash,S as hexToRgb,n as isArray,a as isBoolean,pt as isCharacter,h as isDef,dt as isDigit,u as isFloat,o as isFunction,c as isInteger,mt as isLetter,v as isNull,l as isNumber,p as isObject,d as isPlainObject,m as isString,gt as isSymbol,g as isUndefined,b as lerp,K as loop,at as lowerCamelCase,tt as map,wt as mask,kt as maskMoney,St as maskReverse,Ot as maskTest,It as merge,D as msToString,et as noop,J as parseMs,Ft as parseSize,w as random,e as rangeArray,C as rgbToHex,A as shadeColor,r as shuffleArray,L as simpleID,Et as sleep,ft as stringReverse,f as toArray,y as toBool,i as type,N as uniqueID,yt as upperCamelCase,T as uuidv4};
|
|
7
|
+
const t=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},e=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),s=(t,e,s)=>Math.max(e,Math.min(s,t)),r=(t,e,r)=>t+(e-t)*s(r,0,1),i=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),n=t=>{for(let e=t.length-1;e>0;e--){const s=i(0,e);[t[e],t[s]]=[t[s],t[e]]}return t},h={},a=t=>null==t?"null":h[h.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>h["[object "+t+"]"]=t.toLowerCase()));const u=t=>"array"==a(t),l=t=>"boolean"==a(t),o=t=>"null"!=a(t),c=t=>"number"==a(t),p=t=>c(t)&&!!(t%1),v=t=>"function"==a(t),m=t=>c(t)&&!(t%1),d=t=>null===t,g=t=>"object"==a(t),f=t=>g(t)&&t.constructor===Object,y=t=>"string"==a(t),_=t=>void 0===t,x=t=>u(t)?t:o(t)?[t]:[],b=t=>"true"===t||"1"===t,w=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},k=t=>("0"+t).slice(-2),S=t=>parseInt(t,16)||0,j=(t,e)=>{return(r=S(t)+e,s(r,0,255)).toString(16);var r},C=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>S(t))))||[]},O=(t,e,s)=>"#"+k(t.toString(16))+k(e.toString(16))+k(s.toString(16)),A=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>k(j(t,e)))),I=t=>Math.random().toString(16).slice(t),M=()=>I(-3),L=()=>I(-4),U=()=>L()+L(),N=()=>U()+U(),q=()=>M()+L(),E=()=>N()+N()+N()+N(),F=()=>U()+"-"+L()+"-4"+M()+"-"+L()+"-"+(U()+L()),T=t=>("0"+t.toString()).slice(-2),Y={"%y":t=>T(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>T(t.getMonth()+1),"%d":t=>T(t.getDate()),"%H":t=>T(t.getHours()),"%M":t=>T(t.getMinutes()),"%S":t=>T(t.getSeconds())},H=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>Y[e]?Y[e](t):e.slice(1))),P=36e5,Z=24*P,$={ms:1,s:1e3,m:6e4,h:P,d:Z,w:7*Z,y:315576e5},z=["y","d","h","m","s","ms"],D=t=>{const e=[];return z.forEach((s=>{const r=$[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},J=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])*$[s[2]]||0;return r},W=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}};class B{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 G(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 K=(t,e)=>{let s=!1;const r=()=>G(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},Q=(t,e,s)=>u(t)?R(t,e,s):g(t)?V(t,e,s):null,R=(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},X=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},tt=(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},et=()=>{},st="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",rt=(t,e,s)=>{let r="";for(let i=0;i<t.length;i++){const n=st.indexOf(t[i]),h=st.indexOf(e[i%e.length]);r+=-1===n||-1===h?t[i]:st[(s?n-h+62:n+h)%62]}return r},it=t=>(t=rt(st,t,!1),{generate:e=>rt(e,t,!1),reverse:e=>rt(e,t,!0)}),nt=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),ht=t=>nt(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),at=ht,ut=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},lt=/&(amp|apos|lt|gt|nbsp|quot);/g,ot={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},ct=t=>t.replace(lt,((t,e)=>ot[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),pt=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),vt=t=>/^[\w]$/.test(t),mt=t=>/^[\d]$/.test(t),dt=t=>/^[A-Za-z]$/.test(t),gt=t=>/^[^\w]$/.test(t),ft=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),yt=t=>n(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=yt(e);for(let r=0;r<t;r++)s+=e.charAt(i(0,e.length-1))}return s}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new _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=>nt(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),wt=t=>(t.maskIndex--,""),kt={"#":({value:t})=>t,0:t=>mt(t.value)?t.value:wt(t),9:t=>mt(t.value)?t.value:wt(t),A:t=>dt(t.value)?t.value:wt(t),Z:t=>dt(t.value)?t.value:wt(t),L:t=>dt(t.value)?t.value.toLowerCase():wt(t),U:t=>dt(t.value)?t.value.toUpperCase():wt(t),S:t=>gt(t.value)?t.value:wt(t)},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 _(n)||_(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,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),It(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),Ot=jt,At=(t,e)=>t.length==Ot(t,e).length,It=(t,e)=>xt(Ot(xt(t),xt(e))),Mt=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),Lt=(t,e)=>Mt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),Ut=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(f(t)&&f(s))for(const e in s)f(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=Ut(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return Ut(t,...e)},Nt=(t,e,s)=>G(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(et)})),qt=(t,e,s)=>G(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(et)})),Et=t=>new Promise((e=>setTimeout(e,t))),Ft=t=>{if(c(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"};class 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=o(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=>o(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!y(this.validate(t).error)}testAsync(t){return G(this,void 0,void 0,(function*(){return!y((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 G(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(u(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=>l(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:o(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=>c(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?parseFloat(t):t})))}}class Zt extends Tt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(g(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:h}=i.validate(t[r]);n&&e.push(n),o(h)&&(s[r]=h)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!g(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Mt(e,t):t}}))}}class $t extends Tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>y(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:ht(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:g(t)?JSON.stringify(t):o(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:bt(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!y(zt.validate(t,...e).error)}testAsync(t,...e){return G(this,void 0,void 0,(function*(){return!y((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 G(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}class Dt extends zt{}export{B as EventEmitter,Dt as Parse,_t as RandomString,zt as Validation,Tt as ValidationBase,Mt as allowed,w as cache,at as camelCase,t as chunkArray,s as clamp,ut as cutText,H as dateFormat,W as debounce,ct as decodeHTML,Lt as denied,Q as each,pt as encodeHTML,Nt as forEachAsync,R as forEachIndex,V as forEachKey,qt as forEachSeries,X as forLoop,it as hash,C as hexToRgb,u as isArray,l as isBoolean,vt as isCharacter,o as isDef,mt as isDigit,p as isFloat,v as isFunction,m as isInteger,dt as isLetter,d as isNull,c as isNumber,g as isObject,f as isPlainObject,y as isString,gt as isSymbol,_ as isUndefined,r as lerp,K as loop,ht as lowerCamelCase,tt as map,jt as mask,Ct as maskMoney,It as maskReverse,At as maskTest,Ut as merge,D as msToString,et as noop,J as parseMs,Ft as parseSize,i as random,ft as randomCase,e as rangeArray,O as rgbToHex,A as shadeColor,n as shuffleArray,yt as shuffleString,q as simpleID,Et as sleep,xt as stringReverse,x as toArray,b as toBool,a as type,E as uniqueID,bt as upperCamelCase,F as uuidv4};
|
package/dist/index.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.29
|
|
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(e){"use strict";const t={},r=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const s=e=>"array"==r(e),i=e=>"boolean"==r(e),n=e=>"null"!=r(e),a=e=>"number"==r(e),l=e=>"object"==r(e),h=e=>l(e)&&e.constructor===Object,u=e=>"string"==r(e),o=e=>void 0===e,c=(e,t,r)=>Math.max(t,Math.min(r,e)),v=e=>("0"+e).slice(-2),p=e=>parseInt(e,16)||0,d=(e,t)=>{return(r=p(e)+t,c(r,0,255)).toString(16);var r},m=e=>Math.random().toString(16).slice(e),g=()=>m(-3),f=()=>m(-4),y=()=>f()+f(),_=()=>y()+y(),x=e=>("0"+e.toString()).slice(-2),b={"%y":e=>x(e.getFullYear()),"%Y":e=>e.getFullYear().toString(),"%m":e=>x(e.getMonth()+1),"%d":e=>x(e.getDate()),"%H":e=>x(e.getHours()),"%M":e=>x(e.getMinutes()),"%S":e=>x(e.getSeconds())},k=36e5,w=24*k,S={ms:1,s:1e3,m:6e4,h:k,d:w,w:7*w,y:315576e5},j=["y","d","h","m","s","ms"];function C(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{h(s.next(e))}catch(e){n(e)}}function l(e){try{h(s.throw(e))}catch(e){n(e)}}function h(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,l)}h((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const O=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},A=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length&&(s=i[n],!1!==t.call(r,e[s],s,e));n++);return e},I=()=>{},M="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",E=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=M.indexOf(e[i]),a=M.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:M[(r?n-a+62:n+a)%62]}return s},T=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),F=e=>T(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),L=F,q=/&(amp|apos|lt|gt|nbsp|quot);/g,N={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},U=e=>/^[\d]$/.test(e),D=e=>/^[A-Za-z]$/.test(e),H=e=>/^[^\w]$/.test(e),P=e=>e.split("").reverse().join(""),B=e=>T(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),Y=e=>(e.maskIndex--,""),$={"#":({value:e})=>e,0:e=>U(e.value)?e.value:Y(e),9:e=>U(e.value)?e.value:Y(e),A:e=>D(e.value)?e.value:Y(e),Z:e=>D(e.value)?e.value:Y(e),L:e=>D(e.value)?e.value.toLowerCase():Y(e),U:e=>D(e.value)?e.value.toUpperCase():Y(e),S:e=>H(e.value)?e.value:Y(e)},z=e=>{e.maskIndex++,e.textIndex++;const{mask:t,maskIndex:r,text:s,textIndex:i}=e;e.key=t[r],e.value=s[i];const{key:n,value:a}=e;return o(n)||o(a)?"":(e=>$[e.key]?$[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+z(e)},J=(e,t)=>z({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),R=J,Z=(e,t)=>P(R(P(e),P(t))),V=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{}),W=(e,...t)=>{if(!t.length)return e;const r=t.shift();if(h(e)&&h(r))for(const t in r)h(r[t])?(e[t]||(e=Object.assign(Object.assign({},e),{[t]:{}})),e[t]=W(e[t],r[t])):e=Object.assign(Object.assign({},e),{[t]:r[t]});return W(e,...t)};class K{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(e){return this._value=e,this.unshift((e=>{const t=n(e);return{break:!t,value:t?e:this._value}}))}error(e){return this._error=e,this}filter(...e){return this._filter=e,this.unshift((e=>{const t=-1==this._filter.indexOf(e);return{break:!t,value:t?e:void 0}}))}invalid(e){return{error:this._error?this._error:this.name?this.name+":"+e:e}}parse(){return this.unshift((e=>({value:e})))}push(e){return this.rules.push(e),this}required(){return this.unshift((e=>n(e)?{value:e}:this.invalid("["+this._type+"].required")))}test(e){return!u(this.validate(e).error)}testAsync(e){return C(this,void 0,void 0,(function*(){return!u((yield this.validateAsync(e)).error)}))}unshift(e){return this.rules.unshift(e),this}validate(e){if(this.custom.length)return this.invalid("[async]");let t={value:e};for(let e=0;e<this.rules.length;e++){if(t=this.rules[e](t.value),t.error)return{error:t.error};if(t.break)return{value:t.value}}return{value:t.value}}validateAsync(e){return C(this,void 0,void 0,(function*(){let t=this.validate(e);if(t.error)return{error:t.error};for(let e=0;e<this.custom.length;e++){if(t=yield this.custom[e](t.value),t.error)return{error:t.error};if(t.break)return{value:t.value}}return{value:t.value}}))}}class G extends K{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((e=>{if(s(e)){if(this._items){this._items.name="";for(let t=0;t<e.length;t++){const{error:r,value:s}=this._items.validate(e[t]);if(r)return this.invalid("["+this._type+"]< "+r+"["+t+"] >");e[t]=s}}return{value:e}}return this.invalid("["+this._type+"]")}))}between(e,t){return this.min(e),this.max(t)}items(e){return this._items=e,this}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}}class Q extends K{constructor(){super(),this._type="boolean",this.push((e=>i(e)?{value:e}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((e=>({value:n(e)?-1!=[1,"1",!0,"true"].indexOf(e):e})))}}class X extends K{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((e=>a(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}max(e){return this._max=e,this.push((e=>e<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}min(e){return this._min=e,this.push((e=>e>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:n(e)?parseFloat(e):e})))}}class ee extends K{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(l(e)){const t=[],r={};return Object.keys(this._keys).forEach((s=>{const i=this._keys[s];i.name=s;const{error:a,value:l}=i.validate(e[s]);a&&t.push(a),n(l)&&(r[s]=l)})),t.length?this.invalid("["+this._type+"]{ "+t.join(", ")+" }"):{value:r}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{if(!l(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?V(t,e):e}}))}}class te extends K{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>u(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}date(){return this.push((e=>isNaN(new Date(e).getTime())?this.invalid("["+this._type+"].date"):{value:e}))}email(){return this.push((e=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(e)?{value:e}:this.invalid("["+this._type+"].email")))}length(e){return this._length=e,this.push((e=>e.length==this._length?{value:e}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((e=>({value:e.toLowerCase()})))}lowerCamelCase(){return this.push((e=>({value:F(e)})))}max(e){return this._max=e,this.push((e=>e.length<=this._max?{value:e}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(e){return this.push((t=>({value:J(e,t)})))}min(e){return this._min=e,this.push((e=>e.length>=this._min?{value:e}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((e=>({value:l(e)?JSON.stringify(e):n(e)?e.toString():e})))}required(){return this.push((e=>e.length?{value:e}:this.invalid("["+this._type+"].required")))}trim(){return this.push((e=>({value:e.trim()})))}upperCase(){return this.push((e=>({value:e.toUpperCase()})))}upperCamelCase(){return this.push((e=>({value:B(e)})))}}class re{static array(e){return(new G).items(e)}static boolean(){return new Q}static number(){return new X}static object(e){return(new ee).keys(e)}static string(){return new te}test(e,...t){return!u(re.validate(e,...t).error)}testAsync(e,...t){return C(this,void 0,void 0,(function*(){return!u((yield re.validateAsync(e,...t)).error)}))}static validate(e,...t){let r={value:e};for(let e=0;e<t.length;e++)if(r=t[e].validate(r.value),r.error)return{error:r.error};return r}static validateAsync(e,...t){return C(this,void 0,void 0,(function*(){let r={value:e};for(let e=0;e<t.length;e++)if(r=yield t[e].validateAsync(r.value),r.error)return{error:r.error};return r}))}}return e.EventEmitter=class{constructor(){this.events=new Map}on(e,t){const r=this.events.get(e);return r?r.push(t):this.events.set(e,[t]),{dispose:()=>this.off(e,t)}}once(e,t){const r=s=>{t(s),this.off(e,r)};return this.on(e,r),{dispose:()=>this.off(e,r)}}off(e,t){const r=this.events.get(e);if(t){if(r){const e=r.indexOf(t);e>=0&&r.splice(e,1)}}else this.events.set(e,[])}emit(e,t){const r=this.events.get(e);r&&r.forEach((e=>e(t)))}},e.Parse=class extends re{},e.Validation=re,e.ValidationBase=K,e.allowed=V,e.cache=()=>{const e=[],t=[];return r=>{let s=e.indexOf(r);return-1==s&&(e.push(r),t.push({}),s=t.length-1),t[s]}},e.camelCase=L,e.chunkArray=(e,t)=>{const r=[];for(let s=0;s<e.length;s+=t)r.push(e.slice(s,s+t));return r},e.clamp=c,e.cutText=(e,t)=>{if(e.length>t){const r=e.substring(0,t).split(" ");return r.pop(),r.join(" ")+"..."}return e},e.dateFormat=(e,t)=>t.replace(/%[yYmdHMS]/g,(t=>b[t]?b[t](e):t.slice(1))),e.debounce=(e,t)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...i){s(),r=setTimeout((()=>e(...i)),t)}}},e.decodeHTML=e=>e.replace(q,((e,t)=>N[t])).replace(/&#(\d+);/gi,((e,t)=>String.fromCharCode(parseInt(t,10)))),e.denied=(e,t)=>V(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,r)=>s(e)?O(e,t,r):l(e)?A(e,t,r):null,e.encodeHTML=e=>e.replace(/[^0-9A-Za-z ]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>C(void 0,void 0,void 0,(function*(){yield Promise.allSettled(e.map(((s,i)=>t.call(r,s,i,e)))).catch(I)})),e.forEachIndex=O,e.forEachKey=A,e.forEachSeries=(e,t,r)=>C(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e).catch(I)})),e.forLoop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.hash=e=>(e=E(M,e,!1),{generate:t=>E(t,e,!1),reverse:t=>E(t,e,!0)}),e.hexToRgb=e=>{var t;return(null===(t=e.replace(/^#/,"").match(/.{1,2}/g))||void 0===t?void 0:t.map((e=>p(e))))||[]},e.isArray=s,e.isBoolean=i,e.isCharacter=e=>/^[\w]$/.test(e),e.isDef=n,e.isDigit=U,e.isFloat=e=>a(e)&&!!(e%1),e.isFunction=e=>"function"==r(e),e.isInteger=e=>a(e)&&!(e%1),e.isLetter=D,e.isNull=e=>null===e,e.isNumber=a,e.isObject=l,e.isPlainObject=h,e.isString=u,e.isSymbol=H,e.isUndefined=o,e.lerp=(e,t,r)=>e+(t-e)*c(r,0,1),e.loop=(e,t)=>{let r=!1;const s=()=>C(void 0,void 0,void 0,(function*(){r&&(yield e(),setTimeout(s,t))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},e.lowerCamelCase=F,e.map=(e,t,r)=>{let s="";const i=Object.keys(e);for(let n=0;n<i.length;n++)s=i[n],e[s]=t.call(r,e[s],s,e);return e},e.mask=J,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),Z(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=Z,e.maskTest=(e,t)=>e.length==R(e,t).length,e.merge=W,e.msToString=e=>{const t=[];return j.forEach((r=>{const s=S[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},e.noop=I,e.parseMs=e=>{const t=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let r,s=0;for(;null!==(r=t.exec(e));)r.index===t.lastIndex&&t.lastIndex++,s+=parseFloat(r[1])*S[r[2]]||0;return s},e.parseSize=e=>{if(a(e))return e+"px";const t=(e=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e);return t?parseFloat(t[1])+(t[2]||"px"):"0px"},e.random=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),e.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),e.rgbToHex=(e,t,r)=>"#"+v(e.toString(16))+v(t.toString(16))+v(r.toString(16)),e.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>v(d(e,t)))),e.shuffleArray=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},e.simpleID=()=>g()+f(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=P,e.toArray=e=>s(e)?e:n(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=r,e.uniqueID=()=>_()+_()+_()+_(),e.upperCamelCase=B,e.uuidv4=()=>y()+"-"+f()+"-4"+g()+"-"+f()+"-"+(y()+f()),e}({});
|
|
7
|
+
var UtilsJS=function(t){"use strict";const e=(t,e,s)=>Math.max(e,Math.min(s,t)),s=(t,e)=>Math.floor(Math.random()*(e-t+1)+t),r=t=>{for(let e=t.length-1;e>0;e--){const r=s(0,e);[t[e],t[r]]=[t[r],t[e]]}return t},i={},n=t=>null==t?"null":i[i.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>i["[object "+t+"]"]=t.toLowerCase()));const a=t=>"array"==n(t),h=t=>"boolean"==n(t),l=t=>"null"!=n(t),u=t=>"number"==n(t),o=t=>"object"==n(t),c=t=>o(t)&&t.constructor===Object,p=t=>"string"==n(t),m=t=>void 0===t,v=t=>("0"+t).slice(-2),d=t=>parseInt(t,16)||0,g=(t,s)=>{return(r=d(t)+s,e(r,0,255)).toString(16);var r},f=t=>Math.random().toString(16).slice(t),y=()=>f(-3),_=()=>f(-4),x=()=>_()+_(),b=()=>x()+x(),w=t=>("0"+t.toString()).slice(-2),k={"%y":t=>w(t.getFullYear()),"%Y":t=>t.getFullYear().toString(),"%m":t=>w(t.getMonth()+1),"%d":t=>w(t.getDate()),"%H":t=>w(t.getHours()),"%M":t=>w(t.getMinutes()),"%S":t=>w(t.getSeconds())},S=36e5,C=24*S,j={ms:1,s:1e3,m:6e4,h:S,d:C,w:7*C,y:315576e5},O=["y","d","h","m","s","ms"];function A(t,e,s,r){return new(s||(s=Promise))((function(i,n){function a(t){try{l(r.next(t))}catch(t){n(t)}}function h(t){try{l(r.throw(t))}catch(t){n(t)}}function l(t){var e;t.done?i(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,h)}l((r=r.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const I=(t,e,s)=>{for(let r=0;r<t.length&&!1!==e.call(s,t[r],r,t);r++);return t},M=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length&&(r=i[n],!1!==e.call(s,t[r],r,t));n++);return t},L=()=>{},E="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",T=(t,e,s)=>{let r="";for(let i=0;i<t.length;i++){const n=E.indexOf(t[i]),a=E.indexOf(e[i%e.length]);r+=-1===n||-1===a?t[i]:E[(s?n-a+62:n+a)%62]}return r},U=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),N=t=>U(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),F=N,q=/&(amp|apos|lt|gt|nbsp|quot);/g,D={amp:"&",apos:"'",lt:"<",gt:">",nbsp:" ",quot:'"'},H=t=>/^[\d]$/.test(t),P=t=>/^[A-Za-z]$/.test(t),B=t=>/^[^\w]$/.test(t),R=t=>r(t.split("")).join("");class Y{constructor(){this.hasSpace=!1,this._space=" ",this.hasNumbers=!1,this._numbers="0123456789",this.hasSymbols=!1,this._symbols="!@#$%^&*()<>.<~:;?+=-_",this.hasUpper=!1,this._upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ",this.hasLower=!1,this._lower="abcdefghijklmnopqrstuvwxyz",this.hasChars=!1,this._chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}chars(){return this.hasChars=!0,this}static chars(){return(new Y).chars()}lower(){return this.hasLower=!0,this}static lower(){return(new Y).lower()}make(t){let e="";this.hasSpace&&(e+=this._space),this.hasNumbers&&(e+=this._numbers),this.hasSymbols&&(e+=this._symbols),this.hasUpper&&!this.hasChars&&(e+=this._upper),this.hasLower&&!this.hasChars&&(e+=this._lower),this.hasChars&&(e+=this._chars);let r="";if(t&&e.length){e=R(e);for(let i=0;i<t;i++)r+=e.charAt(s(0,e.length-1))}return r}numbers(){return this.hasNumbers=!0,this}static numbers(){return(new Y).numbers()}space(){return this.hasSpace=!0,this}static space(){return(new Y).space()}symbols(){return this.hasSymbols=!0,this}static symbols(){return(new Y).symbols()}upper(){return this.hasUpper=!0,this}static upper(){return(new Y).upper()}}const z=t=>t.split("").reverse().join(""),J=t=>U(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,s)=>e.toUpperCase()+s)))),Z=t=>(t.maskIndex--,""),$={"#":({value:t})=>t,0:t=>H(t.value)?t.value:Z(t),9:t=>H(t.value)?t.value:Z(t),A:t=>P(t.value)?t.value:Z(t),Z:t=>P(t.value)?t.value:Z(t),L:t=>P(t.value)?t.value.toLowerCase():Z(t),U:t=>P(t.value)?t.value.toUpperCase():Z(t),S:t=>B(t.value)?t.value:Z(t)},V=t=>{t.maskIndex++,t.textIndex++;const{mask:e,maskIndex:s,text:r,textIndex:i}=t;t.key=e[s],t.value=r[i];const{key:n,value:a}=t;return m(n)||m(a)?"":(t=>$[t.key]?$[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+V(t)},W=(t,e)=>V({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),K=W,G=(t,e)=>z(K(z(t),z(e))),Q=(t,e)=>t.reduce(((t,s)=>Object.assign(Object.assign({},t),{[s]:e[s]})),{}),X=(t,...e)=>{if(!e.length)return t;const s=e.shift();if(c(t)&&c(s))for(const e in s)c(s[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=X(t[e],s[e])):t=Object.assign(Object.assign({},t),{[e]:s[e]});return X(t,...e)};class tt{constructor(){this.name="",this._error="",this._filter=[],this._type="any",this.rules=[],this.custom=[]}empty(t){return this._value=t,this.unshift((t=>{const e=l(t);return{break:!e,value:e?t:this._value}}))}error(t){return this._error=t,this}filter(...t){return this._filter=t,this.unshift((t=>{const e=-1==this._filter.indexOf(t);return{break:!e,value:e?t:void 0}}))}invalid(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}}parse(){return this.unshift((t=>({value:t})))}push(t){return this.rules.push(t),this}required(){return this.unshift((t=>l(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!p(this.validate(t).error)}testAsync(t){return A(this,void 0,void 0,(function*(){return!p((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return A(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class et extends tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(a(t)){if(this._items){this._items.name="";for(let e=0;e<t.length;e++){const{error:s,value:r}=this._items.validate(t[e]);if(s)return this.invalid("["+this._type+"]< "+s+"["+e+"] >");t[e]=r}}return{value:t}}return this.invalid("["+this._type+"]")}))}between(t,e){return this.min(t),this.max(e)}items(t){return this._items=t,this}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}}class st extends tt{constructor(){super(),this._type="boolean",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:l(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class rt extends tt{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>u(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}max(t){return this._max=t,this.push((t=>t<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}min(t){return this._min=t,this.push((t=>t>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:l(t)?parseFloat(t):t})))}}class it extends tt{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(o(t)){const e=[],s={};return Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(t[r]);n&&e.push(n),l(a)&&(s[r]=a)})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:s}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{if(!o(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?Q(e,t):t}}))}}class nt extends tt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>p(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}date(){return this.push((t=>isNaN(new Date(t).getTime())?this.invalid("["+this._type+"].date"):{value:t}))}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:N(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:W(t,e)})))}min(t){return this._min=t,this.push((t=>t.length>=this._min?{value:t}:this.invalid("["+this._type+"].min("+this._min+")")))}parse(){return this.unshift((t=>({value:o(t)?JSON.stringify(t):l(t)?t.toString():t})))}required(){return this.push((t=>t.length?{value:t}:this.invalid("["+this._type+"].required")))}trim(){return this.push((t=>({value:t.trim()})))}upperCase(){return this.push((t=>({value:t.toUpperCase()})))}upperCamelCase(){return this.push((t=>({value:J(t)})))}}class at{static array(t){return(new et).items(t)}static boolean(){return new st}static number(){return new rt}static object(t){return(new it).keys(t)}static string(){return new nt}test(t,...e){return!p(at.validate(t,...e).error)}testAsync(t,...e){return A(this,void 0,void 0,(function*(){return!p((yield at.validateAsync(t,...e)).error)}))}static validate(t,...e){let s={value:t};for(let t=0;t<e.length;t++)if(s=e[t].validate(s.value),s.error)return{error:s.error};return s}static validateAsync(t,...e){return A(this,void 0,void 0,(function*(){let s={value:t};for(let t=0;t<e.length;t++)if(s=yield e[t].validateAsync(s.value),s.error)return{error:s.error};return s}))}}return t.EventEmitter=class{constructor(){this.events=new Map}on(t,e){const s=this.events.get(t);return s?s.push(e):this.events.set(t,[e]),{dispose:()=>this.off(t,e)}}once(t,e){const s=r=>{e(r),this.off(t,s)};return this.on(t,s),{dispose:()=>this.off(t,s)}}off(t,e){const s=this.events.get(t);if(e){if(s){const t=s.indexOf(e);t>=0&&s.splice(t,1)}}else this.events.set(t,[])}emit(t,e){const s=this.events.get(t);s&&s.forEach((t=>t(e)))}},t.Parse=class extends at{},t.RandomString=Y,t.Validation=at,t.ValidationBase=tt,t.allowed=Q,t.cache=()=>{const t=[],e=[];return s=>{let r=t.indexOf(s);return-1==r&&(t.push(s),e.push({}),r=e.length-1),e[r]}},t.camelCase=F,t.chunkArray=(t,e)=>{const s=[];for(let r=0;r<t.length;r+=e)s.push(t.slice(r,r+e));return s},t.clamp=e,t.cutText=(t,e)=>{if(t.length>e){const s=t.substring(0,e).split(" ");return s.pop(),s.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>k[e]?k[e](t):e.slice(1))),t.debounce=(t,e)=>{let s;const r=()=>{s&&clearTimeout(s)};return{cancel:r,run(...i){r(),s=setTimeout((()=>t(...i)),e)}}},t.decodeHTML=t=>t.replace(q,((t,e)=>D[e])).replace(/&#(\d+);/gi,((t,e)=>String.fromCharCode(parseInt(e,10)))),t.denied=(t,e)=>Q(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>a(t)?I(t,e,s):o(t)?M(t,e,s):null,t.encodeHTML=t=>t.replace(/[^0-9A-Za-z ]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,s)=>A(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((r,i)=>e.call(s,r,i,t)))).catch(L)})),t.forEachIndex=I,t.forEachKey=M,t.forEachSeries=(t,e,s)=>A(void 0,void 0,void 0,(function*(){for(let r=0;r<t.length;r++)yield e.call(s,t[r],r,t).catch(L)})),t.forLoop=(t,e,s)=>{for(let r=1;r<=t&&!1!==e.call(s,r,t);r++);return t},t.hash=t=>(t=T(E,t,!1),{generate:e=>T(e,t,!1),reverse:e=>T(e,t,!0)}),t.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>d(t))))||[]},t.isArray=a,t.isBoolean=h,t.isCharacter=t=>/^[\w]$/.test(t),t.isDef=l,t.isDigit=H,t.isFloat=t=>u(t)&&!!(t%1),t.isFunction=t=>"function"==n(t),t.isInteger=t=>u(t)&&!(t%1),t.isLetter=P,t.isNull=t=>null===t,t.isNumber=u,t.isObject=o,t.isPlainObject=c,t.isString=p,t.isSymbol=B,t.isUndefined=m,t.lerp=(t,s,r)=>t+(s-t)*e(r,0,1),t.loop=(t,e)=>{let s=!1;const r=()=>A(void 0,void 0,void 0,(function*(){s&&(yield t(),setTimeout(r,e))}));return{start(){s||(s=!0,r())},stop(){s=!1}}},t.lowerCamelCase=N,t.map=(t,e,s)=>{let r="";const i=Object.keys(t);for(let n=0;n<i.length;n++)r=i[n],t[r]=e.call(s,t[r],r,t);return t},t.mask=W,t.maskMoney=(t,e,s)=>(s++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),G(t,e.length<s?("0".repeat(s)+e).slice(-1*s):e)),t.maskReverse=G,t.maskTest=(t,e)=>t.length==K(t,e).length,t.merge=X,t.msToString=t=>{const e=[];return O.forEach((s=>{const r=j[s],i=(t-t%r)/r;i&&(t-=i*r,e.push(i.toString()+s))})),e.join(" ")},t.noop=L,t.parseMs=t=>{const e=/(-?(?:\d+)?\.?\d+) *(ms|s|m|h|d|w|y)/gm;let s,r=0;for(;null!==(s=e.exec(t));)s.index===e.lastIndex&&e.lastIndex++,r+=parseFloat(s[1])*j[s[2]]||0;return r},t.parseSize=t=>{if(u(t))return t+"px";const e=(t=>/(-?(?:\d+)?\.?\d+) *(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t);return e?parseFloat(e[1])+(e[2]||"px"):"0px"},t.random=s,t.randomCase=t=>t.split("").map((t=>Math.random()<.5?t.toUpperCase():t.toLowerCase())).join(""),t.rangeArray=(t,e,s=1)=>Array.from({length:(e-t)/s+1},((e,r)=>t+r*s)),t.rgbToHex=(t,e,s)=>"#"+v(t.toString(16))+v(e.toString(16))+v(s.toString(16)),t.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>v(g(t,e)))),t.shuffleArray=r,t.shuffleString=R,t.simpleID=()=>y()+_(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=z,t.toArray=t=>a(t)?t:l(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=n,t.uniqueID=()=>b()+b()+b()+b(),t.upperCamelCase=J,t.uuidv4=()=>x()+"-"+_()+"-4"+y()+"-"+_()+"-"+(x()+_()),t}({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/utils.js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.29",
|
|
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",
|
|
@@ -45,18 +45,18 @@
|
|
|
45
45
|
"homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@jest/globals": "^29.7.0",
|
|
48
|
-
"@rollup/plugin-commonjs": "^25.0.
|
|
48
|
+
"@rollup/plugin-commonjs": "^25.0.8",
|
|
49
49
|
"@rollup/plugin-terser": "^0.4.4",
|
|
50
50
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
51
51
|
"@types/jest": "^29.5.12",
|
|
52
|
-
"@types/node": "^20.
|
|
53
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
54
|
-
"@typescript-eslint/parser": "^7.
|
|
52
|
+
"@types/node": "^20.13.0",
|
|
53
|
+
"@typescript-eslint/eslint-plugin": "^7.11.0",
|
|
54
|
+
"@typescript-eslint/parser": "^7.11.0",
|
|
55
55
|
"eslint": "^8.57.0",
|
|
56
56
|
"jest": "^29.7.0",
|
|
57
|
-
"rollup": "^4.
|
|
58
|
-
"rollup-plugin-dts": "^6.1.
|
|
59
|
-
"ts-jest": "^29.1.
|
|
57
|
+
"rollup": "^4.18.0",
|
|
58
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
59
|
+
"ts-jest": "^29.1.4",
|
|
60
60
|
"ts-node-dev": "^2.0.0",
|
|
61
61
|
"tslib": "^2.6.2"
|
|
62
62
|
}
|