@zlikemario/helper 0.0.10 → 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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./utils.cjs");function v(c=0,l=(...u)=>u[0],f=!1){return function(u,h,o){const g=o.value,r=o.get,t=new Map;let a=Date.now();const m=Symbol(String(h)),y=()=>c>0&&a<Date.now(),d=(...n)=>(typeof l=="function"?l(...n):l)??m;return typeof g=="function"&&(o.value=function(...n){const e=d(...n);if(!t.get(e)||y()){a=Date.now()+c;const i=g.apply(this,n);i instanceof Promise?(t.set(e,i),i.then(p=>{!f&&s.isUndefined(p)&&t.delete(e)})):(f||!s.isUndefined(i))&&t.set(e,i)}return t.get(e)}),typeof r=="function"&&(o.get=function(){const n=d();if(!t.get(n)||y()){a=Date.now()+c;const e=r.apply(this);e instanceof Promise?(t.set(n,e),e.then(i=>{!f&&s.isUndefined(i)&&t.delete(n)})):(f||!s.isUndefined(e))&&t.set(n,e)}return t.get(n)}),o}}exports.Memoize=v;
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;
@@ -1,32 +1,28 @@
1
- import { isUndefined as c } from "./utils.js";
2
- function v(s = 0, a = (...u) => u[0], i = !1) {
3
- return function(u, p, f) {
4
- const g = f.value, y = f.get, t = /* @__PURE__ */ new Map();
5
- let l = Date.now();
6
- const r = Symbol(String(p)), h = () => s > 0 && l < Date.now(), m = (...n) => (typeof a == "function" ? a(...n) : a) ?? r;
7
- return typeof g == "function" && (f.value = function(...n) {
8
- const e = m(...n);
9
- if (!t.get(e) || h()) {
10
- l = Date.now() + s;
11
- const o = g.apply(this, n);
12
- o instanceof Promise ? (t.set(e, o), o.then((w) => {
13
- !i && c(w) && t.delete(e);
14
- })) : (i || !c(o)) && t.set(e, o);
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 t.get(e);
17
- }), typeof y == "function" && (f.get = function() {
18
- const n = m();
19
- if (!t.get(n) || h()) {
20
- l = Date.now() + s;
21
- const e = y.apply(this);
22
- e instanceof Promise ? (t.set(n, e), e.then((o) => {
23
- !i && c(o) && t.delete(n);
24
- })) : (i || !c(e)) && t.set(n, e);
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 });
25
21
  }
26
- return t.get(n);
27
- }), f;
22
+ return e.get(t)?.value;
23
+ }), c;
28
24
  };
29
25
  }
