@zlikemario/helper 0.0.11 → 0.0.12

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.
@@ -0,0 +1,3 @@
1
+ export { default as useCatchError } from "./useCatchError";
2
+ export { default as useLoadingEvent } from "./useLoadingEvent";
3
+ export { default as useSyncedRef } from "./useSyncedRef";
@@ -0,0 +1,8 @@
1
+ declare const useCatchError: <Args extends any[], R, IsThrowError extends boolean = false>(execute: (...args: Args) => MaybePromise<R>, options?: Partial<{
2
+ target: string;
3
+ timeout: number;
4
+ timeoutError: any;
5
+ isThrowError: IsThrowError;
6
+ onError: (error: unknown, target: MaybeUndefined<string>, ...args: Args) => any;
7
+ }>) => (...args: Args) => Promise<R | (IsThrowError extends true ? never : undefined)>;
8
+ export default useCatchError;
@@ -0,0 +1,4 @@
1
+ declare const useLoadingEvent: <Args extends any[], R extends any>(execute: (...args: Args) => Promise<R>, options?: Partial<{
2
+ isAllowMulticall: boolean;
3
+ }>) => [(...args: Args) => Promise<R>, boolean];
4
+ export default useLoadingEvent;
@@ -0,0 +1,4 @@
1
+ declare const useSyncedRef: <T>(value: T) => Readonly<{
2
+ readonly current: T;
3
+ }>;
4
+ export default useSyncedRef;
package/dist/react.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("react"),m=require("./utils.cjs"),a=n=>{const e=o.useRef(n);return e.current=n,o.useMemo(()=>Object.freeze({get current(){return e.current}}),[])},f=(n,e)=>{const u=e?.timeout??2e4,r=a({options:{timeout:u,timeoutError:e?.timeoutError?e?.timeoutError:new Error(`${e?.target?`${e?.target} `:""}Timeout of ${u}ms exceeded`),isThrowError:e?.isThrowError??!1,...e},execute:n});return o.useCallback(async(...s)=>{try{return await m.preventTimeout(Promise.resolve(r.current.execute(...s)),{timeout:r.current.options.timeout,timeoutError:r.current.options.timeoutError})}catch(t){if(r.current.options.onError?.(t,r.current.options.target,...s),r.current.options.isThrowError)throw t;return}},[])},E=(n,e)=>{const[u,r]=o.useState(!1),s=o.useRef(0),t=o.useRef(void 0),c=a({options:{isAllowMulticall:!1,...e},execute:n,isLoading:u}),l=i=>{c.current.isLoading=i,r(i)};return[o.useCallback(async(...i)=>{if(c.current.isLoading&&t.current&&!c.current.options.isAllowMulticall)return t.current;l(!0);const d=s.current+=1;return t.current=c.current.execute(...i).finally(()=>{d===s.current&&(t.current=void 0,l(!1))}),t.current},[]),u]};exports.useCatchError=f;exports.useLoadingEvent=E;exports.useSyncedRef=a;
package/dist/react.js ADDED
@@ -0,0 +1,67 @@
1
+ import { useRef as i, useMemo as f, useCallback as m, useState as E } from "react";
2
+ import { preventTimeout as g } from "./utils.js";
3
+ const l = (o) => {
4
+ const r = i(o);
5
+ return r.current = o, f(
6
+ () => Object.freeze({
7
+ get current() {
8
+ return r.current;
9
+ }
10
+ }),
11
+ []
12
+ );
13
+ }, v = (o, r) => {
14
+ const n = r?.timeout ?? 2e4, e = l({
15
+ options: {
16
+ timeout: n,
17
+ timeoutError: r?.timeoutError ? r?.timeoutError : new Error(`${r?.target ? `${r?.target} ` : ""}Timeout of ${n}ms exceeded`),
18
+ isThrowError: r?.isThrowError ?? !1,
19
+ ...r
20
+ },
21
+ execute: o
22
+ });
23
+ return m(async (...u) => {
24
+ try {
25
+ return await g(Promise.resolve(e.current.execute(...u)), {
26
+ timeout: e.current.options.timeout,
27
+ timeoutError: e.current.options.timeoutError
28
+ });
29
+ } catch (t) {
30
+ if (e.current.options.onError?.(t, e.current.options.target, ...u), e.current.options.isThrowError)
31
+ throw t;
32
+ return;
33
+ }
34
+ }, []);
35
+ }, p = (o, r) => {
36
+ const [n, e] = E(!1), u = i(0), t = i(void 0), s = l({
37
+ options: {
38
+ /**
39
+ * 是否允许函数多次调用,默认: false
40
+ * false 情况下,如果再次执行 execute 会引用上一次的 promise 直接返回
41
+ */
42
+ isAllowMulticall: !1,
43
+ ...r
44
+ },
45
+ execute: o,
46
+ isLoading: n
47
+ }), a = (c) => {
48
+ s.current.isLoading = c, e(c);
49
+ };
50
+ return [
51
+ m(async (...c) => {
52
+ if (s.current.isLoading && t.current && !s.current.options.isAllowMulticall)
53
+ return t.current;
54
+ a(!0);
55
+ const d = u.current += 1;
56
+ return t.current = s.current.execute(...c).finally(() => {
57
+ d === u.current && (t.current = void 0, a(!1));
58
+ }), t.current;
59
+ }, []),
60
+ n
61
+ ];
62
+ };
63
+ export {
64
+ v as useCatchError,
65
+ p as useLoadingEvent,
66
+ l as useSyncedRef
67
+ };
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",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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=(e=1500)=>new Promise(n=>setTimeout(n,e));function m(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,y=(...e)=>e.some(i),d=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 h(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 g(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 p(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 T=async(e,n)=>{try{return await e}catch(r){return n?.(r)}};async function P(e,n){const{errorMessage:r="Timeout",timeoutError:t,timeout:a=15e3}=n||{},u=typeof e=="function"?e():e,f=new Promise((w,c)=>setTimeout(()=>c(t||new Error(r)),a));return Promise.race([u,f])}const A=(e,n,r)=>e.some(i)?r:n(...e);exports.arrayBufferToBase64=g;exports.base64ToUint8Array=h;exports.computeWithDefinedParams=A;exports.encrypt=m;exports.isDomain=o;exports.isEmail=p;exports.isHasUndefined=y;exports.isPromise=d;exports.isUndefined=i;exports.preventTimeout=P;exports.sleep=l;exports.tryCatchAsync=T;exports.uint8ArrayToBase64=s;
package/dist/utils.d.ts CHANGED
@@ -86,6 +86,7 @@ export declare const tryCatchAsync: <T, F extends ((error: unknown) => any) | un
86
86
  */
