@andrewcaires/utils.js 0.2.4 → 0.2.6
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 +7 -0
- package/dist/index.d.ts +24 -23
- package/dist/index.esm.js +2 -2
- package/dist/index.min.js +2 -2
- package/package.json +28 -16
- package/dist/index.umd.js +0 -7
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @andrewcaires/utils.js v0.2.6
|
|
3
|
+
* JavaScript utility library for web and nodejs development
|
|
4
|
+
* (c) 2022 Andrew Caires
|
|
5
|
+
* @license: MIT
|
|
6
|
+
*/
|
|
7
|
+
"use strict";const t={},e=t=>"array"==u(t),r=t=>"boolean"==u(t),s=t=>"null"!=u(t),i=t=>"number"==u(t),n=t=>"object"==u(t),a=t=>"string"==u(t),h=t=>void 0===t,u=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=(t,e,r)=>Math.max(e,Math.min(r,t)),l=(t,e)=>{return(r=parseInt(t,16)+e,o(r,0,255)).toString(16);var r},p=t=>Math.random().toString(16).slice(t),c=()=>p(-3),v=()=>p(-4),m=()=>v()+v(),d=()=>m()+m();const x=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},f=(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},g=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),_=t=>g(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),y=_,b=t=>g(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),w=t=>t.split("").reverse().join(""),k=t=>/^[A-Za-z]$/.test(t),C=t=>(t.mask--,""),j=(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:C(r);case"A":case"Z":return k(e)?e:C(r);case"L":return k(e)?e.toLowerCase():C(r);case"U":return k(e)?e.toUpperCase():C(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:C(r);default:return t!=e&&r.text--,t}})(s,i,r)+j(t,e,r)},S=(t,e)=>j(t,e,{mask:-1,text:-1}),A=S,O=(t,e)=>w(A(w(t),w(e))),E=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{});function M(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 F{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!a(this.validate(t).error)}testAsync(t){return M(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 M(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 I extends F{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 F{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","true"].indexOf(t):t})))}}class q extends F{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 L extends F{constructor(){super(),this._type="object",this.push((t=>{if(n(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){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?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class T extends F{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:_(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:S(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:b(t)})))}}class B{static array(t){return(new I).items(t)}static boolean(){return new U}static number(){return new q}static object(t){return(new L).keys(t)}static string(){return new T}test(t,...e){return!a(B.validate(t,...e).error)}testAsync(t,...e){return M(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 M(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=F,exports.allowedObject=E,exports.camelCase=y,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=o,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.deniedObject=(t,e)=>E(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),exports.each=(t,r,s)=>e(t)?x(t,r,s):n(t)?f(t,r,s):null,exports.forEachAsync=(t,e,r)=>M(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),exports.forEachIndex=x,exports.forEachKey=f,exports.forEachSeries=(t,e,r)=>M(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),exports.isArray=e,exports.isBoolean=r,exports.isDef=s,exports.isFloat=t=>i(t)&&!!(t%1),exports.isFunction=t=>"function"==u(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.loop=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},exports.lowerCamelCase=_,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=S,exports.maskMoney=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),O(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),exports.maskReverse=O,exports.maskTest=(t,e)=>t.length==A(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.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+l(t,e)).slice(-2))),exports.simpleID=()=>c()+v(),exports.sleep=t=>new Promise((e=>setTimeout(e,t))),exports.stringReverse=w,exports.toArray=t=>e(t)?t:s(t)?[t]:[],exports.toBool=t=>"true"===t||"1"===t,exports.type=u,exports.uniqueID=()=>d()+d()+d()+d(),exports.upperCamelCase=b,exports.uuidv4=()=>m()+"-"+v()+"-4"+c()+"-"+v()+"-"+(m()+v());
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.
|
|
2
|
+
* @andrewcaires/utils.js v0.2.6
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2022 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
@@ -17,31 +17,31 @@ declare const uuidv4: () => string;
|
|
|
17
17
|
|
|
18
18
|
declare const dateFormat: (date: Date, format: string) => string;
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
type TypeCallbackFunction = (...args: Array<any>) => any;
|
|
21
|
+
type TypeArray<T> = Array<T>;
|
|
22
|
+
type TypeArrayAny = TypeArray<any>;
|
|
23
|
+
type TypeArrayString = TypeArray<string>;
|
|
24
|
+
type TypeArrayNumber = TypeArray<number>;
|
|
25
|
+
type TypeArrayFunction = TypeArray<TypeCallbackFunction>;
|
|
26
|
+
type TypeObject<T> = {
|
|
27
27
|
[key: string]: T;
|
|
28
28
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
type TypeObjectAny = TypeObject<any>;
|
|
30
|
+
type TypeObjectString = TypeObject<string>;
|
|
31
|
+
type TypeObjectNumber = TypeObject<number>;
|
|
32
|
+
type TypeObjectFunction = TypeObject<TypeCallbackFunction>;
|
|
33
|
+
type TypeCallback<T, K, O, R> = (value: T, key: K, object: O) => R;
|
|
34
|
+
type TypeCallbackArray<T> = TypeCallback<T, number, TypeArray<T>, any>;
|
|
35
|
+
type TypeCallbackMap<T> = TypeCallback<T, number, TypeArray<T>, T>;
|
|
36
|
+
type TypeCallbackObject<T> = TypeCallback<T, string, TypeObject<T>, any>;
|
|
37
37
|
|
|
38
38
|
declare const debounce: (callback: TypeCallbackFunction, ms: number) => (...args: Array<any>) => void;
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
type EventCallback = (data?: any) => void;
|
|
41
41
|
interface EventDisposable {
|
|
42
42
|
dispose: () => void;
|
|
43
43
|
}
|
|
44
|
-
|
|
44
|
+
type EventList = Array<EventCallback>;
|
|
45
45
|
declare class EventEmitter {
|
|
46
46
|
private events;
|
|
47
47
|
constructor();
|
|
@@ -51,8 +51,8 @@ declare class EventEmitter {
|
|
|
51
51
|
emit(event: string, data?: any): void;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
type EachCallback = TypeCallback<any, string | number, any, any>;
|
|
55
|
+
type LoopCallback = (index: number, count: number) => any;
|
|
56
56
|
declare const forEachIndex: <T>(array: T[], callback: TypeCallbackArray<T>, thisArg?: any) => T[];
|
|
57
57
|
declare const forEachKey: <T>(object: TypeObject<T>, callback: TypeCallbackObject<T>, thisArg?: any) => TypeObject<T>;
|
|
58
58
|
declare const each: (object: any, callback: EachCallback, thisArg?: any) => any;
|
|
@@ -62,6 +62,7 @@ declare const noop: () => void;
|
|
|
62
62
|
|
|
63
63
|
declare const mask: (mask: string, text: string) => string;
|
|
64
64
|
declare const maskMoney: (mask: string, text: string, decimal: number) => string;
|
|
65
|
+
declare const maskTest: (mask: string, text: string) => boolean;
|
|
65
66
|
declare const maskReverse: (mask: string, text: string) => string;
|
|
66
67
|
|
|
67
68
|
declare const clamp: (value: number, min: number, max: number) => number;
|
|
@@ -69,7 +70,7 @@ declare const clamp: (value: number, min: number, max: number) => number;
|
|
|
69
70
|
declare const allowedObject: (allowed: string[], raw: TypeObjectAny) => TypeObjectAny;
|
|
70
71
|
declare const deniedObject: (denied: string[], raw: TypeObjectAny) => TypeObjectAny;
|
|
71
72
|
|
|
72
|
-
|
|
73
|
+
type ForEachCallback<T> = TypeCallback<T, number, Array<T>, Promise<void>>;
|
|
73
74
|
declare const forEachAsync: <T>(array: T[], callback: ForEachCallback<T>, thisArg?: any) => Promise<void>;
|
|
74
75
|
declare const forEachSeries: <T>(array: T[], callback: ForEachCallback<T>, thisArg?: any) => Promise<void>;
|
|
75
76
|
declare const sleep: (ms: number) => Promise<void>;
|
|
@@ -100,7 +101,7 @@ interface ValidationResult {
|
|
|
100
101
|
error?: string;
|
|
101
102
|
value?: any;
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
+
type ValidationCallback = (value: any) => ValidationResult;
|
|
104
105
|
declare class ValidationBase<T = any> {
|
|
105
106
|
name: string;
|
|
106
107
|
private value;
|
|
@@ -186,4 +187,4 @@ declare class Validation {
|
|
|
186
187
|
static validateAsync(value?: any, ...rules: Array<ValidationBase>): Promise<ValidationResult>;
|
|
187
188
|
}
|
|
188
189
|
|
|
189
|
-
export { EventCallback, EventDisposable, EventEmitter, EventList, TypeArray, TypeArrayAny, TypeArrayFunction, TypeArrayNumber, TypeArrayString, TypeCallback, TypeCallbackArray, TypeCallbackFunction, TypeCallbackMap, TypeCallbackObject, TypeObject, TypeObjectAny, TypeObjectFunction, TypeObjectNumber, TypeObjectString, Validation, ValidationBase, allowedObject, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, deniedObject, each, forEachAsync, forEachIndex, forEachKey, forEachSeries, isArray, isBoolean, isDef, isFloat, isFunction, isInteger, isNull, isNumber, isObject, isString, isUndefined, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, noop, parseSize, shadeColor, simpleID, sleep, stringReverse, toArray, toBool, type, uniqueID, upperCamelCase, uuidv4 };
|
|
190
|
+
export { EventCallback, EventDisposable, EventEmitter, EventList, TypeArray, TypeArrayAny, TypeArrayFunction, TypeArrayNumber, TypeArrayString, TypeCallback, TypeCallbackArray, TypeCallbackFunction, TypeCallbackMap, TypeCallbackObject, TypeObject, TypeObjectAny, TypeObjectFunction, TypeObjectNumber, TypeObjectString, Validation, ValidationBase, allowedObject, camelCase, chunkArray, clamp, cutText, dateFormat, debounce, deniedObject, each, forEachAsync, forEachIndex, forEachKey, forEachSeries, isArray, isBoolean, isDef, isFloat, isFunction, isInteger, isNull, isNumber, isObject, isString, isUndefined, loop, lowerCamelCase, map, mask, maskMoney, maskReverse, maskTest, noop, parseSize, 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.6
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2022 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
var t={},n=function(t){return"array"==p(t)},r=function(t){return"boolean"==p(t)},e=function(t){return"null"!=p(t)},u=function(t){return"function"==p(t)},i=function(t){return"number"==p(t)},o=function(t){return"object"==p(t)},a=function(t){return"string"==p(t)},c=function(t){return i(t)&&!!(t%1)},s=function(t){return i(t)&&!(t%1)},l=function(t){return null===t},f=function(t){return void 0===t},p=function(n){return null==n?"null":t[t.toString.call(n)]||"object"};["Array","Boolean","Function","Number","Object","String"].forEach((function(n){return t["[object "+n+"]"]=n.toLowerCase()}));var h=function(t,n){for(var r=[],e=0;e<t.length;e+=n)r.push(t.slice(e,e+n));return r},v=function(t){return n(t)?t:e(t)?[t]:[]},y=function(t){return"true"===t||"1"===t},m=function(t,n,r){return Math.max(n,Math.min(r,t))},g=function(t,n){return(r=parseInt(t,16)+n,m(r,0,255)).toString(16);var r},d=function(t,n){return"#"+t.replace(/^#/,"").replace(/../g,(function(t){return("0"+g(t,n)).slice(-2)}))},_=function(t){return Math.random().toString(16).slice(t)},b=function(){return _(-3)},w=function(){return _(-4)},x=function(){return w()+w()},k=function(){return x()+x()},j=function(){return b()+w()},O=function(){return k()+k()+k()+k()},C=function(){return x()+"-"+w()+"-4"+b()+"-"+w()+"-"+(x()+w())},S=function(t,n){return n.replace(/%[yYmdHMS]/g,(function(n){var r;switch(n){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 n.slice(1)}return("0"+r.toString()).slice(-2)}))},A=function(t,n){var r;return function(){for(var e=[],u=0;u<arguments.length;u++)e[u]=arguments[u];r&&clearTimeout(r),r=setTimeout((function(){return t.apply(void 0,e)}),n)}},M=function(){function t(){this.events=new Map}return t.prototype.on=function(t,n){var r=this,e=this.events.get(t);return e?e.push(n):this.events.set(t,[n]),{dispose:function(){return r.off(t,n)}}},t.prototype.once=function(t,n){var r=this,e=function(u){n(u),r.off(t,e)};return this.on(t,e),{dispose:function(){return r.off(t,e)}}},t.prototype.off=function(t,n){var r=this.events.get(t);if(n){if(r){var e=r.indexOf(n);e>=0&&r.splice(e,1)}}else this.events.set(t,[])},t.prototype.emit=function(t,n){var r=this.events.get(t);r&&r.forEach((function(t){return t(n)}))},t}(),P=function(t,n,r){for(var e=0;e<t.length&&!1!==n.call(r,t[e],e,t);e++);return t},U=function(t,n,r){for(var e="",u=Object.keys(t),i=0;i<u.length&&(e=u[i],!1!==n.call(r,t[e],e,t));i++);return t},E=function(t,r,e){return n(t)?P(t,r,e):o(t)?U(t,r,e):null},F=function(t,n,r){for(var e=1;e<=t&&!1!==n.call(r,e,t);e++);return t},L=function(t,n,r){for(var e="",u=Object.keys(t),i=0;i<u.length;i++)t[e=u[i]]=n.call(r,t[e],e,t);return t},T=function(){},q=function(t,n){if(t.length>n){var r=t.substring(0,n).split(" ");return r.pop(),r.join(" ")+"..."}return t},Y=function(t,n){return n(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,"")},$=function(t){return Y(t,(function(t){return t.replace(/\s(\w)/g,(function(t){return t.toUpperCase()}))}))},H=$,I=function(t){return Y(t,(function(t){return t.replace(/(\w)(\w*)/g,(function(t,n,r){return n.toUpperCase()+r}))}))},Z=function(t){return t.split("").reverse().join("")},z=function(t){return/^[A-Za-z]$/.test(t)},N=function(t,n,r){r.mask++,r.text++;var e=t[r.mask],u=n[r.text];return f(e)||f(u)?"":function(t,n,r){switch(t.toUpperCase()){case"#":return n;case"0":case"9":return function(t){return/^[0-9]$/.test(t)}(n)?n:(r.mask--,"");case"A":case"Z":return z(n)?n:(r.mask--,"");case"L":return z(n)?n.toLowerCase():(r.mask--,"");case"U":return z(n)?n.toUpperCase():(r.mask--,"");case"S":return function(t){return/^[^A-Za-z0-9]$/.test(t)}(n)?n:(r.mask--,"");default:return t!=n&&r.text--,t}}(e,u,r)+N(t,n,r)},W=function(t,n){return N(t,n,{mask:-1,text:-1})},B=function(t,n,r){return r++,n=parseInt((n||"0").replace(/\W/g,"")).toString(),G(t,n.length<r?("0".repeat(r)+n).slice(-1*r):n)},D=W,G=function(t,n){return Z(D(Z(t),Z(n)))},J=function(t,n){return J=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])},J(t,n)};function K(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}J(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var Q=function(){return Q=Object.assign||function(t){for(var n,r=1,e=arguments.length;r<e;r++)for(var u in n=arguments[r])Object.prototype.hasOwnProperty.call(n,u)&&(t[u]=n[u]);return t},Q.apply(this,arguments)};function R(t,n,r,e){return new(r||(r=Promise))((function(u,i){function o(t){try{c(e.next(t))}catch(t){i(t)}}function a(t){try{c(e.throw(t))}catch(t){i(t)}}function c(t){var n;t.done?u(t.value):(n=t.value,n instanceof r?n:new r((function(t){t(n)}))).then(o,a)}c((e=e.apply(t,n||[])).next())}))}function V(t,n){var r,e,u,i,o={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(c){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(o=0)),o;)try{if(r=1,e&&(u=2&a[0]?e.return:a[0]?e.throw||((u=e.return)&&u.call(e),0):e.next)&&!(u=u.call(e,a[1])).done)return u;switch(e=0,u&&(a=[2&a[0],u.value]),a[0]){case 0:case 1:u=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,e=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(u=o.trys,(u=u.length>0&&u[u.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!u||a[1]>u[0]&&a[1]<u[3])){o.label=a[1];break}if(6===a[0]&&o.label<u[1]){o.label=u[1],u=a;break}if(u&&o.label<u[2]){o.label=u[2],o.ops.push(a);break}u[2]&&o.ops.pop(),o.trys.pop();continue}a=n.call(t,o)}catch(t){a=[6,t],e=0}finally{r=u=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}function X(t,n,r){if(r||2===arguments.length)for(var e,u=0,i=n.length;u<i;u++)!e&&u in n||(e||(e=Array.prototype.slice.call(n,0,u)),e[u]=n[u]);return t.concat(e||Array.prototype.slice.call(n))}var tt=function(t,n){return t.reduce((function(t,r){var e;return Q(Q({},t),((e={})[r]=n[r],e))}),{})},nt=function(t,n){return tt(Object.keys(n).filter((function(n){return-1==t.indexOf(n)})),n)},rt=function(t,n,r){return R(void 0,void 0,void 0,(function(){return V(this,(function(e){switch(e.label){case 0:return[4,Promise.all(t.map((function(e,u){return n.call(r,e,u,t)})))];case 1:return e.sent(),[2]}}))}))},et=function(t,n,r){return R(void 0,void 0,void 0,(function(){var e;return V(this,(function(u){switch(u.label){case 0:e=0,u.label=1;case 1:return e<t.length?[4,n.call(r,t[e],e,t)]:[3,4];case 2:u.sent(),u.label=3;case 3:return e++,[3,1];case 4:return[2]}}))}))},ut=function(t){return new Promise((function(n){return setTimeout(n,t)}))},it=function(t){if(i(t))return t+"px";var n=function(t){return/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t)}(t)||["","0","",""],r=n[1],e=n[2],u=n[3];return(e.length?parseFloat(r+"."+e):parseInt(r))+(u||"px")},ot=function(){function t(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}return t.prototype.empty=function(t){var n=this;return this.value=t,this.unshift((function(t){var r=e(t);return{break:!r,value:r?t:n.value}}))},t.prototype.error=function(t){return this._error=t,this},t.prototype.invalid=function(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}},t.prototype.parse=function(){return this.unshift((function(t){return{value:t}}))},t.prototype.push=function(t){return this.rules.push(t),this},t.prototype.required=function(){var t=this;return this.unshift((function(n){return e(n)?{value:n}:t.invalid("["+t._type+"].required")}))},t.prototype.test=function(t){return!a(this.validate(t).error)},t.prototype.testAsync=function(t){return R(this,void 0,void 0,(function(){var n;return V(this,(function(r){switch(r.label){case 0:return n=a,[4,this.validateAsync(t)];case 1:return[2,!n.apply(void 0,[r.sent().error])]}}))}))},t.prototype.unshift=function(t){return this.rules.unshift(t),this},t.prototype.validate=function(t){if(this.custom.length)return this.invalid("[async]");for(var n={value:t},r=0;r<this.rules.length;r++){if((n=this.rules[r](n.value)).error)return{error:n.error};if(n.break)return{value:n.value}}return{value:n.value}},t.prototype.validateAsync=function(t){return R(this,void 0,void 0,(function(){var n,r;return V(this,(function(e){switch(e.label){case 0:if((n=this.validate(t)).error)return[2,{error:n.error}];r=0,e.label=1;case 1:return r<this.custom.length?[4,this.custom[r](n.value)]:[3,4];case 2:if((n=e.sent()).error)return[2,{error:n.error}];e.label=3;case 3:return r++,[3,1];case 4:return[2,{value:n.value}]}}))}))},t}(),at=function(t){function r(){var r=t.call(this)||this;return r._length=0,r._max=0,r._min=0,r._type="array",r.push((function(t){if(n(t)){if(r._items){r._items.name="";for(var e=0;e<t.length;e++){var u=r._items.validate(t[e]),i=u.error,o=u.value;if(i)return r.invalid("["+r._type+"]< "+i+"["+e+"] >");t[e]=o}}return{value:t}}return r.invalid("["+r._type+"]")})),r}return K(r,t),r.prototype.between=function(t,n){return this.min(t),this.max(n)},r.prototype.items=function(t){return this._items=t,this},r.prototype.length=function(t){var n=this;return this._length=t,this.push((function(t){return t.length==n._length?{value:t}:n.invalid("["+n._type+"].length("+n._length+")")}))},r.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t.length<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},r.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t.length>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},r}(ot),ct=function(t){function n(){var n=t.call(this)||this;return n._type="boolean",n.push((function(t){return r(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return K(n,t),n.prototype.parse=function(){return this.unshift((function(t){return{value:e(t)?-1!=[1,"1","true"].indexOf(t):t}}))},n}(ot),st=function(t){function n(){var n=t.call(this)||this;return n._max=0,n._min=0,n._type="number",n.push((function(t){return i(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return K(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n.prototype.parse=function(){return this.unshift((function(t){return{value:e(t)?parseFloat(t):t}}))},n}(ot),lt=function(t){function n(){var n=t.call(this)||this;return n._type="object",n.push((function(t){if(o(t)){if(n._keys){var r=[];return Object.keys(n._keys).forEach((function(u){if(n._keys){var i=n._keys[u];i.name=u;var o=i.validate(t[u]),a=o.error,c=o.value;a&&r.push(a),e(c)&&(t[u]=c)}})),r.length?n.invalid("["+n._type+"]{ "+r.join(", ")+" }"):{value:t}}return{value:t}}return n.invalid("["+n._type+"]")})),n}return K(n,t),n.prototype.keys=function(t){return this._keys=t,this},n}(ot),ft=function(t){function n(){var n=t.call(this)||this;return n._length=0,n._max=0,n._min=0,n._type="string",n.push((function(t){return a(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return K(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.email=function(){var t=this;return this.push((function(n){return/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(n)?{value:n}:t.invalid("["+t._type+"].email")}))},n.prototype.length=function(t){var n=this;return this._length=t,this.push((function(t){return t.length==n._length?{value:t}:n.invalid("["+n._type+"].length("+n._length+")")}))},n.prototype.lowerCase=function(){return this.push((function(t){return{value:t.toLowerCase()}}))},n.prototype.lowerCamelCase=function(){return this.push((function(t){return{value:$(t)}}))},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t.length<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.mask=function(t){return this.push((function(n){return{value:W(t,n)}}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t.length>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n.prototype.parse=function(){return this.unshift((function(t){return{value:o(t)?JSON.stringify(t):e(t)?t.toString():t}}))},n.prototype.required=function(){var t=this;return this.push((function(n){return n.length?{value:n}:t.invalid("["+t._type+"].required")}))},n.prototype.trim=function(){return this.push((function(t){return{value:t.trim()}}))},n.prototype.upperCase=function(){return this.push((function(t){return{value:t.toUpperCase()}}))},n.prototype.upperCamelCase=function(){return this.push((function(t){return{value:I(t)}}))},n}(ot),pt=function(){function t(){}return t.array=function(t){return(new at).items(t)},t.boolean=function(){return new ct},t.number=function(){return new st},t.object=function(t){return(new lt).keys(t)},t.string=function(){return new ft},t.prototype.test=function(n){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return!a(t.validate.apply(t,X([n],r,!1)).error)},t.prototype.testAsync=function(n){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return R(this,void 0,void 0,(function(){var e;return V(this,(function(u){switch(u.label){case 0:return e=a,[4,t.validateAsync.apply(t,X([n],r,!1))];case 1:return[2,!e.apply(void 0,[u.sent().error])]}}))}))},t.validate=function(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];for(var e={value:t},u=0;u<n.length;u++)if((e=n[u].validate(e.value)).error)return{error:e.error};return e},t.validateAsync=function(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return R(this,void 0,void 0,(function(){var r,e;return V(this,(function(u){switch(u.label){case 0:r={value:t},e=0,u.label=1;case 1:return e<n.length?[4,n[e].validateAsync(r.value)]:[3,4];case 2:if((r=u.sent()).error)return[2,{error:r.error}];u.label=3;case 3:return e++,[3,1];case 4:return[2,r]}}))}))},t}();export{M as EventEmitter,pt as Validation,ot as ValidationBase,tt as allowedObject,H as camelCase,h as chunkArray,m as clamp,q as cutText,S as dateFormat,A as debounce,nt as deniedObject,E as each,rt as forEachAsync,P as forEachIndex,U as forEachKey,et as forEachSeries,n as isArray,r as isBoolean,e as isDef,c as isFloat,u as isFunction,s as isInteger,l as isNull,i as isNumber,o as isObject,a as isString,f as isUndefined,F as loop,$ as lowerCamelCase,L as map,W as mask,B as maskMoney,G as maskReverse,T as noop,it as parseSize,d as shadeColor,j as simpleID,ut as sleep,Z as stringReverse,v as toArray,y as toBool,p as type,O as uniqueID,I as upperCamelCase,C 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(t)?t:s(t)?[t]:[],d=t=>"true"===t||"1"===t,g=(t,e,r)=>Math.max(e,Math.min(r,t)),_=(t,e)=>{return(r=parseInt(t,16)+e,g(r,0,255)).toString(16);var r},f=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+_(t,e)).slice(-2))),y=t=>Math.random().toString(16).slice(t),x=()=>y(-3),b=()=>y(-4),w=()=>b()+b(),k=()=>w()+w(),j=()=>x()+b(),C=()=>k()+k()+k()+k(),S=()=>w()+"-"+b()+"-4"+x()+"-"+b()+"-"+(w()+b()),A=(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)})),O=(t,e)=>{let r;return(...s)=>{r&&clearTimeout(r),r=setTimeout((()=>t(...s)),e)}};class M{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 U=(t,e,r)=>{for(let s=0;s<t.length&&!1!==e.call(r,t[s],s,t);s++);return t},F=(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},L=(t,r,s)=>e(t)?U(t,r,s):a(t)?F(t,r,s):null,q=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},Y=(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},$=()=>{},E=(t,e)=>{if(t.length>e){const r=t.substring(0,e).split(" ");return r.pop(),r.join(" ")+"..."}return t},H=(t,e)=>e(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,""),I=t=>H(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),P=I,T=t=>H(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),Z=t=>t.split("").reverse().join(""),z=t=>/^[A-Za-z]$/.test(t),N=t=>(t.mask--,""),W=(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:N(r);case"A":case"Z":return z(e)?e:N(r);case"L":return z(e)?e.toLowerCase():N(r);case"U":return z(e)?e.toUpperCase():N(r);case"S":return(t=>/^[^A-Za-z0-9]$/.test(t))(e)?e:N(r);default:return t!=e&&r.text--,t}})(s,i,r)+W(t,e,r)},B=(t,e)=>W(t,e,{mask:-1,text:-1}),D=(t,e,r)=>(r++,e=parseInt((e||"0").replace(/\W/g,"")).toString(),K(t,e.length<r?("0".repeat(r)+e).slice(-1*r):e)),J=B,G=(t,e)=>t.length==J(t,e).length,K=(t,e)=>Z(J(Z(t),Z(e))),Q=(t,e)=>t.reduce(((t,r)=>Object.assign(Object.assign({},t),{[r]:e[r]})),{}),R=(t,e)=>Q(Object.keys(e).filter((e=>-1==t.indexOf(e))),e);function V(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 X=(t,e,r)=>V(void 0,void 0,void 0,(function*(){yield Promise.all(t.map(((s,i)=>e.call(r,s,i,t))))})),tt=(t,e,r)=>V(void 0,void 0,void 0,(function*(){for(let s=0;s<t.length;s++)yield e.call(r,t[s],s,t)})),et=t=>new Promise((e=>setTimeout(e,t))),rt=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 st{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 V(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 V(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 it extends st{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 nt extends st{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","true"].indexOf(t):t})))}}class at extends st{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 ht extends st{constructor(){super(),this._type="object",this.push((t=>{if(a(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){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?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class ut extends st{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:I(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:B(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:T(t)})))}}class lt{static array(t){return(new it).items(t)}static boolean(){return new nt}static number(){return new at}static object(t){return(new ht).keys(t)}static string(){return new ut}test(t,...e){return!h(lt.validate(t,...e).error)}testAsync(t,...e){return V(this,void 0,void 0,(function*(){return!h((yield lt.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 V(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{M as EventEmitter,lt as Validation,st as ValidationBase,Q as allowedObject,P as camelCase,p as chunkArray,g as clamp,E as cutText,A as dateFormat,O as debounce,R as deniedObject,L as each,X as forEachAsync,U as forEachIndex,F as forEachKey,tt 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,q as loop,I as lowerCamelCase,Y as map,B as mask,D as maskMoney,K as maskReverse,G as maskTest,$ as noop,rt as parseSize,f as shadeColor,j as simpleID,et as sleep,Z as stringReverse,m as toArray,d as toBool,v as type,C as uniqueID,T as upperCamelCase,S 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.6
|
|
3
3
|
* JavaScript utility library for web and nodejs development
|
|
4
4
|
* (c) 2022 Andrew Caires
|
|
5
5
|
* @license: MIT
|
|
6
6
|
*/
|
|
7
|
-
var UtilsJS=function(t){"use strict";var n={},r=function(t){return"array"==c(t)},e=function(t){return"boolean"==c(t)},u=function(t){return"null"!=c(t)},i=function(t){return"number"==c(t)},o=function(t){return"object"==c(t)},a=function(t){return"string"==c(t)},s=function(t){return void 0===t},c=function(t){return null==t?"null":n[n.toString.call(t)]||"object"};["Array","Boolean","Function","Number","Object","String"].forEach((function(t){return n["[object "+t+"]"]=t.toLowerCase()}));var l=function(t,n,r){return Math.max(n,Math.min(r,t))},f=function(t,n){return(r=parseInt(t,16)+n,l(r,0,255)).toString(16);var r},p=function(t){return Math.random().toString(16).slice(t)},h=function(){return p(-3)},v=function(){return p(-4)},y=function(){return v()+v()},m=function(){return y()+y()},d=function(){function t(){this.events=new Map}return t.prototype.on=function(t,n){var r=this,e=this.events.get(t);return e?e.push(n):this.events.set(t,[n]),{dispose:function(){return r.off(t,n)}}},t.prototype.once=function(t,n){var r=this,e=function(u){n(u),r.off(t,e)};return this.on(t,e),{dispose:function(){return r.off(t,e)}}},t.prototype.off=function(t,n){var r=this.events.get(t);if(n){if(r){var e=r.indexOf(n);e>=0&&r.splice(e,1)}}else this.events.set(t,[])},t.prototype.emit=function(t,n){var r=this.events.get(t);r&&r.forEach((function(t){return t(n)}))},t}(),g=function(t,n,r){for(var e=0;e<t.length&&!1!==n.call(r,t[e],e,t);e++);return t},_=function(t,n,r){for(var e="",u=Object.keys(t),i=0;i<u.length&&(e=u[i],!1!==n.call(r,t[e],e,t));i++);return t},b=function(t,n){return n(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,"")},w=function(t){return b(t,(function(t){return t.replace(/\s(\w)/g,(function(t){return t.toUpperCase()}))}))},x=w,k=function(t){return b(t,(function(t){return t.replace(/(\w)(\w*)/g,(function(t,n,r){return n.toUpperCase()+r}))}))},C=function(t){return t.split("").reverse().join("")},j=function(t){return/^[A-Za-z]$/.test(t)},O=function(t,n,r){r.mask++,r.text++;var e=t[r.mask],u=n[r.text];return s(e)||s(u)?"":function(t,n,r){switch(t.toUpperCase()){case"#":return n;case"0":case"9":return function(t){return/^[0-9]$/.test(t)}(n)?n:(r.mask--,"");case"A":case"Z":return j(n)?n:(r.mask--,"");case"L":return j(n)?n.toLowerCase():(r.mask--,"");case"U":return j(n)?n.toUpperCase():(r.mask--,"");case"S":return function(t){return/^[^A-Za-z0-9]$/.test(t)}(n)?n:(r.mask--,"");default:return t!=n&&r.text--,t}}(e,u,r)+O(t,n,r)},S=function(t,n){return O(t,n,{mask:-1,text:-1})},A=S,E=function(t,n){return C(A(C(t),C(n)))},M=function(t,n){return M=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])},M(t,n)};function F(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}M(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var U=function(){return U=Object.assign||function(t){for(var n,r=1,e=arguments.length;r<e;r++)for(var u in n=arguments[r])Object.prototype.hasOwnProperty.call(n,u)&&(t[u]=n[u]);return t},U.apply(this,arguments)};function I(t,n,r,e){return new(r||(r=Promise))((function(u,i){function o(t){try{s(e.next(t))}catch(t){i(t)}}function a(t){try{s(e.throw(t))}catch(t){i(t)}}function s(t){var n;t.done?u(t.value):(n=t.value,n instanceof r?n:new r((function(t){t(n)}))).then(o,a)}s((e=e.apply(t,n||[])).next())}))}function P(t,n){var r,e,u,i,o={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(s){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(o=0)),o;)try{if(r=1,e&&(u=2&a[0]?e.return:a[0]?e.throw||((u=e.return)&&u.call(e),0):e.next)&&!(u=u.call(e,a[1])).done)return u;switch(e=0,u&&(a=[2&a[0],u.value]),a[0]){case 0:case 1:u=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,e=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(u=o.trys,(u=u.length>0&&u[u.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!u||a[1]>u[0]&&a[1]<u[3])){o.label=a[1];break}if(6===a[0]&&o.label<u[1]){o.label=u[1],u=a;break}if(u&&o.label<u[2]){o.label=u[2],o.ops.push(a);break}u[2]&&o.ops.pop(),o.trys.pop();continue}a=n.call(t,o)}catch(t){a=[6,t],e=0}finally{r=u=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}function T(t,n,r){if(r||2===arguments.length)for(var e,u=0,i=n.length;u<i;u++)!e&&u in n||(e||(e=Array.prototype.slice.call(n,0,u)),e[u]=n[u]);return t.concat(e||Array.prototype.slice.call(n))}var q=function(t,n){return t.reduce((function(t,r){var e;return U(U({},t),((e={})[r]=n[r],e))}),{})},L=function(){function t(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}return t.prototype.empty=function(t){var n=this;return this.value=t,this.unshift((function(t){var r=u(t);return{break:!r,value:r?t:n.value}}))},t.prototype.error=function(t){return this._error=t,this},t.prototype.invalid=function(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}},t.prototype.parse=function(){return this.unshift((function(t){return{value:t}}))},t.prototype.push=function(t){return this.rules.push(t),this},t.prototype.required=function(){var t=this;return this.unshift((function(n){return u(n)?{value:n}:t.invalid("["+t._type+"].required")}))},t.prototype.test=function(t){return!a(this.validate(t).error)},t.prototype.testAsync=function(t){return I(this,void 0,void 0,(function(){var n;return P(this,(function(r){switch(r.label){case 0:return n=a,[4,this.validateAsync(t)];case 1:return[2,!n.apply(void 0,[r.sent().error])]}}))}))},t.prototype.unshift=function(t){return this.rules.unshift(t),this},t.prototype.validate=function(t){if(this.custom.length)return this.invalid("[async]");for(var n={value:t},r=0;r<this.rules.length;r++){if((n=this.rules[r](n.value)).error)return{error:n.error};if(n.break)return{value:n.value}}return{value:n.value}},t.prototype.validateAsync=function(t){return I(this,void 0,void 0,(function(){var n,r;return P(this,(function(e){switch(e.label){case 0:if((n=this.validate(t)).error)return[2,{error:n.error}];r=0,e.label=1;case 1:return r<this.custom.length?[4,this.custom[r](n.value)]:[3,4];case 2:if((n=e.sent()).error)return[2,{error:n.error}];e.label=3;case 3:return r++,[3,1];case 4:return[2,{value:n.value}]}}))}))},t}(),B=function(t){function n(){var n=t.call(this)||this;return n._length=0,n._max=0,n._min=0,n._type="array",n.push((function(t){if(r(t)){if(n._items){n._items.name="";for(var e=0;e<t.length;e++){var u=n._items.validate(t[e]),i=u.error,o=u.value;if(i)return n.invalid("["+n._type+"]< "+i+"["+e+"] >");t[e]=o}}return{value:t}}return n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.items=function(t){return this._items=t,this},n.prototype.length=function(t){var n=this;return this._length=t,this.push((function(t){return t.length==n._length?{value:t}:n.invalid("["+n._type+"].length("+n._length+")")}))},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t.length<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t.length>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n}(L),D=function(t){function n(){var n=t.call(this)||this;return n._type="boolean",n.push((function(t){return e(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.parse=function(){return this.unshift((function(t){return{value:u(t)?-1!=[1,"1","true"].indexOf(t):t}}))},n}(L),N=function(t){function n(){var n=t.call(this)||this;return n._max=0,n._min=0,n._type="number",n.push((function(t){return i(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n.prototype.parse=function(){return this.unshift((function(t){return{value:u(t)?parseFloat(t):t}}))},n}(L),Y=function(t){function n(){var n=t.call(this)||this;return n._type="object",n.push((function(t){if(o(t)){if(n._keys){var r=[];return Object.keys(n._keys).forEach((function(e){if(n._keys){var i=n._keys[e];i.name=e;var o=i.validate(t[e]),a=o.error,s=o.value;a&&r.push(a),u(s)&&(t[e]=s)}})),r.length?n.invalid("["+n._type+"]{ "+r.join(", ")+" }"):{value:t}}return{value:t}}return n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.keys=function(t){return this._keys=t,this},n}(L),$=function(t){function n(){var n=t.call(this)||this;return n._length=0,n._max=0,n._min=0,n._type="string",n.push((function(t){return a(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.email=function(){var t=this;return this.push((function(n){return/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(n)?{value:n}:t.invalid("["+t._type+"].email")}))},n.prototype.length=function(t){var n=this;return this._length=t,this.push((function(t){return t.length==n._length?{value:t}:n.invalid("["+n._type+"].length("+n._length+")")}))},n.prototype.lowerCase=function(){return this.push((function(t){return{value:t.toLowerCase()}}))},n.prototype.lowerCamelCase=function(){return this.push((function(t){return{value:w(t)}}))},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t.length<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.mask=function(t){return this.push((function(n){return{value:S(t,n)}}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t.length>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n.prototype.parse=function(){return this.unshift((function(t){return{value:o(t)?JSON.stringify(t):u(t)?t.toString():t}}))},n.prototype.required=function(){var t=this;return this.push((function(n){return n.length?{value:n}:t.invalid("["+t._type+"].required")}))},n.prototype.trim=function(){return this.push((function(t){return{value:t.trim()}}))},n.prototype.upperCase=function(){return this.push((function(t){return{value:t.toUpperCase()}}))},n.prototype.upperCamelCase=function(){return this.push((function(t){return{value:k(t)}}))},n}(L),z=function(){function t(){}return t.array=function(t){return(new B).items(t)},t.boolean=function(){return new D},t.number=function(){return new N},t.object=function(t){return(new Y).keys(t)},t.string=function(){return new $},t.prototype.test=function(n){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return!a(t.validate.apply(t,T([n],r,!1)).error)},t.prototype.testAsync=function(n){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return I(this,void 0,void 0,(function(){var e;return P(this,(function(u){switch(u.label){case 0:return e=a,[4,t.validateAsync.apply(t,T([n],r,!1))];case 1:return[2,!e.apply(void 0,[u.sent().error])]}}))}))},t.validate=function(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];for(var e={value:t},u=0;u<n.length;u++)if((e=n[u].validate(e.value)).error)return{error:e.error};return e},t.validateAsync=function(t){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];return I(this,void 0,void 0,(function(){var r,e;return P(this,(function(u){switch(u.label){case 0:r={value:t},e=0,u.label=1;case 1:return e<n.length?[4,n[e].validateAsync(r.value)]:[3,4];case 2:if((r=u.sent()).error)return[2,{error:r.error}];u.label=3;case 3:return e++,[3,1];case 4:return[2,r]}}))}))},t}();return t.EventEmitter=d,t.Validation=z,t.ValidationBase=L,t.allowedObject=q,t.camelCase=x,t.chunkArray=function(t,n){for(var r=[],e=0;e<t.length;e+=n)r.push(t.slice(e,e+n));return r},t.clamp=l,t.cutText=function(t,n){if(t.length>n){var r=t.substring(0,n).split(" ");return r.pop(),r.join(" ")+"..."}return t},t.dateFormat=function(t,n){return n.replace(/%[yYmdHMS]/g,(function(n){var r;switch(n){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 n.slice(1)}return("0"+r.toString()).slice(-2)}))},t.debounce=function(t,n){var r;return function(){for(var e=[],u=0;u<arguments.length;u++)e[u]=arguments[u];r&&clearTimeout(r),r=setTimeout((function(){return t.apply(void 0,e)}),n)}},t.deniedObject=function(t,n){return q(Object.keys(n).filter((function(n){return-1==t.indexOf(n)})),n)},t.each=function(t,n,e){return r(t)?g(t,n,e):o(t)?_(t,n,e):null},t.forEachAsync=function(t,n,r){return I(void 0,void 0,void 0,(function(){return P(this,(function(e){switch(e.label){case 0:return[4,Promise.all(t.map((function(e,u){return n.call(r,e,u,t)})))];case 1:return e.sent(),[2]}}))}))},t.forEachIndex=g,t.forEachKey=_,t.forEachSeries=function(t,n,r){return I(void 0,void 0,void 0,(function(){var e;return P(this,(function(u){switch(u.label){case 0:e=0,u.label=1;case 1:return e<t.length?[4,n.call(r,t[e],e,t)]:[3,4];case 2:u.sent(),u.label=3;case 3:return e++,[3,1];case 4:return[2]}}))}))},t.isArray=r,t.isBoolean=e,t.isDef=u,t.isFloat=function(t){return i(t)&&!!(t%1)},t.isFunction=function(t){return"function"==c(t)},t.isInteger=function(t){return i(t)&&!(t%1)},t.isNull=function(t){return null===t},t.isNumber=i,t.isObject=o,t.isString=a,t.isUndefined=s,t.loop=function(t,n,r){for(var e=1;e<=t&&!1!==n.call(r,e,t);e++);return t},t.lowerCamelCase=w,t.map=function(t,n,r){for(var e="",u=Object.keys(t),i=0;i<u.length;i++)t[e=u[i]]=n.call(r,t[e],e,t);return t},t.mask=S,t.maskMoney=function(t,n,r){return r++,n=parseInt((n||"0").replace(/\W/g,"")).toString(),E(t,n.length<r?("0".repeat(r)+n).slice(-1*r):n)},t.maskReverse=E,t.noop=function(){},t.parseSize=function(t){if(i(t))return t+"px";var n=function(t){return/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t)}(t)||["","0","",""],r=n[1],e=n[2],u=n[3];return(e.length?parseFloat(r+"."+e):parseInt(r))+(u||"px")},t.shadeColor=function(t,n){return"#"+t.replace(/^#/,"").replace(/../g,(function(t){return("0"+f(t,n)).slice(-2)}))},t.simpleID=function(){return h()+v()},t.sleep=function(t){return new Promise((function(n){return setTimeout(n,t)}))},t.stringReverse=C,t.toArray=function(t){return r(t)?t:u(t)?[t]:[]},t.toBool=function(t){return"true"===t||"1"===t},t.type=c,t.uniqueID=function(){return m()+m()+m()+m()},t.upperCamelCase=k,t.uuidv4=function(){return y()+"-"+v()+"-4"+h()+"-"+v()+"-"+(y()+v())},t}({});
|
|
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,e)=>{return(r=parseInt(t,16)+e,o(r,0,255)).toString(16);var r},p=t=>Math.random().toString(16).slice(t),v=()=>p(-3),m=()=>p(-4),d=()=>m()+m(),f=()=>d()+d();const g=(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,""),x=t=>y(t,(t=>t.replace(/\s(\w)/g,(t=>t.toUpperCase())))),b=x,w=t=>y(t,(t=>t.replace(/(\w)(\w*)/g,((t,e,r)=>e.toUpperCase()+r)))),k=t=>t.split("").reverse().join(""),C=t=>/^[A-Za-z]$/.test(t),j=t=>(t.mask--,""),S=(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:j(r);case"A":case"Z":return C(e)?e:j(r);case"L":return C(e)?e.toLowerCase():j(r);case"U":return C(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)+S(t,e,r)},A=(t,e)=>S(t,e,{mask:-1,text:-1}),O=A,E=(t,e)=>k(O(k(t),k(e))),M=(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{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 U{constructor(){this.name="",this._error="",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}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 F(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 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};return{value:e.value}}))}}class I extends U{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 q extends U{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","true"].indexOf(t):t})))}}class L extends U{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 T extends U{constructor(){super(),this._type="object",this.push((t=>{if(a(t)){if(this._keys){const e=[];return Object.keys(this._keys).forEach((r=>{if(this._keys){const s=this._keys[r];s.name=r;const{error:n,value:a}=s.validate(t[r]);n&&e.push(n),i(a)&&(t[r]=a)}})),e.length?this.invalid("["+this._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return this.invalid("["+this._type+"]")}))}keys(t){return this._keys=t,this}}class B extends U{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:x(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:A(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:w(t)})))}}class D{static array(t){return(new I).items(t)}static boolean(){return new q}static number(){return new L}static object(t){return(new T).keys(t)}static string(){return new B}test(t,...e){return!h(D.validate(t,...e).error)}testAsync(t,...e){return F(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 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}))}}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=U,t.allowedObject=M,t.camelCase=b,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.deniedObject=(t,e)=>M(Object.keys(e).filter((e=>-1==t.indexOf(e))),e),t.each=(t,e,s)=>r(t)?g(t,e,s):a(t)?_(t,e,s):null,t.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))))})),t.forEachIndex=g,t.forEachKey=_,t.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)})),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.loop=(t,e,r)=>{for(let s=1;s<=t&&!1!==e.call(r,s,t);s++);return t},t.lowerCamelCase=x,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=A,t.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)),t.maskReverse=E,t.maskTest=(t,e)=>t.length==O(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.shadeColor=(t,e)=>"#"+t.replace(/^#/,"").replace(/../g,(t=>("0"+c(t,e)).slice(-2))),t.simpleID=()=>v()+m(),t.sleep=t=>new Promise((e=>setTimeout(e,t))),t.stringReverse=k,t.toArray=t=>r(t)?t:i(t)?[t]:[],t.toBool=t=>"true"===t||"1"===t,t.type=l,t.uniqueID=()=>f()+f()+f()+f(),t.upperCamelCase=w,t.uuidv4=()=>d()+"-"+m()+"-4"+v()+"-"+m()+"-"+(d()+m()),t}({});
|
package/package.json
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andrewcaires/utils.js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "JavaScript utility library for web and nodejs development",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"main": "./dist/index.cjs.js",
|
|
6
|
+
"unpkg": "./dist/index.min.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"module": "./dist/index.esm.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.esm.js",
|
|
13
|
+
"browser": "./dist/index.min.js",
|
|
14
|
+
"require": "./dist/index.cjs.js",
|
|
15
|
+
"default": "./dist/index.cjs.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
10
22
|
"scripts": {
|
|
11
|
-
"build": "rollup -c
|
|
12
|
-
"dev": "ts-node-dev --respawn --transpile-only ./src/index.ts"
|
|
13
|
-
"lint": "eslint --ext .ts --fix src"
|
|
23
|
+
"build": "rollup --bundleConfigAsCjs -c rollup.config.js",
|
|
24
|
+
"dev": "ts-node-dev --respawn --transpile-only ./src/index.ts"
|
|
14
25
|
},
|
|
15
26
|
"repository": {
|
|
16
27
|
"type": "git",
|
|
@@ -32,13 +43,14 @@
|
|
|
32
43
|
},
|
|
33
44
|
"homepage": "https://github.com/andrewcaires/npm/tree/main/utils.js#readme",
|
|
34
45
|
"devDependencies": {
|
|
35
|
-
"@rollup/plugin-commonjs": "^23.0.
|
|
36
|
-
"@rollup/plugin-terser": "^0.
|
|
37
|
-
"@rollup/plugin-typescript": "^
|
|
38
|
-
"@
|
|
39
|
-
"@typescript-eslint/
|
|
40
|
-
"eslint": "^
|
|
41
|
-
"
|
|
46
|
+
"@rollup/plugin-commonjs": "^23.0.4",
|
|
47
|
+
"@rollup/plugin-terser": "^0.2.0",
|
|
48
|
+
"@rollup/plugin-typescript": "^10.0.1",
|
|
49
|
+
"@types/node": "^18.11.11",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^5.46.0",
|
|
51
|
+
"@typescript-eslint/parser": "^5.46.0",
|
|
52
|
+
"eslint": "^8.29.0",
|
|
53
|
+
"rollup": "^3.7.0",
|
|
42
54
|
"rollup-plugin-dts": "^5.0.0",
|
|
43
55
|
"ts-node-dev": "^2.0.0",
|
|
44
56
|
"tslib": "^2.4.1"
|
package/dist/index.umd.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* @andrewcaires/utils.js v0.2.4
|
|
3
|
-
* JavaScript utility library for web and nodejs development
|
|
4
|
-
* (c) 2022 Andrew Caires
|
|
5
|
-
* @license: MIT
|
|
6
|
-
*/
|
|
7
|
-
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).UtilsJS={})}(this,(function(t){"use strict";var n={},e=function(t){return"array"==c(t)},r=function(t){return"boolean"==c(t)},u=function(t){return"null"!=c(t)},i=function(t){return"number"==c(t)},o=function(t){return"object"==c(t)},a=function(t){return"string"==c(t)},s=function(t){return void 0===t},c=function(t){return null==t?"null":n[n.toString.call(t)]||"object"};["Array","Boolean","Function","Number","Object","String"].forEach((function(t){return n["[object "+t+"]"]=t.toLowerCase()}));var l=function(t,n,e){return Math.max(n,Math.min(e,t))},f=function(t,n){return(e=parseInt(t,16)+n,l(e,0,255)).toString(16);var e},p=function(t){return Math.random().toString(16).slice(t)},h=function(){return p(-3)},v=function(){return p(-4)},y=function(){return v()+v()},m=function(){return y()+y()},d=function(){function t(){this.events=new Map}return t.prototype.on=function(t,n){var e=this,r=this.events.get(t);return r?r.push(n):this.events.set(t,[n]),{dispose:function(){return e.off(t,n)}}},t.prototype.once=function(t,n){var e=this,r=function(u){n(u),e.off(t,r)};return this.on(t,r),{dispose:function(){return e.off(t,r)}}},t.prototype.off=function(t,n){var e=this.events.get(t);if(n){if(e){var r=e.indexOf(n);r>=0&&e.splice(r,1)}}else this.events.set(t,[])},t.prototype.emit=function(t,n){var e=this.events.get(t);e&&e.forEach((function(t){return t(n)}))},t}(),g=function(t,n,e){for(var r=0;r<t.length&&!1!==n.call(e,t[r],r,t);r++);return t},_=function(t,n,e){for(var r="",u=Object.keys(t),i=0;i<u.length&&(r=u[i],!1!==n.call(e,t[r],r,t));i++);return t},b=function(t,n){return n(t.toLowerCase().replace(/\W+/g," ")).replace(/\s/g,"")},w=function(t){return b(t,(function(t){return t.replace(/\s(\w)/g,(function(t){return t.toUpperCase()}))}))},x=w,k=function(t){return b(t,(function(t){return t.replace(/(\w)(\w*)/g,(function(t,n,e){return n.toUpperCase()+e}))}))},j=function(t){return t.split("").reverse().join("")},C=function(t){return/^[A-Za-z]$/.test(t)},O=function(t,n,e){e.mask++,e.text++;var r=t[e.mask],u=n[e.text];return s(r)||s(u)?"":function(t,n,e){switch(t.toUpperCase()){case"#":return n;case"0":case"9":return function(t){return/^[0-9]$/.test(t)}(n)?n:(e.mask--,"");case"A":case"Z":return C(n)?n:(e.mask--,"");case"L":return C(n)?n.toLowerCase():(e.mask--,"");case"U":return C(n)?n.toUpperCase():(e.mask--,"");case"S":return function(t){return/^[^A-Za-z0-9]$/.test(t)}(n)?n:(e.mask--,"");default:return t!=n&&e.text--,t}}(r,u,e)+O(t,n,e)},S=function(t,n){return O(t,n,{mask:-1,text:-1})},A=S,E=function(t,n){return j(A(j(t),j(n)))},M=function(t,n){return M=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var e in n)Object.prototype.hasOwnProperty.call(n,e)&&(t[e]=n[e])},M(t,n)};function F(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function e(){this.constructor=t}M(t,n),t.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}var T=function(){return T=Object.assign||function(t){for(var n,e=1,r=arguments.length;e<r;e++)for(var u in n=arguments[e])Object.prototype.hasOwnProperty.call(n,u)&&(t[u]=n[u]);return t},T.apply(this,arguments)};function U(t,n,e,r){return new(e||(e=Promise))((function(u,i){function o(t){try{s(r.next(t))}catch(t){i(t)}}function a(t){try{s(r.throw(t))}catch(t){i(t)}}function s(t){var n;t.done?u(t.value):(n=t.value,n instanceof e?n:new e((function(t){t(n)}))).then(o,a)}s((r=r.apply(t,n||[])).next())}))}function I(t,n){var e,r,u,i,o={label:0,sent:function(){if(1&u[0])throw u[1];return u[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(a){return function(s){return function(a){if(e)throw new TypeError("Generator is already executing.");for(;i&&(i=0,a[0]&&(o=0)),o;)try{if(e=1,r&&(u=2&a[0]?r.return:a[0]?r.throw||((u=r.return)&&u.call(r),0):r.next)&&!(u=u.call(r,a[1])).done)return u;switch(r=0,u&&(a=[2&a[0],u.value]),a[0]){case 0:case 1:u=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,r=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(u=o.trys,(u=u.length>0&&u[u.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!u||a[1]>u[0]&&a[1]<u[3])){o.label=a[1];break}if(6===a[0]&&o.label<u[1]){o.label=u[1],u=a;break}if(u&&o.label<u[2]){o.label=u[2],o.ops.push(a);break}u[2]&&o.ops.pop(),o.trys.pop();continue}a=n.call(t,o)}catch(t){a=[6,t],r=0}finally{e=u=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}function P(t,n,e){if(e||2===arguments.length)for(var r,u=0,i=n.length;u<i;u++)!r&&u in n||(r||(r=Array.prototype.slice.call(n,0,u)),r[u]=n[u]);return t.concat(r||Array.prototype.slice.call(n))}var q=function(t,n){return t.reduce((function(t,e){var r;return T(T({},t),((r={})[e]=n[e],r))}),{})},L=function(){function t(){this.name="",this._error="",this._type="any",this.rules=[],this.custom=[]}return t.prototype.empty=function(t){var n=this;return this.value=t,this.unshift((function(t){var e=u(t);return{break:!e,value:e?t:n.value}}))},t.prototype.error=function(t){return this._error=t,this},t.prototype.invalid=function(t){return{error:this._error?this._error:this.name?this.name+":"+t:t}},t.prototype.parse=function(){return this.unshift((function(t){return{value:t}}))},t.prototype.push=function(t){return this.rules.push(t),this},t.prototype.required=function(){var t=this;return this.unshift((function(n){return u(n)?{value:n}:t.invalid("["+t._type+"].required")}))},t.prototype.test=function(t){return!a(this.validate(t).error)},t.prototype.testAsync=function(t){return U(this,void 0,void 0,(function(){var n;return I(this,(function(e){switch(e.label){case 0:return n=a,[4,this.validateAsync(t)];case 1:return[2,!n.apply(void 0,[e.sent().error])]}}))}))},t.prototype.unshift=function(t){return this.rules.unshift(t),this},t.prototype.validate=function(t){if(this.custom.length)return this.invalid("[async]");for(var n={value:t},e=0;e<this.rules.length;e++){if((n=this.rules[e](n.value)).error)return{error:n.error};if(n.break)return{value:n.value}}return{value:n.value}},t.prototype.validateAsync=function(t){return U(this,void 0,void 0,(function(){var n,e;return I(this,(function(r){switch(r.label){case 0:if((n=this.validate(t)).error)return[2,{error:n.error}];e=0,r.label=1;case 1:return e<this.custom.length?[4,this.custom[e](n.value)]:[3,4];case 2:if((n=r.sent()).error)return[2,{error:n.error}];r.label=3;case 3:return e++,[3,1];case 4:return[2,{value:n.value}]}}))}))},t}(),B=function(t){function n(){var n=t.call(this)||this;return n._length=0,n._max=0,n._min=0,n._type="array",n.push((function(t){if(e(t)){if(n._items){n._items.name="";for(var r=0;r<t.length;r++){var u=n._items.validate(t[r]),i=u.error,o=u.value;if(i)return n.invalid("["+n._type+"]< "+i+"["+r+"] >");t[r]=o}}return{value:t}}return n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.items=function(t){return this._items=t,this},n.prototype.length=function(t){var n=this;return this._length=t,this.push((function(t){return t.length==n._length?{value:t}:n.invalid("["+n._type+"].length("+n._length+")")}))},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t.length<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t.length>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n}(L),D=function(t){function n(){var n=t.call(this)||this;return n._type="boolean",n.push((function(t){return r(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.parse=function(){return this.unshift((function(t){return{value:u(t)?-1!=[1,"1","true"].indexOf(t):t}}))},n}(L),N=function(t){function n(){var n=t.call(this)||this;return n._max=0,n._min=0,n._type="number",n.push((function(t){return i(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n.prototype.parse=function(){return this.unshift((function(t){return{value:u(t)?parseFloat(t):t}}))},n}(L),Y=function(t){function n(){var n=t.call(this)||this;return n._type="object",n.push((function(t){if(o(t)){if(n._keys){var e=[];return Object.keys(n._keys).forEach((function(r){if(n._keys){var i=n._keys[r];i.name=r;var o=i.validate(t[r]),a=o.error,s=o.value;a&&e.push(a),u(s)&&(t[r]=s)}})),e.length?n.invalid("["+n._type+"]{ "+e.join(", ")+" }"):{value:t}}return{value:t}}return n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.keys=function(t){return this._keys=t,this},n}(L),$=function(t){function n(){var n=t.call(this)||this;return n._length=0,n._max=0,n._min=0,n._type="string",n.push((function(t){return a(t)?{value:t}:n.invalid("["+n._type+"]")})),n}return F(n,t),n.prototype.between=function(t,n){return this.min(t),this.max(n)},n.prototype.email=function(){var t=this;return this.push((function(n){return/^[\w-\\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(n)?{value:n}:t.invalid("["+t._type+"].email")}))},n.prototype.length=function(t){var n=this;return this._length=t,this.push((function(t){return t.length==n._length?{value:t}:n.invalid("["+n._type+"].length("+n._length+")")}))},n.prototype.lowerCase=function(){return this.push((function(t){return{value:t.toLowerCase()}}))},n.prototype.lowerCamelCase=function(){return this.push((function(t){return{value:w(t)}}))},n.prototype.max=function(t){var n=this;return this._max=t,this.push((function(t){return t.length<=n._max?{value:t}:n.invalid("["+n._type+"].max("+n._max+")")}))},n.prototype.mask=function(t){return this.push((function(n){return{value:S(t,n)}}))},n.prototype.min=function(t){var n=this;return this._min=t,this.push((function(t){return t.length>=n._min?{value:t}:n.invalid("["+n._type+"].min("+n._min+")")}))},n.prototype.parse=function(){return this.unshift((function(t){return{value:o(t)?JSON.stringify(t):u(t)?t.toString():t}}))},n.prototype.required=function(){var t=this;return this.push((function(n){return n.length?{value:n}:t.invalid("["+t._type+"].required")}))},n.prototype.trim=function(){return this.push((function(t){return{value:t.trim()}}))},n.prototype.upperCase=function(){return this.push((function(t){return{value:t.toUpperCase()}}))},n.prototype.upperCamelCase=function(){return this.push((function(t){return{value:k(t)}}))},n}(L),z=function(){function t(){}return t.array=function(t){return(new B).items(t)},t.boolean=function(){return new D},t.number=function(){return new N},t.object=function(t){return(new Y).keys(t)},t.string=function(){return new $},t.prototype.test=function(n){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return!a(t.validate.apply(t,P([n],e,!1)).error)},t.prototype.testAsync=function(n){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return U(this,void 0,void 0,(function(){var r;return I(this,(function(u){switch(u.label){case 0:return r=a,[4,t.validateAsync.apply(t,P([n],e,!1))];case 1:return[2,!r.apply(void 0,[u.sent().error])]}}))}))},t.validate=function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];for(var r={value:t},u=0;u<n.length;u++)if((r=n[u].validate(r.value)).error)return{error:r.error};return r},t.validateAsync=function(t){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];return U(this,void 0,void 0,(function(){var e,r;return I(this,(function(u){switch(u.label){case 0:e={value:t},r=0,u.label=1;case 1:return r<n.length?[4,n[r].validateAsync(e.value)]:[3,4];case 2:if((e=u.sent()).error)return[2,{error:e.error}];u.label=3;case 3:return r++,[3,1];case 4:return[2,e]}}))}))},t}();t.EventEmitter=d,t.Validation=z,t.ValidationBase=L,t.allowedObject=q,t.camelCase=x,t.chunkArray=function(t,n){for(var e=[],r=0;r<t.length;r+=n)e.push(t.slice(r,r+n));return e},t.clamp=l,t.cutText=function(t,n){if(t.length>n){var e=t.substring(0,n).split(" ");return e.pop(),e.join(" ")+"..."}return t},t.dateFormat=function(t,n){return n.replace(/%[yYmdHMS]/g,(function(n){var e;switch(n){case"%y":e=t.getFullYear();break;case"%Y":return t.getFullYear().toString();case"%m":e=t.getMonth()+1;break;case"%d":e=t.getDate();break;case"%H":e=t.getHours();break;case"%M":e=t.getMinutes();break;case"%S":e=t.getSeconds();break;default:return n.slice(1)}return("0"+e.toString()).slice(-2)}))},t.debounce=function(t,n){var e;return function(){for(var r=[],u=0;u<arguments.length;u++)r[u]=arguments[u];e&&clearTimeout(e),e=setTimeout((function(){return t.apply(void 0,r)}),n)}},t.deniedObject=function(t,n){return q(Object.keys(n).filter((function(n){return-1==t.indexOf(n)})),n)},t.each=function(t,n,r){return e(t)?g(t,n,r):o(t)?_(t,n,r):null},t.forEachAsync=function(t,n,e){return U(void 0,void 0,void 0,(function(){return I(this,(function(r){switch(r.label){case 0:return[4,Promise.all(t.map((function(r,u){return n.call(e,r,u,t)})))];case 1:return r.sent(),[2]}}))}))},t.forEachIndex=g,t.forEachKey=_,t.forEachSeries=function(t,n,e){return U(void 0,void 0,void 0,(function(){var r;return I(this,(function(u){switch(u.label){case 0:r=0,u.label=1;case 1:return r<t.length?[4,n.call(e,t[r],r,t)]:[3,4];case 2:u.sent(),u.label=3;case 3:return r++,[3,1];case 4:return[2]}}))}))},t.isArray=e,t.isBoolean=r,t.isDef=u,t.isFloat=function(t){return i(t)&&!!(t%1)},t.isFunction=function(t){return"function"==c(t)},t.isInteger=function(t){return i(t)&&!(t%1)},t.isNull=function(t){return null===t},t.isNumber=i,t.isObject=o,t.isString=a,t.isUndefined=s,t.loop=function(t,n,e){for(var r=1;r<=t&&!1!==n.call(e,r,t);r++);return t},t.lowerCamelCase=w,t.map=function(t,n,e){for(var r="",u=Object.keys(t),i=0;i<u.length;i++)t[r=u[i]]=n.call(e,t[r],r,t);return t},t.mask=S,t.maskMoney=function(t,n,e){return e++,n=parseInt((n||"0").replace(/\W/g,"")).toString(),E(t,n.length<e?("0".repeat(e)+n).slice(-1*e):n)},t.maskReverse=E,t.noop=function(){},t.parseSize=function(t){if(i(t))return t+"px";var n=function(t){return/(\d+)\.*(\d*)(px|%|cm|mm|in|pt|pc|en|ex|ch|rem|vw|vh|vmin|vmax)*/g.exec(t)}(t)||["","0","",""],e=n[1],r=n[2],u=n[3];return(r.length?parseFloat(e+"."+r):parseInt(e))+(u||"px")},t.shadeColor=function(t,n){return"#"+t.replace(/^#/,"").replace(/../g,(function(t){return("0"+f(t,n)).slice(-2)}))},t.simpleID=function(){return h()+v()},t.sleep=function(t){return new Promise((function(n){return setTimeout(n,t)}))},t.stringReverse=j,t.toArray=function(t){return e(t)?t:u(t)?[t]:[]},t.toBool=function(t){return"true"===t||"1"===t},t.type=c,t.uniqueID=function(){return m()+m()+m()+m()},t.upperCamelCase=k,t.uuidv4=function(){return y()+"-"+v()+"-4"+h()+"-"+v()+"-"+(y()+v())}}));
|