30
26
  export {
31
- v as Memoize
27
+ T as Memoize
32
28
  };
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("./utils.cjs");function p(c=0,i=(...o)=>o[0],s=!1){return function(o,l){const e=new Map;let f=Date.now();const d=Symbol(String(l.name)),u=()=>c>0&&f<Date.now();function m(...r){const t=(typeof i=="function"?i(...r):i)??d;if(!e.get(t)||u()){f=Date.now()+c;const n=o.apply(this,r);n instanceof Promise?(e.set(t,n),n.then(h=>{!s&&a.isUndefined(h)&&e.delete(t)})):(s||!a.isUndefined(n))&&e.set(t,n)}return e.get(t)}return m}}exports.Memoize=p;
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 r } from "./utils.js";
2
- function w(c = 0, o = (...i) => i[0], f = !1) {
3
- return function(i, l) {
4
- const e = /* @__PURE__ */ new Map();
5
- let s = Date.now();
6
- const d = Symbol(String(l.name)), m = () => c > 0 && s < Date.now();
7
- function p(...a) {
8
- const t = (typeof o == "function" ? o(...a) : o) ?? d;
9
- if (!e.get(t) || m()) {
10
- s = Date.now() + c;
11
- const n = i.apply(this, a);
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 e.get(t);
13
+ return n.get(t)?.value;
17
14
  }
18
- return p;
15
+ return M;
19
16
  };
20
17
  }
21
18
  export {
22
- w as Memoize
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>) => string;
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",duration: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;
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
- * Utility functions for common operations.
2
+ * 常用操作的工具函数集合
3
3
  */
4
4
  /**
5
- * Sleep for a specified interval (ms).
6
- * @param interval - Time in milliseconds to sleep. Default: 1500ms.
5
+ * 延迟指定的时间间隔(毫秒)
6
+ * @param interval - 延迟的毫秒数,默认 1500ms
7
7
  */
8
8
  export declare const sleep: (interval?: number) => Promise<void>;
9
9
  /**
10
- * Encrypts a string by masking the middle part.
11
- * @param text - The input string.
12
- * @param prefix - Number of characters to keep at the start.
13
- * @param suffix - Number of characters to keep at the end.
14
- * @param placeholder - Masking string. Default: '...'.
15
- * @returns Masked string.
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
- * Checks if a value is undefined.
20
- * @param v - Value to check.
19
+ * 检查值是否为 undefined
20
+ * @param v - 要检查的值
21
21
  */
22
22
  export declare const isUndefined: (v: unknown) => v is undefined;
23
23
  /**
24
- * Checks if any of the provided values are undefined.
25
- * @param vs - Values to check.
24
+ * 检查提供的值中是否有任何一个为 undefined
25
+ * @param vs - 要检查的值
26
26
  */
27
27
  export declare const isHasUndefined: (...vs: unknown[]) => boolean;
28
28
  /**
29
- * Checks if a value is a Promise.
30
- * @param v - Value to check.
29
+ * 检查值是否为 Promise
30
+ * @param v - 要检查的值
31
31
  */
32
32
  export declare const isPromise: <T = any>(v: unknown) => v is Promise<T>;
33
33
  /**
34
- * Converts a Uint8Array to a Base64 string.
35
- * @param array - Uint8Array to convert.
34
+ * Uint8Array 转换为 Base64 字符串
35
+ * @param array - 要转换的 Uint8Array
36
36
  */
37
37
  export declare function uint8ArrayToBase64(array: Uint8Array): string;
38
38
  /**
39
- * Converts a Base64 string to a Uint8Array.
40
- * @param base64 - Base64 string to convert.
39
+ * Base64 字符串转换为 Uint8Array
40
+ * @param base64 - 要转换的 Base64 字符串
41
41
  */
42
42
  export declare function base64ToUint8Array(base64: string): Uint8Array;
43
43
  /**
44
- * Converts an ArrayBuffer to a Base64 string.
45
- * @param buffer - ArrayBuffer to convert.
44
+ * ArrayBuffer 转换为 Base64 字符串
45
+ * @param buffer - 要转换的 ArrayBuffer
46
46
  */
47
47
  export declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
48
48
  /**
49
- * Validates a domain name.
50
- * Rules:
51
- * 1. Only numbers, letters, and hyphens allowed.
52
- * 2. Composed of one or more parts separated by dots.
53
- * 3. Last part must be at least two characters.
54
- * 4. Cannot start or end with a hyphen.
55
- * 5. No consecutive hyphens or hyphen-dot/dot-hyphen.
56
- * @param text - Domain string to validate.
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
- * Validates an email address.
61
- * Rules:
62
- * 1. Account part: letters, numbers, _, -, . allowed.
63
- * 2. No leading/trailing dot.
64
- * 3. No consecutive special characters.
65
- * 4. Special character must be followed by at least one letter/number.
66
- * 5. Domain part must be valid.
67
- * @param text - Email string to validate.
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
- * Async try-catch wrapper.
72
- * @param p - Promise to execute.
73
- * @param catchFn - Optional error handler.
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
- * Executes a callback or promise and ensures it completes within a specified timeout duration.
78
- * If the operation does not finish in time, the returned promise is rejected with a timeout error.
77
+ * 执行回调函数或 Promise,并确保在指定的超时时间内完成
78
+ * 如果操作未能及时完成,返回的 Promise 将以超时错误被拒绝
79
79
  *
80
- * @template R The type of the resolved value.
81
- * @param callback - A function returning a promise or a promise to execute.
82
- * @param options - Optional settings for timeout behavior.
83
- * @param options.errorMessage - Custom error message for timeout rejection. Defaults to "Timeout".
84
- * @param options.duration - Timeout duration in milliseconds. Defaults to 15000 ms.
85
- * @returns A promise that resolves with the result of the callback or rejects if the timeout is reached.
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
- duration?: number;
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 u(e) {
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 u(new Uint8Array(e));
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", duration: t = 15e3 } = n || {}, o = typeof e == "function" ? e() : e, i = new Promise((c, a) => setTimeout(() => a(new Error(r)), t));
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
- u as uint8ArrayToBase64
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.10",
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
+ }