@zlikemario/helper 0.0.9 → 0.0.11
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/decorator-old.cjs +1 -1
- package/dist/decorator-old.d.ts +1 -1
- package/dist/decorator-old.js +23 -17
- package/dist/decorator.cjs +1 -1
- package/dist/decorator.js +14 -17
- package/dist/number.d.ts +1 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.ts +49 -49
- package/dist/utils.js +4 -4
- package/package.json +2 -2
package/dist/decorator-old.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("./utils.cjs");function x(l=0,u=(...h)=>h[0],a=!1){return function(h,y,o){const g=o.value,v=o.get,r=new WeakMap,M=Symbol(String(y)),d=e=>(r.has(e)||r.set(e,new Map),r.get(e)),p=e=>l>0&&e<Date.now(),m=(...e)=>(typeof u=="function"?u(...e):u)??M;return typeof g=="function"&&(o.value=function(...e){const t=d(this),n=m(...e),c=t.get(n);if(!c||p(c.expireTime)){const i=Date.now()+l,s=g.apply(this,e);s instanceof Promise?(t.set(n,{value:s,expireTime:i}),s.then(w=>{!a&&f.isUndefined(w)&&t.delete(n)})):(a||!f.isUndefined(s))&&t.set(n,{value:s,expireTime:i})}return t.get(n)?.value}),typeof v=="function"&&(o.get=function(){const e=d(this),t=m(),n=e.get(t);if(!n||p(n.expireTime)){const c=Date.now()+l,i=v.apply(this);i instanceof Promise?(e.set(t,{value:i,expireTime:c}),i.then(s=>{!a&&f.isUndefined(s)&&e.delete(t)})):(a||!f.isUndefined(i))&&e.set(t,{value:i,expireTime:c})}return e.get(t)?.value}),o}}exports.Memoize=x;
|
package/dist/decorator-old.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function Memoize<Args extends any[]>(duration?: number, computeKey?: ((...args: Args) => PropertyKey) | PropertyKey, isCacheVoid?: boolean):
|
|
1
|
+
export declare function Memoize<Args extends any[]>(duration?: number, computeKey?: ((...args: Args) => PropertyKey) | PropertyKey, isCacheVoid?: boolean): (_: any, name: string | symbol, descriptor: TypedPropertyDescriptor<any>) => TypedPropertyDescriptor<any>;
|
package/dist/decorator-old.js
CHANGED
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
import { isUndefined as
|
|
2
|
-
function
|
|
3
|
-
return function(
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
t instanceof Promise ? (e.set(n, t), t.then((w) => {
|
|
13
|
-
!a && r(w) && e.delete(n);
|
|
14
|
-
})) : (a || !r(t)) && e.set(n, t);
|
|
1
|
+
import { isUndefined as f } from "./utils.js";
|
|
2
|
+
function T(l = 0, u = (...r) => r[0], a = !1) {
|
|
3
|
+
return function(r, x, c) {
|
|
4
|
+
const p = c.value, g = c.get, h = /* @__PURE__ */ new WeakMap(), w = Symbol(String(x)), v = (e) => (h.has(e) || h.set(e, /* @__PURE__ */ new Map()), h.get(e)), m = (e) => l > 0 && e < Date.now(), y = (...e) => (typeof u == "function" ? u(...e) : u) ?? w;
|
|
5
|
+
return typeof p == "function" && (c.value = function(...e) {
|
|
6
|
+
const t = v(this), n = y(...e), s = t.get(n);
|
|
7
|
+
if (!s || m(s.expireTime)) {
|
|
8
|
+
const i = Date.now() + l, o = p.apply(this, e);
|
|
9
|
+
o instanceof Promise ? (t.set(n, { value: o, expireTime: i }), o.then((D) => {
|
|
10
|
+
!a && f(D) && t.delete(n);
|
|
11
|
+
})) : (a || !f(o)) && t.set(n, { value: o, expireTime: i });
|
|
15
12
|
}
|
|
16
|
-
return
|
|
17
|
-
},
|
|
13
|
+
return t.get(n)?.value;
|
|
14
|
+
}), typeof g == "function" && (c.get = function() {
|
|
15
|
+
const e = v(this), t = y(), n = e.get(t);
|
|
16
|
+
if (!n || m(n.expireTime)) {
|
|
17
|
+
const s = Date.now() + l, i = g.apply(this);
|
|
18
|
+
i instanceof Promise ? (e.set(t, { value: i, expireTime: s }), i.then((o) => {
|
|
19
|
+
!a && f(o) && e.delete(t);
|
|
20
|
+
})) : (a || !f(i)) && e.set(t, { value: i, expireTime: s });
|
|
21
|
+
}
|
|
22
|
+
return e.get(t)?.value;
|
|
23
|
+
}), c;
|
|
18
24
|
};
|
|
19
25
|
}
|
|
20
26
|
export {
|
|
21
|
-
|
|
27
|
+
T as Memoize
|
|
22
28
|
};
|
package/dist/decorator.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("./utils.cjs");function v(o=0,s=(...c)=>c[0],r=!1){return function(c,f){const h=Symbol(String(f.name)),p=e=>o>0&&e<Date.now(),m=new Map,a=new WeakMap,M=e=>(a.has(e)||a.set(e,new Map),a.get(e));function g(...e){const t=(typeof s=="function"?s(...e):s)??h,n=f.static?m:M(this),l=n.get(t);if(!l||p(l.expireTime)){const u=Date.now()+o,i=c.apply(this,e);i instanceof Promise?(n.set(t,{value:i,expireTime:u}),i.then(S=>{!r&&d.isUndefined(S)&&n.delete(t)})):(r||!d.isUndefined(i))&&n.set(t,{value:i,expireTime:u})}return n.get(t)?.value}return g}}exports.Memoize=v;
|
package/dist/decorator.js
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
|
-
import { isUndefined as
|
|
2
|
-
function
|
|
3
|
-
return function(i,
|
|
4
|
-
const e = /* @__PURE__ */ new Map();
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
n instanceof Promise ? (e.set(t, n), n.then((h) => {
|
|
13
|
-
!f && r(h) && e.delete(t);
|
|
14
|
-
})) : (f || !r(n)) && e.set(t, n);
|
|
1
|
+
import { isUndefined as l } from "./utils.js";
|
|
2
|
+
function S(o = 0, c = (...i) => i[0], r = !1) {
|
|
3
|
+
return function(i, f) {
|
|
4
|
+
const u = Symbol(String(f.name)), d = (e) => o > 0 && e < Date.now(), m = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new WeakMap(), w = (e) => (s.has(e) || s.set(e, /* @__PURE__ */ new Map()), s.get(e));
|
|
5
|
+
function M(...e) {
|
|
6
|
+
const t = (typeof c == "function" ? c(...e) : c) ?? u, n = f.static ? m : w(this), h = n.get(t);
|
|
7
|
+
if (!h || d(h.expireTime)) {
|
|
8
|
+
const p = Date.now() + o, a = i.apply(this, e);
|
|
9
|
+
a instanceof Promise ? (n.set(t, { value: a, expireTime: p }), a.then((g) => {
|
|
10
|
+
!r && l(g) && n.delete(t);
|
|
11
|
+
})) : (r || !l(a)) && n.set(t, { value: a, expireTime: p });
|
|
15
12
|
}
|
|
16
|
-
return
|
|
13
|
+
return n.get(t)?.value;
|
|
17
14
|
}
|
|
18
|
-
return
|
|
15
|
+
return M;
|
|
19
16
|
};
|
|
20
17
|
}
|
|
21
18
|
export {
|
|
22
|
-
|
|
19
|
+
S as Memoize
|
|
23
20
|
};
|
package/dist/number.d.ts
CHANGED
|
@@ -52,7 +52,7 @@ export declare const readableNumber: (number: Numberish, decimals?: number) => s
|
|
|
52
52
|
* @param data - 数字数组
|
|
53
53
|
* @returns 求和结果字符串
|
|
54
54
|
*/
|
|
55
|
-
export declare const sum: (data: Array<Numberish>) =>
|
|
55
|
+
export declare const sum: (data: Array<Numberish>) => NumberString;
|
|
56
56
|
/**
|
|
57
57
|
* 按 key 或函数求和
|
|
58
58
|
* @param data - 对象数组
|
package/dist/utils.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=(e=1500)=>new Promise(n=>setTimeout(n,e));function l(e,n=4,r=4,t="..."){return!e||e.length<=n+r?e:e.slice(0,n)+t+(r?e.slice(-r):"")}const i=e=>e===void 0,m=(...e)=>e.some(i),y=e=>!!e&&typeof e?.then=="function"&&typeof e?.catch=="function";function s(e){let n="";for(let r=0;r<e.length;r++)n+=String.fromCharCode(e[r]);return btoa(n)}function d(e){const n=atob(e),r=new Uint8Array(n.length);for(let t=0;t<n.length;t++)r[t]=n.charCodeAt(t);return r}function h(e){return s(new Uint8Array(e))}function o(e){if(typeof e!="string"||(e=e.toLowerCase(),!/^[0-9a-z.\-]+$/.test(e))||e.includes("--")||e.includes("-.")||e.includes(".-")||e.startsWith("-")||e.endsWith("-"))return!1;const n=e.split(".");return!(n.length<2||n.some(r=>!r.length)||n[n.length-1].length<2)}function g(e){const[n,...r]=e.toLowerCase().split("@");if(!n||!r.length||!/^[a-z0-9._-]+$/.test(n)||/^\.|\.$/.test(n)||/[.\-_]{2,}/.test(n)||/[.\-_](?![a-z0-9])/.test(n))return!1;for(const t of r)if(!o(t))return!1;return!0}const p=async(e,n)=>{try{return await e}catch(r){return n?.(r)}};async function T(e,n){const{errorMessage:r="Timeout",
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=(e=1500)=>new Promise(n=>setTimeout(n,e));function l(e,n=4,r=4,t="..."){return!e||e.length<=n+r?e:e.slice(0,n)+t+(r?e.slice(-r):"")}const i=e=>e===void 0,m=(...e)=>e.some(i),y=e=>!!e&&typeof e?.then=="function"&&typeof e?.catch=="function";function s(e){let n="";for(let r=0;r<e.length;r++)n+=String.fromCharCode(e[r]);return btoa(n)}function d(e){const n=atob(e),r=new Uint8Array(n.length);for(let t=0;t<n.length;t++)r[t]=n.charCodeAt(t);return r}function h(e){return s(new Uint8Array(e))}function o(e){if(typeof e!="string"||(e=e.toLowerCase(),!/^[0-9a-z.\-]+$/.test(e))||e.includes("--")||e.includes("-.")||e.includes(".-")||e.startsWith("-")||e.endsWith("-"))return!1;const n=e.split(".");return!(n.length<2||n.some(r=>!r.length)||n[n.length-1].length<2)}function g(e){const[n,...r]=e.toLowerCase().split("@");if(!n||!r.length||!/^[a-z0-9._-]+$/.test(n)||/^\.|\.$/.test(n)||/[.\-_]{2,}/.test(n)||/[.\-_](?![a-z0-9])/.test(n))return!1;for(const t of r)if(!o(t))return!1;return!0}const p=async(e,n)=>{try{return await e}catch(r){return n?.(r)}};async function T(e,n){const{errorMessage:r="Timeout",timeout:t=15e3}=n||{},a=typeof e=="function"?e():e,u=new Promise((A,f)=>setTimeout(()=>f(new Error(r)),t));return Promise.race([a,u])}const P=(e,n,r)=>e.some(i)?r:n(...e);exports.arrayBufferToBase64=h;exports.base64ToUint8Array=d;exports.computeWithDefinedParams=P;exports.encrypt=l;exports.isDomain=o;exports.isEmail=g;exports.isHasUndefined=m;exports.isPromise=y;exports.isUndefined=i;exports.preventTimeout=T;exports.sleep=c;exports.tryCatchAsync=p;exports.uint8ArrayToBase64=s;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* 常用操作的工具函数集合
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param interval -
|
|
5
|
+
* 延迟指定的时间间隔(毫秒)
|
|
6
|
+
* @param interval - 延迟的毫秒数,默认 1500ms
|
|
7
7
|
*/
|
|
8
8
|
export declare const sleep: (interval?: number) => Promise<void>;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param text -
|
|
12
|
-
* @param prefix -
|
|
13
|
-
* @param suffix -
|
|
14
|
-
* @param placeholder -
|
|
15
|
-
* @returns
|
|
10
|
+
* 通过遮罩中间部分来加密字符串
|
|
11
|
+
* @param text - 输入字符串
|
|
12
|
+
* @param prefix - 开头保留的字符数
|
|
13
|
+
* @param suffix - 结尾保留的字符数
|
|
14
|
+
* @param placeholder - 遮罩字符串,默认 '...'
|
|
15
|
+
* @returns 遮罩后的字符串
|
|
16
16
|
*/
|
|
17
17
|
export declare function encrypt(text: string, prefix?: number, suffix?: number, placeholder?: string): string;
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
20
|
-
* @param v -
|
|
19
|
+
* 检查值是否为 undefined
|
|
20
|
+
* @param v - 要检查的值
|
|
21
21
|
*/
|
|
22
22
|
export declare const isUndefined: (v: unknown) => v is undefined;
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
25
|
-
* @param vs -
|
|
24
|
+
* 检查提供的值中是否有任何一个为 undefined
|
|
25
|
+
* @param vs - 要检查的值
|
|
26
26
|
*/
|
|
27
27
|
export declare const isHasUndefined: (...vs: unknown[]) => boolean;
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @param v -
|
|
29
|
+
* 检查值是否为 Promise
|
|
30
|
+
* @param v - 要检查的值
|
|
31
31
|
*/
|
|
32
32
|
export declare const isPromise: <T = any>(v: unknown) => v is Promise<T>;
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
* @param array - Uint8Array
|
|
34
|
+
* 将 Uint8Array 转换为 Base64 字符串
|
|
35
|
+
* @param array - 要转换的 Uint8Array
|
|
36
36
|
*/
|
|
37
37
|
export declare function uint8ArrayToBase64(array: Uint8Array): string;
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
40
|
-
* @param base64 - Base64
|
|
39
|
+
* 将 Base64 字符串转换为 Uint8Array
|
|
40
|
+
* @param base64 - 要转换的 Base64 字符串
|
|
41
41
|
*/
|
|
42
42
|
export declare function base64ToUint8Array(base64: string): Uint8Array;
|
|
43
43
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @param buffer - ArrayBuffer
|
|
44
|
+
* 将 ArrayBuffer 转换为 Base64 字符串
|
|
45
|
+
* @param buffer - 要转换的 ArrayBuffer
|
|
46
46
|
*/
|
|
47
47
|
export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* 1.
|
|
52
|
-
* 2.
|
|
53
|
-
* 3.
|
|
54
|
-
* 4.
|
|
55
|
-
* 5.
|
|
56
|
-
* @param text -
|
|
49
|
+
* 验证域名
|
|
50
|
+
* 规则:
|
|
51
|
+
* 1. 只允许数字、字母和连字符
|
|
52
|
+
* 2. 由一个或多个用点分隔的部分组成
|
|
53
|
+
* 3. 最后一部分必须至少有两个字符
|
|
54
|
+
* 4. 不能以连字符开头或结尾
|
|
55
|
+
* 5. 不能有连续的连字符或连字符-点/点-连字符
|
|
56
|
+
* @param text - 要验证的域名字符串
|
|
57
57
|
*/
|
|
58
58
|
export declare function isDomain(text: string): boolean;
|
|
59
59
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* 1.
|
|
63
|
-
* 2.
|
|
64
|
-
* 3.
|
|
65
|
-
* 4.
|
|
66
|
-
* 5.
|
|
67
|
-
* @param text -
|
|
60
|
+
* 验证电子邮件地址
|
|
61
|
+
* 规则:
|
|
62
|
+
* 1. 账户部分:允许字母、数字、_、-、.
|
|
63
|
+
* 2. 不能以点开头或结尾
|
|
64
|
+
* 3. 不能有连续的特殊字符
|
|
65
|
+
* 4. 特殊字符后必须至少跟一个字母或数字
|
|
66
|
+
* 5. 域名部分必须有效
|
|
67
|
+
* @param text - 要验证的电子邮件字符串
|
|
68
68
|
*/
|
|
69
69
|
export declare function isEmail(text: string): boolean;
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
72
|
-
* @param p - Promise
|
|
73
|
-
* @param catchFn -
|
|
71
|
+
* 异步 try-catch 包装器
|
|
72
|
+
* @param p - 要执行的 Promise
|
|
73
|
+
* @param catchFn - 可选的错误处理函数
|
|
74
74
|
*/
|
|
75
75
|
export declare const tryCatchAsync: <T, F extends ((error: unknown) => any) | undefined = undefined>(p: Promise<T>, catchFn?: F) => Promise<T | (F extends (...args: any) => any ? ReturnType<F> : undefined)>;
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
77
|
+
* 执行回调函数或 Promise,并确保在指定的超时时间内完成
|
|
78
|
+
* 如果操作未能及时完成,返回的 Promise 将以超时错误被拒绝
|
|
79
79
|
*
|
|
80
|
-
* @template R
|
|
81
|
-
* @param callback -
|
|
82
|
-
* @param options -
|
|
83
|
-
* @param options.errorMessage -
|
|
84
|
-
* @param options.duration -
|
|
85
|
-
* @returns
|
|
80
|
+
* @template R 解析值的类型
|
|
81
|
+
* @param callback - 返回 Promise 的函数或要执行的 Promise
|
|
82
|
+
* @param options - 超时行为的可选设置
|
|
83
|
+
* @param options.errorMessage - 超时拒绝的自定义错误消息,默认为 "Timeout"
|
|
84
|
+
* @param options.duration - 超时时间(毫秒),默认为 15000 ms
|
|
85
|
+
* @returns 一个 Promise,解析为回调的结果或在达到超时时拒绝
|
|
86
86
|
*/
|
|
87
87
|
export declare function preventTimeout<R>(callback: (() => Promise<R>) | Promise<R>, options?: {
|
|
88
88
|
errorMessage?: string;
|
|
89
|
-
|
|
89
|
+
timeout?: number;
|
|
90
90
|
}): Promise<R>;
|
|
91
91
|
export type ParamsWithMaybeUndefined<T extends readonly unknown[]> = {
|
|
92
92
|
[K in keyof T]: T[K] | undefined;
|
package/dist/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ function m(e, n = 4, r = 4, t = "...") {
|
|
|
3
3
|
return !e || e.length <= n + r ? e : e.slice(0, n) + t + (r ? e.slice(-r) : "");
|
|
4
4
|
}
|
|
5
5
|
const s = (e) => e === void 0, h = (...e) => e.some(s), y = (e) => !!e && typeof e?.then == "function" && typeof e?.catch == "function";
|
|
6
|
-
function
|
|
6
|
+
function a(e) {
|
|
7
7
|
let n = "";
|
|
8
8
|
for (let r = 0; r < e.length; r++)
|
|
9
9
|
n += String.fromCharCode(e[r]);
|
|
@@ -16,7 +16,7 @@ function d(e) {
|
|
|
16
16
|
return r;
|
|
17
17
|
}
|
|
18
18
|
function g(e) {
|
|
19
|
-
return
|
|
19
|
+
return a(new Uint8Array(e));
|
|
20
20
|
}
|
|
21
21
|
function f(e) {
|
|
22
22
|
if (typeof e != "string" || (e = e.toLowerCase(), !/^[0-9a-z.\-]+$/.test(e)) || e.includes("--") || e.includes("-.") || e.includes(".-") || e.startsWith("-") || e.endsWith("-"))
|
|
@@ -39,7 +39,7 @@ const w = async (e, n) => {
|
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
41
|
async function P(e, n) {
|
|
42
|
-
const { errorMessage: r = "Timeout",
|
|
42
|
+
const { errorMessage: r = "Timeout", timeout: t = 15e3 } = n || {}, o = typeof e == "function" ? e() : e, i = new Promise((c, u) => setTimeout(() => u(new Error(r)), t));
|
|
43
43
|
return Promise.race([o, i]);
|
|
44
44
|
}
|
|
45
45
|
const T = (e, n, r) => e.some(s) ? r : n(...e);
|
|
@@ -56,5 +56,5 @@ export {
|
|
|
56
56
|
P as preventTimeout,
|
|
57
57
|
l as sleep,
|
|
58
58
|
w as tryCatchAsync,
|
|
59
|
-
|
|
59
|
+
a as uint8ArrayToBase64
|
|
60
60
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zlikemario/helper",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "A utility library with number operations and common helper functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"utility",
|
|
@@ -75,4 +75,4 @@
|
|
|
75
75
|
"bignumber.js": "*",
|
|
76
76
|
"dayjs": "*"
|
|
77
77
|
}
|
|
78
|
-
}
|
|
78
|
+
}
|