@andrewcaires/utils.js 0.2.24 → 0.2.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.24
2
+ * @andrewcaires/utils.js v0.2.26
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=e=>"array"==u(e),r=e=>"boolean"==u(e),s=e=>"null"!=u(e),i=e=>"number"==u(e),n=e=>"object"==u(e),a=e=>"string"==u(e),o=e=>(null==e?void 0:e.constructor)===Object,l=e=>void 0===e,u=t=>null==t?"null":e[e.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>e["[object "+t+"]"]=t.toLowerCase()));const h=(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,h(r,0,255)).toString(16);var r},d=e=>Math.random().toString(16).slice(e),x=()=>d(-3),m=()=>d(-4),f=()=>m()+m(),g=()=>f()+f(),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())};function b(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 k=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},w=(e,t,r)=>{let s="";const 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},S=()=>{},j="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",C=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=j.indexOf(e[i]),a=j.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:j[(r?n-a+62:n+a)%62]}return s},O=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),A=e=>/^[\d]$/.test(e),I=e=>/^[A-Za-z]$/.test(e),M=e=>/^[^\w]$/.test(e),E=e=>O(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),T=E,F=e=>O(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),L=e=>e.split("").reverse().join(""),N=e=>(e.maskIndex--,""),D={"#":({value:e})=>e,0:e=>A(e.value)?e.value:N(e),9:e=>A(e.value)?e.value:N(e),A:e=>I(e.value)?e.value:N(e),Z:e=>I(e.value)?e.value:N(e),L:e=>I(e.value)?e.value.toLowerCase():N(e),U:e=>I(e.value)?e.value.toUpperCase():N(e),S:e=>M(e.value)?e.value:N(e)},H=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 l(n)||l(a)?"":(e=>D[e.key]?D[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+H(e)},U=(e,t)=>H({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),q=U,B=(e,t)=>L(q(L(e),L(t))),P=36e5,Y=24*P,$={ms:1,s:1e3,m:6e4,h:P,d:Y,w:7*Y,y:315576e5},R=["y","d","h","m","s","ms"],z=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{}),J=(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]=J(e[t],r[t])):e=Object.assign(Object.assign({},e),{[t]:r[t]});return J(e,...t)};class V{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=s(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=>s(e)?{value:e}:this.invalid("["+this._type+"].required")))}test(e){return!a(this.validate(e).error)}testAsync(e){return b(this,void 0,void 0,(function*(){return!a((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 b(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 W extends V{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((e=>{if(t(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 Z extends V{constructor(){super(),this._type="boolean",this.push((e=>r(e)?{value:e}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((e=>({value:s(e)?-1!=[1,"1",!0,"true"].indexOf(e):e})))}}class K extends V{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((e=>i(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:s(e)?parseFloat(e):e})))}}class G extends V{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(n(e)){const t=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:o}=n.validate(e[i]);a&&t.push(a),s(o)&&(r[i]=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(!n(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?z(t,e):e}}))}}class Q extends V{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>a(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:E(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:U(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:n(e)?JSON.stringify(e):s(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:F(e)})))}}class X{static array(e){return(new W).items(e)}static boolean(){return new Z}static number(){return new K}static object(e){return(new G).keys(e)}static string(){return new Q}test(e,...t){return!a(X.validate(e,...t).error)}testAsync(e,...t){return b(this,void 0,void 0,(function*(){return!a((yield X.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 b(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.Validation=X,exports.ValidationBase=V,exports.allowed=z,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=T,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=h,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(/&#(\d+);/g,((e,t)=>String.fromCharCode(t))),exports.denied=(e,t)=>z(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,r,s)=>t(e)?k(e,r,s):n(e)?w(e,r,s):null,exports.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>b(void 0,void 0,void 0,(function*(){yield Promise.allSettled(e.map(((s,i)=>t.call(r,s,i,e)))).catch(S)})),exports.forEachIndex=k,exports.forEachKey=w,exports.forEachSeries=(e,t,r)=>b(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e).catch(S)})),exports.forLoop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.hash=e=>(e=C(j,e,!1),{generate:t=>C(t,e,!1),reverse:t=>C(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=t,exports.isBoolean=r,exports.isCharacter=e=>/^[\w]$/.test(e),exports.isDef=s,exports.isDigit=A,exports.isFloat=e=>i(e)&&!!(e%1),exports.isFunction=e=>"function"==u(e),exports.isInteger=e=>i(e)&&!(e%1),exports.isLetter=I,exports.isNull=e=>null===e,exports.isNumber=i,exports.isObject=n,exports.isPlainObject=o,exports.isString=a,exports.isSymbol=M,exports.isUndefined=l,exports.lerp=(e,t,r)=>e+(t-e)*h(r,0,1),exports.loop=(e,t)=>{let r=!1;const s=()=>b(void 0,void 0,void 0,(function*(){r&&(yield e(),setTimeout(s,t))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},exports.lowerCamelCase=E,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=U,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),B(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=B,exports.maskTest=(e,t)=>e.length==q(e,t).length,exports.merge=J,exports.msToString=e=>{const t=[];return R.forEach((r=>{const s=$[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},exports.noop=S,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])*$[r[2]]||0;return s},exports.parseSize=e=>{if(i(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.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=L,exports.toArray=e=>t(e)?e:s(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=u,exports.uniqueID=()=>g()+g()+g()+g(),exports.upperCamelCase=F,exports.uuidv4=()=>f()+"-"+m()+"-4"+x()+"-"+m()+"-"+(f()+m());
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(""),B=e=>E(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),P=e=>(e.maskIndex--,""),Y={"#":({value:e})=>e,0:e=>N(e.value)?e.value:P(e),9:e=>N(e.value)?e.value:P(e),A:e=>D(e.value)?e.value:P(e),Z:e=>D(e.value)?e.value:P(e),L:e=>D(e.value)?e.value.toLowerCase():P(e),U:e=>D(e.value)?e.value.toUpperCase():P(e),S:e=>H(e.value)?e.value:P(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:B(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.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=B,exports.uuidv4=()=>g()+"-"+m()+"-4"+x()+"-"+m()+"-"+(g()+m());
package/dist/index.d.ts CHANGED
@@ -1,16 +1,27 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.24
2
+ * @andrewcaires/utils.js v0.2.26
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[];
10
+
9
11
  declare const shuffleArray: (array: Array<any>) => any[];
12
+
10
13
  declare const toArray: (value: any) => Array<any>;
11
14
 
12
15
  declare const toBool: (value: string) => boolean;
13
16
 
17
+ declare const isArray: (test: any) => test is any[];
18
+
19
+ declare const isBoolean: (test: any) => test is boolean;
20
+
21
+ declare const isDef: (test: any) => test is any;
22
+
23
+ declare const isFloat: (test: any) => test is number;
24
+
14
25
  type TypeCallbackFunction = (...args: Array<any>) => any;
15
26
  type TypeArray<T> = Array<T>;
16
27
  type TypeArrayAny = TypeArray<any>;
@@ -29,6 +40,24 @@ type TypeCallbackArray<T> = TypeCallback<T, number, TypeArray<T>, any>;
29
40
  type TypeCallbackMap<T> = TypeCallback<T, number, TypeArray<T>, T>;
30
41
  type TypeCallbackObject<T> = TypeCallback<T, string, TypeObject<T>, any>;
31
42
 
43
+ declare const isFunction: (test: any) => test is TypeCallbackFunction;
44
+
45
+ declare const isInteger: (test: any) => test is number;
46
+
47
+ declare const isNull: (test: any) => test is null;
48
+
49
+ declare const isNumber: (test: any) => test is number;
50
+
51
+ declare const isObject: (test: any) => test is TypeObjectAny;
52
+
53
+ declare const isPlainObject: (test: any) => test is TypeObjectAny;
54
+
55
+ declare const isString: (test: any) => test is string;
56
+
57
+ declare const isUndefined: (test: any) => test is undefined;
58
+
59
+ declare const type: (test: any) => string;
60
+
32
61
  type CacheCallback = (key: any) => TypeObjectAny;
33
62
  declare const cache: () => CacheCallback;
34
63
 
@@ -42,6 +71,10 @@ declare const uuidv4: () => string;
42
71
 
43
72
  declare const dateFormat: (date: Date, format: string) => string;
44
73
 
74
+ declare const msToString: (ms: number) => string;
75
+
76
+ declare const parseMs: (str: string) => number;
77
+
45
78
  interface IDebounceCallbacks {
46
79
  cancel: TypeCallbackFunction;
47
80
  run: TypeCallbackFunction;
@@ -87,12 +120,12 @@ declare const maskMoney: (mask: string, text: string, decimal: number) => string
87
120
  declare const maskTest: (mask: string, text: string) => boolean;
88
121
  declare const maskReverse: (mask: string, text: string) => string;
89
122
 
90
- declare const parseMs: (str: string) => number;
91
- declare const msToString: (ms: number) => string;
92
-
93
123
  declare const clamp: (value: number, min: number, max: number) => number;
124
+
94
125
  declare const lerp: (value1: number, value2: number, amount: number) => number;
95
126
 
127
+ declare const random: (min: number, max: number) => number;
128
+
96
129
  declare const allowed: (allowed: Array<string>, target: TypeObjectAny) => TypeObjectAny;
97
130
  declare const denied: (denied: Array<string>, target: TypeObjectAny) => TypeObjectAny;
98
131
  declare const merge: (target: TypeObjectAny, ...sources: Array<TypeObjectAny>) => TypeObjectAny;
@@ -104,31 +137,27 @@ declare const sleep: (ms: number) => Promise<void>;
104
137
 
105
138
  declare const parseSize: (size: number | string) => string;
106
139
 
140
+ declare const camelCase: (str: string) => string;
141
+
107
142
  declare const cutText: (str: string, length: number) => string;
108
- declare const decodeHTML: (str: string) => string;
143
+
144
+ declare const decodeHTML: (value: string) => string;
145
+
109
146
  declare const encodeHTML: (str: string) => string;
147
+
110
148
  declare const isCharacter: (str: string) => boolean;
149
+
111
150
  declare const isDigit: (str: string) => boolean;
151
+
112
152
  declare const isLetter: (str: string) => boolean;
153
+
113
154
  declare const isSymbol: (str: string) => boolean;
155
+
114
156
  declare const lowerCamelCase: (str: string) => string;
115
- declare const camelCase: (str: string) => string;
116
- declare const upperCamelCase: (str: string) => string;
157
+
117
158
  declare const stringReverse: (str: string) => string;
118
159
 
119
- declare const isArray: (test: any) => test is any[];
120
- declare const isBoolean: (test: any) => test is boolean;
121
- declare const isDef: (test: any) => test is any;
122
- declare const isFunction: (test: any) => test is TypeCallbackFunction;
123
- declare const isNumber: (test: any) => test is number;
124
- declare const isObject: (test: any) => test is TypeObjectAny;
125
- declare const isString: (test: any) => test is string;
126
- declare const isFloat: (test: any) => test is number;
127
- declare const isInteger: (test: any) => test is number;
128
- declare const isNull: (test: any) => test is null;
129
- declare const isPlainObject: (test: any) => test is TypeObjectAny;
130
- declare const isUndefined: (test: any) => test is undefined;
131
- declare const type: (test: any) => string;
160
+ declare const upperCamelCase: (str: string) => string;
132
161
 
133
162
  interface IValidationResult {
134
163
  break?: boolean;
@@ -137,6 +166,7 @@ interface IValidationResult {
137
166
  }
138
167
  type ValidationCallback = (value: any) => IValidationResult;
139
168
  type ValidationCallbackAsync = (value: any) => Promise<IValidationResult>;
169
+
140
170
  declare class ValidationBase<T = any> {
141
171
  name: string;
142
172
  private _value;
@@ -158,6 +188,7 @@ declare class ValidationBase<T = any> {
158
188
  validate(value?: any): IValidationResult;
159
189
  validateAsync(value?: any): Promise<IValidationResult>;
160
190
  }
191
+
161
192
  declare class ValidationArray extends ValidationBase<Array<any>> {
162
193
  private _length;
163
194
  private _max;
@@ -171,11 +202,13 @@ declare class ValidationArray extends ValidationBase<Array<any>> {
171
202
  max(value: number): this;
172
203
  min(value: number): this;
173
204
  }
205
+
174
206
  declare class ValidationBoolean extends ValidationBase<boolean> {
175
207
  protected _type: string;
176
208
  constructor();
177
209
  parse(): this;
178
210
  }
211
+
179
212
  declare class ValidationNumber extends ValidationBase<number> {
180
213
  private _max;
181
214
  private _min;
@@ -186,6 +219,7 @@ declare class ValidationNumber extends ValidationBase<number> {
186
219
  min(value: number): this;
187
220
  parse(): this;
188
221
  }
222
+
189
223
  declare class ValidationObject extends ValidationBase<TypeObjectAny> {
190
224
  private _keys;
191
225
  protected _type: string;
@@ -193,6 +227,7 @@ declare class ValidationObject extends ValidationBase<TypeObjectAny> {
193
227
  keys(value?: TypeObject<ValidationBase>): this;
194
228
  parse(): this;
195
229
  }
230
+
196
231
  declare class ValidationString extends ValidationBase<string> {
197
232
  private _length;
198
233
  private _max;
@@ -214,6 +249,7 @@ declare class ValidationString extends ValidationBase<string> {
214
249
  upperCase(): this;
215
250
  upperCamelCase(): this;
216
251
  }
252
+
217
253
  declare class Validation {
218
254
  static array(value?: ValidationBase): ValidationArray;
219
255
  static boolean(): ValidationBoolean;
@@ -226,4 +262,4 @@ declare class Validation {
226
262
  static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<IValidationResult>;
227
263
  }
228
264
 
229
- export { type CacheCallback, type EachCallback, type EventCallback, EventEmitter, type EventList, type ForEachCallback, type IDebounceCallbacks, type IEventDisposable, type IHash, type ILoopCallbacks, type IValidationResult, type LoopCallback, 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, rangeArray, rgbToHex, shadeColor, shuffleArray, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
265
+ export { type CacheCallback, type EachCallback, type EventCallback, EventEmitter, type EventList, type ForEachCallback, type IDebounceCallbacks, type IEventDisposable, type IHash, type ILoopCallbacks, type IValidationResult, type LoopCallback, 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 };
package/dist/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.24
2
+ * @andrewcaires/utils.js v0.2.26
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={},e=t=>"array"==p(t),r=t=>"boolean"==p(t),s=t=>"null"!=p(t),i=t=>"function"==p(t),n=t=>"number"==p(t),a=t=>"object"==p(t),u=t=>"string"==p(t),h=t=>n(t)&&!!(t%1),l=t=>n(t)&&!(t%1),o=t=>null===t,c=t=>(null==t?void 0:t.constructor)===Object,v=t=>void 0===t,p=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const d=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},m=(t,e,r=1)=>Array.from({length:(e-t)/r+1},((e,s)=>t+s*r)),f=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},g=t=>e(t)?t:s(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=>("0"+t).slice(-2),k=t=>parseInt(t,16)||0,j=(t,e)=>{return(r=k(t)+e,x(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=>k(t))))||[]},S=(t,e,r)=>"#"+w(t.toString(16))+w(e.toString(16))+w(r.toString(16)),C=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>w(j(t,e)))),A=t=>Math.random().toString(16).slice(t),I=()=>A(-3),M=()=>A(-4),E=()=>M()+M(),F=()=>E()+E(),L=()=>I()+M(),N=()=>F()+F()+F()+F(),T=()=>E()+"-"+M()+"-4"+I()+"-"+M()+"-"+(E()+M()),U=t=>("0"+t.toString()).slice(-2),q={"%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())},Y=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>q[e]?q[e](t):e.slice(1)));function $(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{h(s.next(t))}catch(t){n(t)}}function u(t){try{h(s.throw(t))}catch(t){n(t)}}function h(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,u)}h((s=s.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const H=(t,e)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...i){s(),r=setTimeout((()=>t(...i)),e)}}},P=(t,e)=>{let r=!1;const s=()=>$(void 0,void 0,void 0,(function*(){r&&(yield t(),setTimeout(s,e))}));return{start(){r||(r=!0,s())},stop(){r=!1}}};class D{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)))}}const J=(t,r,s)=>e(t)?W(t,r,s):a(t)?Z(t,r,s):null,W=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},Z=(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},z=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},B=(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},G=()=>{},K="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",Q=(t,e,r)=>{let s="";for(let i=0;i<t.length;i++){const n=K.indexOf(t[i]),a=K.indexOf(e[i%e.length]);s+=-1===n||-1===a?t[i]:K[(r?n-a+62:n+a)%62]}return s},R=t=>(t=Q(K,t,!1),{generate:e=>Q(e,t,!1),reverse:e=>Q(e,t,!0)}),V=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},X=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),tt=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),et=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),rt=t=>/^[\w]$/.test(t),st=t=>/^[\d]$/.test(t),it=t=>/^[A-Za-z]$/.test(t),nt=t=>/^[^\w]$/.test(t),at=t=>et(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),ut=at,ht=t=>et(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),lt=t=>t.split("").reverse().join(""),ot=t=>(t.maskIndex--,""),ct={"#":({value:t})=>t,0:t=>st(t.value)?t.value:ot(t),9:t=>st(t.value)?t.value:ot(t),A:t=>it(t.value)?t.value:ot(t),Z:t=>it(t.value)?t.value:ot(t),L:t=>it(t.value)?t.value.toLowerCase():ot(t),U:t=>it(t.value)?t.value.toUpperCase():ot(t),S:t=>nt(t.value)?t.value:ot(t)},vt=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 v(n)||v(a)?"":(t=>ct[t.key]?ct[t.key](t):(t.key!=t.value&&t.textIndex--,t.key))(t)+vt(t)},pt=(t,e)=>vt({mask:t,maskIndex:-1,text:e,textIndex:-1,key:"",value:""}),dt=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),gt(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),mt=pt,ft=(t,e)=>t.length==mt(t,e).length,gt=(t,e)=>lt(mt(lt(t),lt(e))),yt=36e5,_t=24*yt,xt={ms:1,s:1e3,m:6e4,h:yt,d:_t,w:7*_t,y:315576e5},bt=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])*xt[r[2]]||0;return s},wt=["y","d","h","m","s","ms"],kt=t=>{const e=[];return wt.forEach((r=>{const s=xt[r],i=(t-t%s)/s;i&&(t-=i*s,e.push(i.toString()+r))})),e.join(" ")},jt=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),Ot=(t,e)=>jt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),St=(t,...e)=>{if(!e.length)return t;const r=e.shift();if(c(t)&&c(r))for(const e in r)c(r[e])?(t[e]||(t=Object.assign(Object.assign({},t),{[e]:{}})),t[e]=St(t[e],r[e])):t=Object.assign(Object.assign({},t),{[e]:r[e]});return St(t,...e)},Ct=(t,e,r)=>$(void 0,void 0,void 0,(function*(){yield Promise.allSettled(t.map(((s,i)=>e.call(r,s,i,t)))).catch(G)})),At=(t,e,r)=>$(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t).catch(G)})),It=t=>new Promise((e=>setTimeout(e,t))),Mt=t=>{if(n(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 Et{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=s(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=>s(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!u(this.validate(t).error)}testAsync(t){return $(this,void 0,void 0,(function*(){return!u((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 $(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 Ft extends Et{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(e(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 Lt extends Et{constructor(){super(),this._type="boolean",this.push((t=>r(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:s(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class Nt extends Et{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>n(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:s(t)?parseFloat(t):t})))}}class Tt extends Et{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(a(t)){const e=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:u}=n.validate(t[i]);a&&e.push(a),s(u)&&(r[i]=u)})),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(!a(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?jt(e,t):t}}))}}class Ut extends Et{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>u(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:pt(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:a(t)?JSON.stringify(t):s(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:ht(t)})))}}class qt{static array(t){return(new Ft).items(t)}static boolean(){return new Lt}static number(){return new Nt}static object(t){return(new Tt).keys(t)}static string(){return new Ut}test(t,...e){return!u(qt.validate(t,...e).error)}testAsync(t,...e){return $(this,void 0,void 0,(function*(){return!u((yield qt.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 $(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}))}}export{D as EventEmitter,qt as Validation,Et as ValidationBase,jt as allowed,_ as cache,ut as camelCase,d as chunkArray,x as clamp,V as cutText,Y as dateFormat,H as debounce,X as decodeHTML,Ot as denied,J as each,tt as encodeHTML,Ct as forEachAsync,W as forEachIndex,Z as forEachKey,At as forEachSeries,z as forLoop,R as hash,O as hexToRgb,e as isArray,r as isBoolean,rt as isCharacter,s as isDef,st as isDigit,h as isFloat,i as isFunction,l as isInteger,it as isLetter,o as isNull,n as isNumber,a as isObject,c as isPlainObject,u as isString,nt as isSymbol,v as isUndefined,b as lerp,P as loop,at as lowerCamelCase,B as map,pt as mask,dt as maskMoney,gt as maskReverse,ft as maskTest,St as merge,kt as msToString,G as noop,bt as parseMs,Mt as parseSize,m as rangeArray,S as rgbToHex,C as shadeColor,f as shuffleArray,L as simpleID,It as sleep,lt as stringReverse,g as toArray,y as toBool,p as type,N as uniqueID,ht as upperCamelCase,T as uuidv4};
7
+ const t=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},e=(t,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};function W(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 B=(t,e)=>{let r;const s=()=>{r&&clearTimeout(r)};return{cancel:s,run(...i){s(),r=setTimeout((()=>t(...i)),e)}}},G=(t,e)=>{let r=!1;const s=()=>W(void 0,void 0,void 0,(function*(){r&&(yield t(),setTimeout(s,e))}));return{start(){r||(r=!0,s())},stop(){r=!1}}};class K{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)))}}const 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)=>W(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)=>W(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 W(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 W(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 W(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 W(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}))}}export{K as EventEmitter,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,B 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,G 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};
package/dist/index.min.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /*!
2
- * @andrewcaires/utils.js v0.2.24
2
+ * @andrewcaires/utils.js v0.2.26
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=>"array"==o(e),s=e=>"boolean"==o(e),i=e=>"null"!=o(e),n=e=>"number"==o(e),a=e=>"object"==o(e),l=e=>"string"==o(e),u=e=>(null==e?void 0:e.constructor)===Object,h=e=>void 0===e,o=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const 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),f=()=>m(-3),g=()=>m(-4),y=()=>g()+g(),_=()=>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())};function k(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{u(s.next(e))}catch(e){n(e)}}function l(e){try{u(s.throw(e))}catch(e){n(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,l)}u((s=s.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const w=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},S=(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},j=()=>{},C="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",O=(e,t,r)=>{let s="";for(let i=0;i<e.length;i++){const n=C.indexOf(e[i]),a=C.indexOf(t[i%t.length]);s+=-1===n||-1===a?e[i]:C[(r?n-a+62:n+a)%62]}return s},A=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),I=e=>/^[\d]$/.test(e),M=e=>/^[A-Za-z]$/.test(e),E=e=>/^[^\w]$/.test(e),T=e=>A(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),F=T,L=e=>A(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),N=e=>e.split("").reverse().join(""),U=e=>(e.maskIndex--,""),D={"#":({value:e})=>e,0:e=>I(e.value)?e.value:U(e),9:e=>I(e.value)?e.value:U(e),A:e=>M(e.value)?e.value:U(e),Z:e=>M(e.value)?e.value:U(e),L:e=>M(e.value)?e.value.toLowerCase():U(e),U:e=>M(e.value)?e.value.toUpperCase():U(e),S:e=>E(e.value)?e.value:U(e)},H=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=>D[e.key]?D[e.key](e):(e.key!=e.value&&e.textIndex--,e.key))(e)+H(e)},q=(e,t)=>H({mask:e,maskIndex:-1,text:t,textIndex:-1,key:"",value:""}),B=q,P=(e,t)=>N(B(N(e),N(t))),Y=36e5,$=24*Y,J={ms:1,s:1e3,m:6e4,h:Y,d:$,w:7*$,y:315576e5},R=["y","d","h","m","s","ms"],z=(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(u(e)&&u(r))for(const t in r)u(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 k(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 k(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 Z 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 K 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 G 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 Q 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:l}=n.validate(e[s]);a&&t.push(a),i(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(!a(e))try{e=JSON.parse(e)}catch(e){return this.invalid("["+this._type+"]")}const t=Object.keys(this._keys);return{value:t.length?z(t,e):e}}))}}class X 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:q(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:L(e)})))}}class ee{static array(e){return(new Z).items(e)}static boolean(){return new K}static number(){return new G}static object(e){return(new Q).keys(e)}static string(){return new X}test(e,...t){return!l(ee.validate(e,...t).error)}testAsync(e,...t){return k(this,void 0,void 0,(function*(){return!l((yield ee.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 k(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.Validation=ee,e.ValidationBase=W,e.allowed=z,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=F,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(/&#(\d+);/g,((e,t)=>String.fromCharCode(t))),e.denied=(e,t)=>z(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,s)=>r(e)?w(e,t,s):a(e)?S(e,t,s):null,e.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>k(void 0,void 0,void 0,(function*(){yield Promise.allSettled(e.map(((s,i)=>t.call(r,s,i,e)))).catch(j)})),e.forEachIndex=w,e.forEachKey=S,e.forEachSeries=(e,t,r)=>k(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e).catch(j)})),e.forLoop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.hash=e=>(e=O(C,e,!1),{generate:t=>O(t,e,!1),reverse:t=>O(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=r,e.isBoolean=s,e.isCharacter=e=>/^[\w]$/.test(e),e.isDef=i,e.isDigit=I,e.isFloat=e=>n(e)&&!!(e%1),e.isFunction=e=>"function"==o(e),e.isInteger=e=>n(e)&&!(e%1),e.isLetter=M,e.isNull=e=>null===e,e.isNumber=n,e.isObject=a,e.isPlainObject=u,e.isString=l,e.isSymbol=E,e.isUndefined=h,e.lerp=(e,t,r)=>e+(t-e)*c(r,0,1),e.loop=(e,t)=>{let r=!1;const s=()=>k(void 0,void 0,void 0,(function*(){r&&(yield e(),setTimeout(s,t))}));return{start(){r||(r=!0,s())},stop(){r=!1}}},e.lowerCamelCase=T,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=q,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),P(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=P,e.maskTest=(e,t)=>e.length==B(e,t).length,e.merge=V,e.msToString=e=>{const t=[];return R.forEach((r=>{const s=J[r],i=(e-e%s)/s;i&&(e-=i*s,t.push(i.toString()+r))})),t.join(" ")},e.noop=j,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])*J[r[2]]||0;return s},e.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"},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=()=>f()+g(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=N,e.toArray=e=>r(e)?e:i(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=o,e.uniqueID=()=>_()+_()+_()+_(),e.upperCamelCase=L,e.uuidv4=()=>y()+"-"+g()+"-4"+f()+"-"+g()+"-"+(y()+g()),e}({});
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),B=e=>e.split("").reverse().join(""),P=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)=>B(R(B(e),B(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:P(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.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=B,e.toArray=e=>s(e)?e:n(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=r,e.uniqueID=()=>_()+_()+_()+_(),e.upperCamelCase=P,e.uuidv4=()=>y()+"-"+f()+"-4"+g()+"-"+f()+"-"+(y()+f()),e}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andrewcaires/utils.js",
3
- "version": "0.2.24",
3
+ "version": "0.2.26",
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",