@andrewcaires/utils.js 0.2.13 → 0.2.15
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 +9 -4
- package/dist/index.esm.js +2 -2
- package/dist/index.min.js +2 -2
- package/package.json +7 -7
package/dist/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.15
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2023 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
"use strict";const e={},t=e=>"array"==o(e),r=e=>"boolean"==o(e),s=e=>"null"!=o(e),i=e=>"number"==o(e),n=e=>"object"==o(e),a=e=>"string"==o(e),h=e=>void 0===e,o=t=>null==t?"null":e[e.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>e["[object "+t+"]"]=t.toLowerCase()));const u=(e,t,r)=>Math.max(t,Math.min(r,e)),l=(e,t)=>{return(r=parseInt(e,16)+t,u(r,0,255)).toString(16);var r},p=e=>Math.random().toString(16).slice(e),c=()=>p(-3),v=()=>p(-4),m=()=>v()+v(),d=()=>m()+m();const x=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},g=(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},f=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),y=e=>f(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),_=y,b=e=>f(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),k=e=>e.split("").reverse().join(""),w=e=>/^[A-Za-z]$/.test(e),C=e=>(e.mask--,""),j=(e,t,r)=>{r.mask++,r.text++;const s=e[r.mask],i=t[r.text];return h(s)||h(i)?"":((e,t,r)=>{switch(e.toUpperCase()){case"#":return t;case"0":case"9":return(e=>/^[0-9]$/.test(e))(t)?t:C(r);case"A":case"Z":return w(t)?t:C(r);case"L":return w(t)?t.toLowerCase():C(r);case"U":return w(t)?t.toUpperCase():C(r);case"S":return(e=>/^[^A-Za-z0-9]$/.test(e))(t)?t:C(r);default:return e!=t&&r.text--,e}})(s,i,r)+j(e,t,r)},A=(e,t)=>j(e,t,{mask:-1,text:-1}),S=A,O=(e,t)=>k(S(k(e),k(t))),M=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function E(e,t,r,s){return new(r||(r=Promise))((function(i,n){function a(e){try{o(s.next(e))}catch(e){n(e)}}function h(e){try{o(s.throw(e))}catch(e){n(e)}}function o(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,h)}o((s=s.apply(e,t||[])).next())}))}class F{constructor(){this.name="",this._error="",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}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 E(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 E(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};return{value:t.value}}))}}class I extends F{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 L extends F{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 T extends F{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 U extends F{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(n(e)){if(this._keys){const t=[];if(Object.keys(this._keys).forEach((r=>{const i=this._keys[r];i.name=r;const{error:n,value:a}=i.validate(e[r]);n&&t.push(n),s(a)&&(e[r]=a)})),t.length)return this.invalid("["+this._type+"]{ "+t.join(", ")+" }")}return{value:e}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{n(e)||(e=JSON.parse(e));const t=Object.keys(this._keys);return{value:t.length?M(t,e):e}}))}}class q extends F{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)}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:y(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:A(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:b(e)})))}}class H{static array(e){return(new I).items(e)}static boolean(){return new L}static number(){return new T}static object(e){return(new U).keys(e)}static string(){return new q}test(e,...t){return!a(H.validate(e,...t).error)}testAsync(e,...t){return E(this,void 0,void 0,(function*(){return!a((yield H.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 E(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=H,exports.ValidationBase=F,exports.allowedObject=M,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=_,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=>{let r;switch(t){case"%y":r=e.getFullYear();break;case"%Y":return e.getFullYear().toString();case"%m":r=e.getMonth()+1;break;case"%d":r=e.getDate();break;case"%H":r=e.getHours();break;case"%M":r=e.getMinutes();break;case"%S":r=e.getSeconds();break;default:return t.slice(1)}return("0"+r.toString()).slice(-2)})),exports.debounce=(e,t)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>e(...s)),t)}},exports.decodeHTML=e=>e.replace(/&#(\d+);/g,((e,t)=>String.fromCharCode(t))),exports.deniedObject=(e,t)=>M(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),exports.each=(e,r,s)=>t(e)?x(e,r,s):n(e)?g(e,r,s):null,exports.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),exports.forEachAsync=(e,t,r)=>E(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),exports.forEachIndex=x,exports.forEachKey=g,exports.forEachSeries=(e,t,r)=>E(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),exports.isArray=t,exports.isBoolean=r,exports.isDef=s,exports.isFloat=e=>i(e)&&!!(e%1),exports.isFunction=e=>"function"==o(e),exports.isInteger=e=>i(e)&&!(e%1),exports.isNull=e=>null===e,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isUndefined=h,exports.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},exports.lowerCamelCase=y,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=A,exports.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),O(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),exports.maskReverse=O,exports.maskTest=(e,t)=>e.length==S(e,t).length,exports.noop=()=>{},exports.parseSize=e=>{if(i(e))return e+"px";const[,t,r,s]=(e=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e)||["","0","",""];return(r.length?parseFloat(t+"."+r):parseInt(t))+(s||"px")},exports.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),exports.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>("0"+l(e,t)).slice(-2))),exports.simpleID=()=>c()+v(),exports.sleep=e=>new Promise((t=>setTimeout(t,e))),exports.stringReverse=k,exports.toArray=e=>t(e)?e:s(e)?[e]:[],exports.toBool=e=>"true"===e||"1"===e,exports.type=o,exports.uniqueID=()=>d()+d()+d()+d(),exports.upperCamelCase=b,exports.uuidv4=()=>m()+"-"+v()+"-4"+c()+"-"+v()+"-"+(m()+v());
|
|
7
|
+
"use strict";const t={},e=t=>"array"==o(t),r=t=>"boolean"==o(t),s=t=>"null"!=o(t),i=t=>"number"==o(t),n=t=>"object"==o(t),a=t=>"string"==o(t),h=t=>void 0===t,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 u=(t,e,r)=>Math.max(e,Math.min(r,t)),l=t=>("0"+t).slice(-2),p=t=>parseInt(t,16)||0,c=(t,e)=>{return(r=p(t)+e,u(r,0,255)).toString(16);var r},v=t=>Math.random().toString(16).slice(t),m=()=>v(-3),d=()=>v(-4),x=()=>d()+d(),g=()=>x()+x();const f=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},_=(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},y=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),b=t=>y(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),k=b,w=t=>y(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),C=t=>t.split("").reverse().join(""),S=t=>/^[A-Za-z]$/.test(t),j=t=>(t.mask--,""),A=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return h(s)||h(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:j(r);case"A":case"Z":return S(e)?e:j(r);case"L":return S(e)?e.toLowerCase():j(r);case"U":return S(e)?e.toUpperCase():j(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:j(r);default:return t!=e&&r.text--,t}})(s,i,r)+A(t,e,r)},O=(t,e)=>A(t,e,{mask:-1,text:-1}),M=O,E=(t,e)=>C(M(C(t),C(e))),T=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{});function F(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{o(s.next(t))}catch(t){n(t)}}function h(t){try{o(s.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}o((s=s.apply(t,e||[])).next())}))}class I{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!a(this.validate(t).error)}testAsync(t){return F(this,void 0,void 0,(function*(){return!a((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return F(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class L extends I{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 U extends I{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 H extends I{constructor(){super(),this._max=0,this._min=0,this._type="number",this.push((t=>i(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 q extends I{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(n(t)){const e=[],r={};return Object.keys(this._keys).forEach((i=>{const n=this._keys[i];n.name=i;const{error:a,value:h}=n.validate(t[i]);a&&e.push(a),s(h)&&(r[i]=h)})),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(!n(t))try{t=JSON.parse(t)}catch(t){return this.invalid("["+this._type+"]")}const e=Object.keys(this._keys);return{value:e.length?T(e,t):t}}))}}class N extends I{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>a(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:b(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:O(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:n(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:w(t)})))}}class B{static array(t){return(new L).items(t)}static boolean(){return new U}static number(){return new H}static object(t){return(new q).keys(t)}static string(){return new N}test(t,...e){return!a(B.validate(t,...e).error)}testAsync(t,...e){return F(this,void 0,void 0,(function*(){return!a((yield B.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 F(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}))}}exports.EventEmitter=class{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)))}},exports.Validation=B,exports.ValidationBase=I,exports.allowedObject=T,exports.cache=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},exports.camelCase=k,exports.chunkArray=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},exports.clamp=u,exports.cutText=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},exports.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),exports.debounce=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}},exports.decodeHTML=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),exports.deniedObject=(t,e)=>T(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,r,s)=>e(t)?f(t,r,s):n(t)?_(t,r,s):null,exports.encodeHTML=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),exports.forEachAsync=(t,e,r)=>F(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),exports.forEachIndex=f,exports.forEachKey=_,exports.forEachSeries=(t,e,r)=>F(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),exports.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>p(t))))||[]},exports.isArray=e,exports.isBoolean=r,exports.isDef=s,exports.isFloat=t=>i(t)&&!!(t%1),exports.isFunction=t=>"function"==o(t),exports.isInteger=t=>i(t)&&!(t%1),exports.isNull=t=>null===t,exports.isNumber=i,exports.isObject=n,exports.isString=a,exports.isUndefined=h,exports.lerp=(t,e,r)=>t+(e-t)*u(r,0,1),exports.loop=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},exports.lowerCamelCase=b,exports.map=(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},exports.mask=O,exports.maskMoney=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),E(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),exports.maskReverse=E,exports.maskTest=(t,e)=>t.length==M(t,e).length,exports.noop=()=>{},exports.parseSize=t=>{if(i(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")},exports.rangeArray=(t,e,r=1)=>Array.from({length:(e-t)/r+1},((e,s)=>t+s*r)),exports.rgbToHex=(t,e,r)=>"#"+l(t.toString(16))+l(e.toString(16))+l(r.toString(16)),exports.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>l(c(t,e)))),exports.simpleID=()=>m()+d(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=C,exports.toArray=t=>e(t)?t:s(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=o,exports.uniqueID=()=>g()+g()+g()+g(),exports.upperCamelCase=w,exports.uuidv4=()=>x()+"-"+d()+"-4"+m()+"-"+d()+"-"+(x()+d());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.15
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2023 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
@@ -31,6 +31,8 @@ type TypeCallbackObject<T> = TypeCallback<T, string, TypeObject<T>, any>;
|
|
|
31
31
|
type CacheCallback = (key: any) => TypeObjectAny;
|
|
32
32
|
declare const cache: () => CacheCallback;
|
|
33
33
|
|
|
34
|
+
declare const hexToRgb: (color: string) => Array<number>;
|
|
35
|
+
declare const rgbToHex: (r: number, g: number, b: number) => string;
|
|
34
36
|
declare const shadeColor: (color: string, percent: number) => string;
|
|
35
37
|
|
|
36
38
|
declare const simpleID: () => string;
|
|
@@ -70,6 +72,7 @@ declare const maskTest: (mask: string, text: string) => boolean;
|
|
|
70
72
|
declare const maskReverse: (mask: string, text: string) => string;
|
|
71
73
|
|
|
72
74
|
declare const clamp: (value: number, min: number, max: number) => number;
|
|
75
|
+
declare const lerp: (value1: number, value2: number, amount: number) => number;
|
|
73
76
|
|
|
74
77
|
declare const allowedObject: (allowed: string[], raw: TypeObjectAny) => TypeObjectAny;
|
|
75
78
|
declare const deniedObject: (denied: string[], raw: TypeObjectAny) => TypeObjectAny;
|
|
@@ -110,13 +113,15 @@ interface ValidationResult {
|
|
|
110
113
|
type ValidationCallback = (value: any) => ValidationResult;
|
|
111
114
|
declare class ValidationBase<T = any> {
|
|
112
115
|
name: string;
|
|
113
|
-
private
|
|
116
|
+
private _value;
|
|
114
117
|
private _error;
|
|
118
|
+
private _filter;
|
|
115
119
|
protected _type: string;
|
|
116
120
|
private rules;
|
|
117
121
|
private custom;
|
|
118
122
|
empty(value?: T): this;
|
|
119
123
|
error(value: string): this;
|
|
124
|
+
filter(...values: Array<any>): this;
|
|
120
125
|
protected invalid(name: string): ValidationResult;
|
|
121
126
|
parse(): this;
|
|
122
127
|
push(callback: ValidationCallback): this;
|
|
@@ -155,7 +160,7 @@ declare class ValidationNumber extends ValidationBase<number> {
|
|
|
155
160
|
min(value: number): this;
|
|
156
161
|
parse(): this;
|
|
157
162
|
}
|
|
158
|
-
declare class ValidationObject extends ValidationBase<
|
|
163
|
+
declare class ValidationObject extends ValidationBase<TypeObjectAny> {
|
|
159
164
|
private _keys;
|
|
160
165
|
protected _type: string;
|
|
161
166
|
constructor();
|
|
@@ -194,4 +199,4 @@ declare class Validation {
|
|
|
194
199
|
static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<ValidationResult>;
|
|
195
200
|
}
|
|
196
201
|
|
|
197
|
-
export { EventCallback, EventDisposable, EventEmitter, EventList, TypeArray, TypeArrayAny, TypeArrayFunction, TypeArrayNumber, TypeArrayString, TypeCallback, TypeCallbackArray, TypeCallbackFunction, TypeCallbackMap, TypeCallbackObject, TypeObject, TypeObjectAny, TypeObjectFunction, TypeObjectNumber, TypeObjectString, Validation, ValidationBase, allowedObject, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, deniedObject, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, isArray, isBoolean, isDef, isFloat, isFunction, isInteger, isNull, isNumber, isObject, isString, isUndefined, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, noop, parseSize, rangeArray, shadeColor, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
|
202
|
+
export { EventCallback, EventDisposable, EventEmitter, EventList, TypeArray, TypeArrayAny, TypeArrayFunction, TypeArrayNumber, TypeArrayString, TypeCallback, TypeCallbackArray, TypeCallbackFunction, TypeCallbackMap, TypeCallbackObject, TypeObject, TypeObjectAny, TypeObjectFunction, TypeObjectNumber, TypeObjectString, Validation, ValidationBase, allowedObject, cache, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, decodeHTML, deniedObject, each, encodeHTML, forEachAsync, forEachIndex, forEachKey, forEachSeries, hexToRgb, isArray, isBoolean, isDef, isFloat, isFunction, isInteger, isNull, isNumber, isObject, isString, isUndefined, lerp, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, noop, parseSize, rangeArray, rgbToHex, shadeColor, 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.15
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2023 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),h=t=>"string"==p(t),u=t=>n(t)&&!!(t%1),l=t=>n(t)&&!(t%1),o=t=>null===t,c=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 v=(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)),d=t=>e(t)?t:s(t)?[t]:[],g=t=>"true"===t||"1"===t,f=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},_=(t,e,r)=>Math.max(e,Math.min(r,t)),y=(t,e)=>{return(r=parseInt(t,16)+e,_(r,0,255)).toString(16);var r},x=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+y(t,e)).slice(-2))),b=t=>Math.random().toString(16).slice(t),w=()=>b(-3),k=()=>b(-4),C=()=>k()+k(),j=()=>C()+C(),S=()=>w()+k(),A=()=>j()+j()+j()+j(),O=()=>C()+"-"+k()+"-4"+w()+"-"+k()+"-"+(C()+k()),M=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),U=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class F{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 L=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},q=(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},Y=(t,r,s)=>e(t)?L(t,r,s):a(t)?q(t,r,s):null,$=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},E=(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},H=()=>{},I=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},N=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),P=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),T=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),Z=t=>T(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),z=Z,J=t=>T(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),W=t=>t.split("").reverse().join(""),B=t=>/^[A-Za-z]$/.test(t),D=t=>(t.mask--,""),G=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return c(s)||c(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:D(r);case"A":case"Z":return B(e)?e:D(r);case"L":return B(e)?e.toLowerCase():D(r);case"U":return B(e)?e.toUpperCase():D(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:D(r);default:return t!=e&&r.text--,t}})(s,i,r)+G(t,e,r)},K=(t,e)=>G(t,e,{mask:-1,text:-1}),Q=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),X(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),R=K,V=(t,e)=>t.length==R(t,e).length,X=(t,e)=>W(R(W(t),W(e))),tt=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),et=(t,e)=>tt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function rt(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}const st=(t,e,r)=>rt(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),it=(t,e,r)=>rt(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),nt=t=>new Promise((e=>setTimeout(e,t))),at=t=>{if(n(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")};class ht{constructor(){this.name="",this._error="",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}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!h(this.validate(t).error)}testAsync(t){return rt(this,void 0,void 0,(function*(){return!h((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return rt(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++)if(e=yield this.custom[t](e.value),e.error)return{error:e.error};return{value:e.value}}))}}class ut extends ht{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 ht{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 ot extends ht{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 ct extends ht{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(a(t)){if(this._keys){const e=[];if(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),s(a)&&(t[r]=a)})),e.length)return this.invalid("["+this._type+"]{ "+e.join(", ")+" }")}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t||{},this}parse(){return this.unshift((t=>{a(t)||(t=JSON.parse(t));const e=Object.keys(this._keys);return{value:e.length?tt(e,t):t}}))}}class pt extends ht{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:Z(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:K(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:J(t)})))}}class vt{static array(t){return(new ut).items(t)}static boolean(){return new lt}static number(){return new ot}static object(t){return(new ct).keys(t)}static string(){return new pt}test(t,...e){return!h(vt.validate(t,...e).error)}testAsync(t,...e){return rt(this,void 0,void 0,(function*(){return!h((yield vt.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 rt(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{F as EventEmitter,vt as Validation,ht as ValidationBase,tt as allowedObject,f as cache,z as camelCase,v as chunkArray,_ as clamp,I as cutText,M as dateFormat,U as debounce,N as decodeHTML,et as deniedObject,Y as each,P as encodeHTML,st as forEachAsync,L as forEachIndex,q as forEachKey,it as forEachSeries,e as isArray,r as isBoolean,s as isDef,u as isFloat,i as isFunction,l as isInteger,o as isNull,n as isNumber,a as isObject,h as isString,c as isUndefined,$ as loop,Z as lowerCamelCase,E as map,K as mask,Q as maskMoney,X as maskReverse,V as maskTest,H as noop,at as parseSize,m as rangeArray,x as shadeColor,S as simpleID,nt as sleep,W as stringReverse,d as toArray,g as toBool,p as type,A as uniqueID,J as upperCamelCase,O as uuidv4};
|
|
7
|
+
const t={},e=t=>"array"==v(t),r=t=>"boolean"==v(t),s=t=>"null"!=v(t),i=t=>"function"==v(t),n=t=>"number"==v(t),a=t=>"object"==v(t),h=t=>"string"==v(t),u=t=>n(t)&&!!(t%1),l=t=>n(t)&&!(t%1),o=t=>null===t,c=t=>void 0===t,v=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const p=(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)),d=t=>e(t)?t:s(t)?[t]:[],g=t=>"true"===t||"1"===t,f=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},_=(t,e,r)=>Math.max(e,Math.min(r,t)),y=(t,e,r)=>t+(e-t)*_(r,0,1),x=t=>("0"+t).slice(-2),b=t=>parseInt(t,16)||0,w=(t,e)=>{return(r=b(t)+e,_(r,0,255)).toString(16);var r},k=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>b(t))))||[]},C=(t,e,r)=>"#"+x(t.toString(16))+x(e.toString(16))+x(r.toString(16)),S=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>x(w(t,e)))),j=t=>Math.random().toString(16).slice(t),O=()=>j(-3),A=()=>j(-4),M=()=>A()+A(),U=()=>M()+M(),F=()=>O()+A(),L=()=>U()+U()+U()+U(),q=()=>M()+"-"+A()+"-4"+O()+"-"+A()+"-"+(M()+A()),Y=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),$=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class E{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 H=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},I=(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},N=(t,r,s)=>e(t)?H(t,r,s):a(t)?I(t,r,s):null,P=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},T=(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},Z=()=>{},z=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},J=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),W=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),B=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),D=t=>B(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),G=D,K=t=>B(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),Q=t=>t.split("").reverse().join(""),R=t=>/^[A-Za-z]$/.test(t),V=t=>(t.mask--,""),X=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return c(s)||c(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:V(r);case"A":case"Z":return R(e)?e:V(r);case"L":return R(e)?e.toLowerCase():V(r);case"U":return R(e)?e.toUpperCase():V(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:V(r);default:return t!=e&&r.text--,t}})(s,i,r)+X(t,e,r)},tt=(t,e)=>X(t,e,{mask:-1,text:-1}),et=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),it(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),rt=tt,st=(t,e)=>t.length==rt(t,e).length,it=(t,e)=>Q(rt(Q(t),Q(e))),nt=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),at=(t,e)=>nt(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function ht(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}const ut=(t,e,r)=>ht(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),lt=(t,e,r)=>ht(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),ot=t=>new Promise((e=>setTimeout(e,t))),ct=t=>{if(n(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")};class vt{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!h(this.validate(t).error)}testAsync(t){return ht(this,void 0,void 0,(function*(){return!h((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 ht(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class pt extends vt{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 mt extends vt{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 dt extends vt{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 gt extends vt{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:h}=n.validate(t[i]);a&&e.push(a),s(h)&&(r[i]=h)})),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?nt(e,t):t}}))}}class ft extends vt{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:D(t)})))}max(t){return this._max=t,this.push((t=>t.length<=this._max?{value:t}:this.invalid("["+this._type+"].max("+this._max+")")))}mask(t){return this.push((e=>({value:tt(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:K(t)})))}}class _t{static array(t){return(new pt).items(t)}static boolean(){return new mt}static number(){return new dt}static object(t){return(new gt).keys(t)}static string(){return new ft}test(t,...e){return!h(_t.validate(t,...e).error)}testAsync(t,...e){return ht(this,void 0,void 0,(function*(){return!h((yield _t.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 ht(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{E as EventEmitter,_t as Validation,vt as ValidationBase,nt as allowedObject,f as cache,G as camelCase,p as chunkArray,_ as clamp,z as cutText,Y as dateFormat,$ as debounce,J as decodeHTML,at as deniedObject,N as each,W as encodeHTML,ut as forEachAsync,H as forEachIndex,I as forEachKey,lt as forEachSeries,k as hexToRgb,e as isArray,r as isBoolean,s as isDef,u as isFloat,i as isFunction,l as isInteger,o as isNull,n as isNumber,a as isObject,h as isString,c as isUndefined,y as lerp,P as loop,D as lowerCamelCase,T as map,tt as mask,et as maskMoney,it as maskReverse,st as maskTest,Z as noop,ct as parseSize,m as rangeArray,C as rgbToHex,S as shadeColor,F as simpleID,ot as sleep,Q as stringReverse,d as toArray,g as toBool,v as type,L as uniqueID,K as upperCamelCase,q 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.15
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2023 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
var UtilsJS=function(e){"use strict";const t={},r=e=>"array"==l(e),s=e=>"boolean"==l(e),i=e=>"null"!=l(e),n=e=>"number"==l(e),a=e=>"object"==l(e),h=e=>"string"==l(e),u=e=>void 0===e,l=e=>null==e?"null":t[t.toString.call(e)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((e=>t["[object "+e+"]"]=e.toLowerCase()));const o=(e,t,r)=>Math.max(t,Math.min(r,e)),c=(e,t)=>{return(r=parseInt(e,16)+t,o(r,0,255)).toString(16);var r},p=e=>Math.random().toString(16).slice(e),v=()=>p(-3),m=()=>p(-4),d=()=>m()+m(),f=()=>d()+d();const g=(e,t,r)=>{for(let s=0;s<e.length&&!1!==t.call(r,e[s],s,e);s++);return e},y=(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},_=(e,t)=>t(e.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),x=e=>_(e,(e=>e.replace(/\s(\w)/g,(e=>e.toUpperCase())))),b=x,k=e=>_(e,(e=>e.replace(/(\w)(\w*)/g,((e,t,r)=>t.toUpperCase()+r)))),w=e=>e.split("").reverse().join(""),C=e=>/^[A-Za-z]$/.test(e),j=e=>(e.mask--,""),A=(e,t,r)=>{r.mask++,r.text++;const s=e[r.mask],i=t[r.text];return u(s)||u(i)?"":((e,t,r)=>{switch(e.toUpperCase()){case"#":return t;case"0":case"9":return(e=>/^[0-9]$/.test(e))(t)?t:j(r);case"A":case"Z":return C(t)?t:j(r);case"L":return C(t)?t.toLowerCase():j(r);case"U":return C(t)?t.toUpperCase():j(r);case"S":return(e=>/^[^A-Za-z0-9]$/.test(e))(t)?t:j(r);default:return e!=t&&r.text--,e}})(s,i,r)+A(e,t,r)},S=(e,t)=>A(e,t,{mask:-1,text:-1}),O=S,M=(e,t)=>w(O(w(e),w(t))),E=(e,t)=>e.reduce(((e,r)=>Object.assign(Object.assign({},e),{[r]:t[r]})),{});function F(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 h(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,h)}u((s=s.apply(e,t||[])).next())}))}class U{constructor(){this.name="",this._error="",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}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!h(this.validate(e).error)}testAsync(e){return F(this,void 0,void 0,(function*(){return!h((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 F(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};return{value:t.value}}))}}class I extends U{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 L extends U{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 T extends U{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 U{constructor(){super(),this._keys={},this._type="object",this.push((e=>{if(a(e)){if(this._keys){const t=[];if(Object.keys(this._keys).forEach((r=>{const s=this._keys[r];s.name=r;const{error:n,value:a}=s.validate(e[r]);n&&t.push(n),i(a)&&(e[r]=a)})),t.length)return this.invalid("["+this._type+"]{ "+t.join(", ")+" }")}return{value:e}}return this.invalid("["+this._type+"]")}))}keys(e){return this._keys=e||{},this}parse(){return this.unshift((e=>{a(e)||(e=JSON.parse(e));const t=Object.keys(this._keys);return{value:t.length?E(t,e):e}}))}}class H extends U{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((e=>h(e)?{value:e}:this.invalid("["+this._type+"]")))}between(e,t){return this.min(e),this.max(t)}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:x(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:S(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:k(e)})))}}class N{static array(e){return(new I).items(e)}static boolean(){return new L}static number(){return new T}static object(e){return(new q).keys(e)}static string(){return new H}test(e,...t){return!h(N.validate(e,...t).error)}testAsync(e,...t){return F(this,void 0,void 0,(function*(){return!h((yield N.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 F(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=N,e.ValidationBase=U,e.allowedObject=E,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=b,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=o,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=>{let r;switch(t){case"%y":r=e.getFullYear();break;case"%Y":return e.getFullYear().toString();case"%m":r=e.getMonth()+1;break;case"%d":r=e.getDate();break;case"%H":r=e.getHours();break;case"%M":r=e.getMinutes();break;case"%S":r=e.getSeconds();break;default:return t.slice(1)}return("0"+r.toString()).slice(-2)})),e.debounce=(e,t)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>e(...s)),t)}},e.decodeHTML=e=>e.replace(/&#(\d+);/g,((e,t)=>String.fromCharCode(t))),e.deniedObject=(e,t)=>E(Object.keys(t).filter((t=>-1==e.indexOf(t))),t),e.each=(e,t,s)=>r(e)?g(e,t,s):a(e)?y(e,t,s):null,e.encodeHTML=e=>e.replace(/[\u00A0-\u9999<>\\&]/g,(e=>"&#"+e.charCodeAt(0)+";")),e.forEachAsync=(e,t,r)=>F(void 0,void 0,void 0,(function*(){yield Promise.all(e.map(((s,i)=>t.call(r,s,i,e))))})),e.forEachIndex=g,e.forEachKey=y,e.forEachSeries=(e,t,r)=>F(void 0,void 0,void 0,(function*(){for(let s=0;s<e.length;s++)yield t.call(r,e[s],s,e)})),e.isArray=r,e.isBoolean=s,e.isDef=i,e.isFloat=e=>n(e)&&!!(e%1),e.isFunction=e=>"function"==l(e),e.isInteger=e=>n(e)&&!(e%1),e.isNull=e=>null===e,e.isNumber=n,e.isObject=a,e.isString=h,e.isUndefined=u,e.loop=(e,t,r)=>{for(let s=1;s<=e&&!1!==t.call(r,s,e);s++);return e},e.lowerCamelCase=x,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=S,e.maskMoney=(e,t,r)=>(r++,t=parseInt((t||"0").replace(/\W/g,"")).toString(),M(e,t.length<r?("0".repeat(r)+t).slice(-1*r):t)),e.maskReverse=M,e.maskTest=(e,t)=>e.length==O(e,t).length,e.noop=()=>{},e.parseSize=e=>{if(n(e))return e+"px";const[,t,r,s]=(e=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(e))(e)||["","0","",""];return(r.length?parseFloat(t+"."+r):parseInt(t))+(s||"px")},e.rangeArray=(e,t,r=1)=>Array.from({length:(t-e)/r+1},((t,s)=>e+s*r)),e.shadeColor=(e,t)=>"#"+e.replace(/^#/,"").replace(/../g,(e=>("0"+c(e,t)).slice(-2))),e.simpleID=()=>v()+m(),e.sleep=e=>new Promise((t=>setTimeout(t,e))),e.stringReverse=w,e.toArray=e=>r(e)?e:i(e)?[e]:[],e.toBool=e=>"true"===e||"1"===e,e.type=l,e.uniqueID=()=>f()+f()+f()+f(),e.upperCamelCase=k,e.uuidv4=()=>d()+"-"+m()+"-4"+v()+"-"+m()+"-"+(d()+m()),e}({});
|
|
7
|
+
var UtilsJS=function(t){"use strict";const e={},r=t=>"array"==l(t),s=t=>"boolean"==l(t),i=t=>"null"!=l(t),n=t=>"number"==l(t),a=t=>"object"==l(t),h=t=>"string"==l(t),u=t=>void 0===t,l=t=>null==t?"null":e[e.toString.call(t)]||"object";["Array","Boolean","Function","Number","Object","String"].forEach((t=>e["[object "+t+"]"]=t.toLowerCase()));const o=(t,e,r)=>Math.max(e,Math.min(r,t)),c=t=>("0"+t).slice(-2),p=t=>parseInt(t,16)||0,v=(t,e)=>{return(r=p(t)+e,o(r,0,255)).toString(16);var r},m=t=>Math.random().toString(16).slice(t),d=()=>m(-3),f=()=>m(-4),g=()=>f()+f(),_=()=>g()+g();const y=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},x=(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},b=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),k=t=>b(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),w=k,C=t=>b(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),S=t=>t.split("").reverse().join(""),j=t=>/^[A-Za-z]$/.test(t),A=t=>(t.mask--,""),O=(t,e,r)=>{r.mask++,r.text++;const s=t[r.mask],i=e[r.text];return u(s)||u(i)?"":((t,e,r)=>{switch(t.toUpperCase()){case"#":return e;case"0":case"9":return(t=>/^[0-9]$/.test(t))(e)?e:A(r);case"A":case"Z":return j(e)?e:A(r);case"L":return j(e)?e.toLowerCase():A(r);case"U":return j(e)?e.toUpperCase():A(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:A(r);default:return t!=e&&r.text--,t}})(s,i,r)+O(t,e,r)},M=(t,e)=>O(t,e,{mask:-1,text:-1}),E=M,T=(t,e)=>S(E(S(t),S(e))),F=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{});function U(t,e,r,s){return new(r||(r=Promise))((function(i,n){function a(t){try{u(s.next(t))}catch(t){n(t)}}function h(t){try{u(s.throw(t))}catch(t){n(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(a,h)}u((s=s.apply(t,e||[])).next())}))}class I{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=i(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=>i(t)?{value:t}:this.invalid("["+this._type+"].required")))}test(t){return!h(this.validate(t).error)}testAsync(t){return U(this,void 0,void 0,(function*(){return!h((yield this.validateAsync(t)).error)}))}unshift(t){return this.rules.unshift(t),this}validate(t){if(this.custom.length)return this.invalid("[async]");let e={value:t};for(let t=0;t<this.rules.length;t++){if(e=this.rules[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}validateAsync(t){return U(this,void 0,void 0,(function*(){let e=this.validate(t);if(e.error)return{error:e.error};for(let t=0;t<this.custom.length;t++){if(e=yield this.custom[t](e.value),e.error)return{error:e.error};if(e.break)return{value:e.value}}return{value:e.value}}))}}class L extends I{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="array",this.push((t=>{if(r(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 H extends I{constructor(){super(),this._type="boolean",this.push((t=>s(t)?{value:t}:this.invalid("["+this._type+"]")))}parse(){return this.unshift((t=>({value:i(t)?-1!=[1,"1",!0,"true"].indexOf(t):t})))}}class q extends I{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:i(t)?parseFloat(t):t})))}}class N extends I{constructor(){super(),this._keys={},this._type="object",this.push((t=>{if(a(t)){const e=[],r={};return Object.keys(this._keys).forEach((s=>{const n=this._keys[s];n.name=s;const{error:a,value:h}=n.validate(t[s]);a&&e.push(a),i(h)&&(r[s]=h)})),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?F(e,t):t}}))}}class B extends I{constructor(){super(),this._length=0,this._max=0,this._min=0,this._type="string",this.push((t=>h(t)?{value:t}:this.invalid("["+this._type+"]")))}between(t,e){return this.min(t),this.max(e)}email(){return this.push((t=>/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(t)?{value:t}:this.invalid("["+this._type+"].email")))}length(t){return this._length=t,this.push((t=>t.length==this._length?{value:t}:this.invalid("["+this._type+"].length("+this._length+")")))}lowerCase(){return this.push((t=>({value:t.toLowerCase()})))}lowerCamelCase(){return this.push((t=>({value:k(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:M(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):i(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:C(t)})))}}class D{static array(t){return(new L).items(t)}static boolean(){return new H}static number(){return new q}static object(t){return(new N).keys(t)}static string(){return new B}test(t,...e){return!h(D.validate(t,...e).error)}testAsync(t,...e){return U(this,void 0,void 0,(function*(){return!h((yield D.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 U(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}))}}return t.EventEmitter=class{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)))}},t.Validation=D,t.ValidationBase=I,t.allowedObject=F,t.cache=()=>{const t=[],e=[];return r=>{let s=t.indexOf(r);return-1==s&&(t.push(r),e.push({}),s=e.length-1),e[s]}},t.camelCase=w,t.chunkArray=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},t.clamp=o,t.cutText=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},t.dateFormat=(t,e)=>e.replace(/%[yYmdHMS]/g,(e=>{let r;switch(e){case"%y":r=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":r=t.getMonth()+1;break;case"%d":r=t.getDate();break;case"%H":r=t.getHours();break;case"%M":r=t.getMinutes();break;case"%S":r=t.getSeconds();break;default:return e.slice(1)}return("0"+r.toString()).slice(-2)})),t.debounce=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}},t.decodeHTML=t=>t.replace(/&#(\d+);/g,((t,e)=>String.fromCharCode(e))),t.deniedObject=(t,e)=>F(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>r(t)?y(t,e,s):a(t)?x(t,e,s):null,t.encodeHTML=t=>t.replace(/[\u00A0-\u9999<>\\&]/g,(t=>"&#"+t.charCodeAt(0)+";")),t.forEachAsync=(t,e,r)=>U(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),t.forEachIndex=y,t.forEachKey=x,t.forEachSeries=(t,e,r)=>U(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),t.hexToRgb=t=>{var e;return(null===(e=t.replace(/^#/,"").match(/.{1,2}/g))||void 0===e?void 0:e.map((t=>p(t))))||[]},t.isArray=r,t.isBoolean=s,t.isDef=i,t.isFloat=t=>n(t)&&!!(t%1),t.isFunction=t=>"function"==l(t),t.isInteger=t=>n(t)&&!(t%1),t.isNull=t=>null===t,t.isNumber=n,t.isObject=a,t.isString=h,t.isUndefined=u,t.lerp=(t,e,r)=>t+(e-t)*o(r,0,1),t.loop=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},t.lowerCamelCase=k,t.map=(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},t.mask=M,t.maskMoney=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),T(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),t.maskReverse=T,t.maskTest=(t,e)=>t.length==E(t,e).length,t.noop=()=>{},t.parseSize=t=>{if(n(t))return t+"px";const[,e,r,s]=(t=>/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t))(t)||["","0","",""];return(r.length?parseFloat(e+"."+r):parseInt(e))+(s||"px")},t.rangeArray=(t,e,r=1)=>Array.from({length:(e-t)/r+1},((e,s)=>t+s*r)),t.rgbToHex=(t,e,r)=>"#"+c(t.toString(16))+c(e.toString(16))+c(r.toString(16)),t.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>c(v(t,e)))),t.simpleID=()=>d()+f(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=S,t.toArray=t=>r(t)?t:i(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=l,t.uniqueID=()=>_()+_()+_()+_(),t.upperCamelCase=C,t.uuidv4=()=>g()+"-"+f()+"-4"+d()+"-"+f()+"-"+(g()+f()),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.15",
|
|
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",
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@rollup/plugin-commonjs": "^24.0.1",
|
|
47
|
-
"@rollup/plugin-terser": "^0.4.
|
|
48
|
-
"@rollup/plugin-typescript": "^11.
|
|
49
|
-
"@types/node": "^18.15.
|
|
50
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
51
|
-
"@typescript-eslint/parser": "^5.
|
|
52
|
-
"eslint": "^8.
|
|
47
|
+
"@rollup/plugin-terser": "^0.4.1",
|
|
48
|
+
"@rollup/plugin-typescript": "^11.1.0",
|
|
49
|
+
"@types/node": "^18.15.11",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^5.57.1",
|
|
51
|
+
"@typescript-eslint/parser": "^5.57.1",
|
|
52
|
+
"eslint": "^8.38.0",
|
|
53
53
|
"rollup": "^3.20.2",
|
|
54
54
|
"rollup-plugin-dts": "^5.3.0",
|
|
55
55
|
"ts-node-dev": "^2.0.0",
|