87
87
  export declare function preventTimeout<R>(callback: (() => Promise<R>) | Promise<R>, options?: {
88
88
  errorMessage?: string;
89
+ timeoutError?: any;
89
90
  timeout?: number;
90
91
  }): Promise<R>;
91
92
  export type ParamsWithMaybeUndefined<T extends readonly unknown[]> = {
package/dist/utils.js CHANGED
@@ -1,60 +1,62 @@
1
- const l = (e = 1500) => new Promise((n) => setTimeout(n, e));
2
- function m(e, n = 4, r = 4, t = "...") {
1
+ const m = (e = 1500) => new Promise((n) => setTimeout(n, e));
2
+ function h(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
- const s = (e) => e === void 0, h = (...e) => e.some(s), y = (e) => !!e && typeof e?.then == "function" && typeof e?.catch == "function";
6
- function a(e) {
5
+ const o = (e) => e === void 0, y = (...e) => e.some(o), d = (e) => !!e && typeof e?.then == "function" && typeof e?.catch == "function";
6
+ function f(e) {
7
7
  let n = "";
8
8
  for (let r = 0; r < e.length; r++)
9
9
  n += String.fromCharCode(e[r]);
10
10
  return btoa(n);
11
11
  }
12
- function d(e) {
12
+ function g(e) {
13
13
  const n = atob(e), r = new Uint8Array(n.length);
14
14
  for (let t = 0; t < n.length; t++)
15
15
  r[t] = n.charCodeAt(t);
16
16
  return r;
17
17
  }
18
- function g(e) {
19
- return a(new Uint8Array(e));
18
+ function p(e) {
19
+ return f(new Uint8Array(e));
20
20
  }
21
- function f(e) {
21
+ function c(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("-"))
23
23
  return !1;
24
24
  const n = e.split(".");
25
25
  return !(n.length < 2 || n.some((r) => !r.length) || n[n.length - 1].length < 2);
26
26
  }
27
- function p(e) {
27
+ function w(e) {
28
28
  const [n, ...r] = e.toLowerCase().split("@");
29
29
  if (!n || !r.length || !/^[a-z0-9._-]+$/.test(n) || /^\.|\.$/.test(n) || /[.\-_]{2,}/.test(n) || /[.\-_](?![a-z0-9])/.test(n)) return !1;
30
30
  for (const t of r)
31
- if (!f(t)) return !1;
31
+ if (!c(t)) return !1;
32
32
  return !0;
33
33
  }
34
- const w = async (e, n) => {
34
+ const P = async (e, n) => {
35
35
  try {
36
36
  return await e;
37
37
  } catch (r) {
38
38
  return n?.(r);
39
39
  }
40
40
  };
41
- async function P(e, n) {
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
- return Promise.race([o, i]);
41
+ async function T(e, n) {
42
+ const { errorMessage: r = "Timeout", timeoutError: t, timeout: s = 15e3 } = n || {}, i = typeof e == "function" ? e() : e, u = new Promise(
43
+ (l, a) => setTimeout(() => a(t || new Error(r)), s)
44
+ );
45
+ return Promise.race([i, u]);
44
46
  }
45
- const T = (e, n, r) => e.some(s) ? r : n(...e);
47
+ const A = (e, n, r) => e.some(o) ? r : n(...e);
46
48
  export {
47
- g as arrayBufferToBase64,
48
- d as base64ToUint8Array,
49
- T as computeWithDefinedParams,
50
- m as encrypt,
51
- f as isDomain,
52
- p as isEmail,
53
- h as isHasUndefined,
54
- y as isPromise,
55
- s as isUndefined,
56
- P as preventTimeout,
57
- l as sleep,
58
- w as tryCatchAsync,
59
- a as uint8ArrayToBase64
49
+ p as arrayBufferToBase64,
50
+ g as base64ToUint8Array,
51
+ A as computeWithDefinedParams,
52
+ h as encrypt,
53
+ c as isDomain,
54
+ w as isEmail,
55
+ y as isHasUndefined,
56
+ d as isPromise,
57
+ o as isUndefined,
58
+ T as preventTimeout,
59
+ m as sleep,
60
+ P as tryCatchAsync,
61
+ f as uint8ArrayToBase64
60
62
  };
@@ -0,0 +1,2 @@
1
+ export { default as useCatchError } from "./useCatchError";
2
+ export { default as useLoadingEvent } from "./useLoadingEvent";
@@ -0,0 +1,8 @@
1
+ declare const useCatchError: <Args extends any[], R, IsThrowError extends boolean = false>(execute: (...args: Args) => MaybePromise<R>, options?: Partial<{
2
+ target: string;
3
+ timeout: number;
4
+ timeoutError: any;
5
+ isThrowError: IsThrowError;
6
+ onError: (error: unknown, target: MaybeUndefined<string>, ...args: Args) => any;
7
+ }>) => (...args: Args) => Promise<IsThrowError extends true ? R : MaybeUndefined<R>>;
8
+ export default useCatchError;
@@ -0,0 +1,5 @@
1
+ import { type ComputedRef } from "vue";
2
+ declare const useLoadingEvent: <Args extends any[], R extends any>(execute: (...args: Args) => Promise<R>, options?: Partial<{
3
+ isAllowMulticall: boolean;
4
+ }>) => [(...args: Args) => Promise<R>, ComputedRef<boolean>];
5
+ export default useLoadingEvent;
package/dist/vue.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./utils.cjs"),s=require("vue"),c=(i,e)=>{const u=e?.timeout??2e4,r={timeout:u,timeoutError:e?.timeoutError?e?.timeoutError:new Error(`${e?.target?`${e?.target} `:""}Timeout of ${u}ms exceeded`),isThrowError:e?.isThrowError??!1,...e};return(async(...o)=>{try{return await n.preventTimeout(Promise.resolve(i(...o)),{timeout:r.timeout,timeoutError:r.timeoutError})}catch(t){if(r.onError?.(t,r.target,...o),r.isThrowError)throw t;return}})},m=(i,e)=>{const u={isAllowMulticall:!1,...e},r=s.ref(!1);let o=0,t;return[async(...l)=>{if(r.value&&t&&!u.isAllowMulticall)return t;r.value=!0;const a=o+=1;return t=i(...l).finally(()=>{a===o&&(t=void 0,r.value=!1)}),t},s.computed(()=>r.value)]};exports.useCatchError=c;exports.useLoadingEvent=m;
package/dist/vue.js ADDED
@@ -0,0 +1,47 @@
1
+ import { preventTimeout as a } from "./utils.js";
2
+ import { ref as m, computed as c } from "vue";
3
+ const v = (u, e) => {
4
+ const i = e?.timeout ?? 2e4, r = {
5
+ timeout: i,
6
+ timeoutError: e?.timeoutError ? e?.timeoutError : new Error(`${e?.target ? `${e?.target} ` : ""}Timeout of ${i}ms exceeded`),
7
+ isThrowError: e?.isThrowError ?? !1,
8
+ ...e
9
+ };
10
+ return (async (...o) => {
11
+ try {
12
+ return await a(Promise.resolve(u(...o)), {
13
+ timeout: r.timeout,
14
+ timeoutError: r.timeoutError
15
+ });
16
+ } catch (t) {
17
+ if (r.onError?.(t, r.target, ...o), r.isThrowError)
18
+ throw t;
19
+ return;
20
+ }
21
+ });
22
+ }, d = (u, e) => {
23
+ const i = {
24
+ /**
25
+ * 是否允许函数多次调用,默认: false
26
+ * false 情况下,如果再次执行 execute 会引用上一次的 promise 直接返回
27
+ */
28
+ isAllowMulticall: !1,
29
+ ...e
30
+ }, r = m(!1);
31
+ let o = 0, t;
32
+ return [
33
+ async (...s) => {
34
+ if (r.value && t && !i.isAllowMulticall) return t;
35
+ r.value = !0;
36
+ const l = o += 1;
37
+ return t = u(...s).finally(() => {
38
+ l === o && (t = void 0, r.value = !1);
39
+ }), t;
40
+ },
41
+ c(() => r.value)
42
+ ];
43
+ };
44
+ export {
45
+ v as useCatchError,
46
+ d as useLoadingEvent
47
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlikemario/helper",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "A utility library with number operations and common helper functions",
5
5
  "keywords": [
6
6
  "utility",
@@ -32,13 +32,16 @@
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^24",
35
+ "@types/react": "^19",
35
36
  "bignumber.js": "^9.3.1",
36
37
  "dayjs": "^1.11.18",
37
38
  "node": "^24.8.0",
39
+ "react": "^19.2.3",
38
40
  "typescript": "~5.8.3",
39
41
  "vite": "^7.1.2",
40
42
  "vite-plugin-dts": "^4.5.4",
41
- "vitest": "^3.2.4"
43
+ "vitest": "^3.2.4",
44
+ "vue": "^3.5.27"
42
45
  },
43
46
  "main": "./dist/number.js",
44
47
  "module": "./dist/number.js",
@@ -68,6 +71,16 @@
68
71
  "types": "./dist/decorator-old.d.ts",
69
72
  "import": "./dist/decorator-old.js",
70
73
  "require": "./dist/decorator-old.cjs"
74
+ },
75
+ "./vue": {
76
+ "types": "./dist/vue.d.ts",
77
+ "import": "./dist/vue.js",
78
+ "require": "./dist/vue.cjs"
79
+ },
80
+ "./react": {
81
+ "types": "./dist/react.d.ts",
82
+ "import": "./dist/react.js",
83
+ "require": "./dist/react.cjs"
71
84
  }
72
85
  },
73
86
  "packageManager": "yarn@4.9.4+sha512.7b1cb0b62abba6a537b3a2ce00811a843bea02bcf53138581a6ae5b1bf563f734872bd47de49ce32a9ca9dcaff995aa789577ffb16811da7c603dcf69e73750b",