@zlikemario/helper 0.0.15 → 1.0.0
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/AGENTS.md +36 -0
- package/AGENTS_USE.md +272 -0
- package/dist/react/useAsyncData.d.ts +10 -0
- package/dist/react/useCatchError.d.ts +10 -0
- package/dist/react/useLoadingEvent.d.ts +9 -0
- package/dist/react/useSyncedRef.d.ts +6 -0
- package/dist/utils.cjs +1 -1
- package/dist/utils.js +37 -33
- package/dist/vue/useAsyncData.d.ts +10 -0
- package/dist/vue/useCatchError.d.ts +10 -0
- package/dist/vue/useLoadingEvent.d.ts +9 -0
- package/package.json +5 -4
package/AGENTS.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @zlikemario/helper - Agents Guide
|
|
2
|
+
|
|
3
|
+
本文件是 **AI 代理的入口说明**,用于快速判断你在这个仓库中的角色,以及接下来应该阅读的文档。
|
|
4
|
+
|
|
5
|
+
## 这个库是做什么的?
|
|
6
|
+
|
|
7
|
+
这是一个【TypeScript 工具库,提供数字处理、常用工具函数、装饰器,以及 React/Vue 的 hooks】的库。
|
|
8
|
+
AI 在参与任务前,应先明确自己的身份,再继续操作。
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 第一步:确认你的角色
|
|
13
|
+
|
|
14
|
+
### 你只是**使用这个库**
|
|
15
|
+
|
|
16
|
+
- 场景:调用 API、集成到项目中、写业务代码
|
|
17
|
+
- 请直接阅读 👉 **[AGENTS_USE](./AGENTS_USE.md)**
|
|
18
|
+
这里说明:
|
|
19
|
+
- 这个库解决什么问题
|
|
20
|
+
- 提供了哪些能力
|
|
21
|
+
- 正确的使用方式与约束
|
|
22
|
+
|
|
23
|
+
### 你需要**修改或扩展这个库**
|
|
24
|
+
|
|
25
|
+
- 场景:新增功能、调整实现、修 Bug、重构
|
|
26
|
+
- 请先完整阅读 👉 **[AGENTS_CODING](./AGENTS_CODING.md)**
|
|
27
|
+
这里定义:
|
|
28
|
+
- 代码规范与结构约定
|
|
29
|
+
- 允许与禁止的改动范围
|
|
30
|
+
- 提交前必须遵守的规则
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## 重要约定
|
|
35
|
+
|
|
36
|
+
在未阅读对应文档前,不要开始写代码。
|
package/AGENTS_USE.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# @zlikemario/helper - Agents Guide (Use)
|
|
2
|
+
|
|
3
|
+
面向“使用这个库”的 AI。目标:用最少 token 快速定位方法 + 看到即用的示例。
|
|
4
|
+
|
|
5
|
+
## 1. 快速入口
|
|
6
|
+
|
|
7
|
+
- 默认入口:`@zlikemario/helper` 等价于 `@zlikemario/helper/number`
|
|
8
|
+
- 子路径入口:`/number`、`/utils`、`/decorator`、`/decorator-old`、`/react`、`/vue`、`/types`
|
|
9
|
+
|
|
10
|
+
## 2. 方法速查(只列名称,先定位,再看下面示例)
|
|
11
|
+
|
|
12
|
+
### number
|
|
13
|
+
|
|
14
|
+
`BigNumber` `isNumber` `simplifyNumber` `readabilityNumber` `toPercentage` `formatPrecision` `readableNumber` `sum` `sumBy`
|
|
15
|
+
|
|
16
|
+
### utils
|
|
17
|
+
|
|
18
|
+
`sleep` `encrypt` `isUndefined` `isHasUndefined` `isPromise` `uint8ArrayToBase64` `base64ToUint8Array` `arrayBufferToBase64` `isDomain` `isEmail` `tryCatchAsync` `preventTimeout` `computeWithDefinedParams`
|
|
19
|
+
|
|
20
|
+
### decorator
|
|
21
|
+
|
|
22
|
+
`Memoize`
|
|
23
|
+
|
|
24
|
+
### decorator-old
|
|
25
|
+
|
|
26
|
+
`Memoize`
|
|
27
|
+
|
|
28
|
+
### react
|
|
29
|
+
|
|
30
|
+
`useAsyncData` `useCatchError` `useLoadingEvent` `useSyncedRef`
|
|
31
|
+
|
|
32
|
+
### vue
|
|
33
|
+
|
|
34
|
+
`useAsyncData` `useCatchError` `useLoadingEvent`
|
|
35
|
+
|
|
36
|
+
### types
|
|
37
|
+
|
|
38
|
+
`Numberish` `NumberString` `MaybeNull` `MaybeUndefined` `MaybePromise` `Defined` `AnyFn` `Noop`
|
|
39
|
+
|
|
40
|
+
## 3. 用法速览(按模块分组,一函数一用法)
|
|
41
|
+
|
|
42
|
+
### number
|
|
43
|
+
|
|
44
|
+
导入路径:`@zlikemario/helper/number`
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import {
|
|
48
|
+
BigNumber,
|
|
49
|
+
isNumber,
|
|
50
|
+
simplifyNumber,
|
|
51
|
+
readabilityNumber,
|
|
52
|
+
toPercentage,
|
|
53
|
+
formatPrecision,
|
|
54
|
+
readableNumber,
|
|
55
|
+
sum,
|
|
56
|
+
sumBy,
|
|
57
|
+
} from "@zlikemario/helper/number";
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
isNumber("123");
|
|
62
|
+
isNumber("12.3", true);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
simplifyNumber(1234567); // "1.2M"
|
|
67
|
+
simplifyNumber("999", 2); // "999"
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
readabilityNumber(1234567.89); // "1,234,567.89"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
toPercentage(0.1234); // "12.34%"
|
|
76
|
+
toPercentage(0.1234, 1, true); // "12.3"
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
formatPrecision("1.234567", 2); // "1.23"
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
readableNumber("1234.0000123", 4); // "1,234.0{4}12"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
sum(["0.1", 0.2, 0.3]); // "0.6"
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
sumBy([{ n: 1 }, { n: 2 }], "n"); // "3"
|
|
93
|
+
sumBy([{ n: 1 }, { n: 2 }], (item) => item.n * 2); // "6"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
const v = new BigNumber("0.1").plus("0.2");
|
|
98
|
+
v.toString(); // "0.3"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### utils
|
|
102
|
+
|
|
103
|
+
导入路径:`@zlikemario/helper/utils`
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import {
|
|
107
|
+
sleep,
|
|
108
|
+
encrypt,
|
|
109
|
+
isUndefined,
|
|
110
|
+
isHasUndefined,
|
|
111
|
+
isPromise,
|
|
112
|
+
uint8ArrayToBase64,
|
|
113
|
+
base64ToUint8Array,
|
|
114
|
+
arrayBufferToBase64,
|
|
115
|
+
isDomain,
|
|
116
|
+
isEmail,
|
|
117
|
+
tryCatchAsync,
|
|
118
|
+
preventTimeout,
|
|
119
|
+
computeWithDefinedParams,
|
|
120
|
+
} from "@zlikemario/helper/utils";
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
await sleep(1000);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
encrypt("1234567890", 2, 2); // "12...90"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
isUndefined(undefined); // true
|
|
133
|
+
isHasUndefined(1, undefined, 3); // true
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
isPromise(Promise.resolve(1)); // true
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
uint8ArrayToBase64(new Uint8Array([1, 2, 3]));
|
|
142
|
+
base64ToUint8Array("AQID");
|
|
143
|
+
arrayBufferToBase64(new Uint8Array([1, 2, 3]).buffer);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
isDomain("example.com"); // true
|
|
148
|
+
isEmail("user@example.com"); // true
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
const data = await tryCatchAsync(fetch("/api").then((r) => r.json()));
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
await preventTimeout(fetch("/api"), { timeout: 3000 });
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
computeWithDefinedParams([1, undefined], (a, b) => a + b, 0); // 0
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### decorator (TS 5.0 新装饰器)
|
|
164
|
+
|
|
165
|
+
导入路径:`@zlikemario/helper/decorator`
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import { Memoize } from "@zlikemario/helper/decorator";
|
|
169
|
+
|
|
170
|
+
class A {
|
|
171
|
+
@Memoize(1000)
|
|
172
|
+
calc(n: number) {
|
|
173
|
+
return n * 2;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### decorator-old (旧装饰器语法)
|
|
179
|
+
|
|
180
|
+
导入路径:`@zlikemario/helper/decorator-old`
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { Memoize } from "@zlikemario/helper/decorator-old";
|
|
184
|
+
|
|
185
|
+
class A {
|
|
186
|
+
@Memoize(1000)
|
|
187
|
+
calc(n: number) {
|
|
188
|
+
return n * 2;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### react
|
|
194
|
+
|
|
195
|
+
导入路径:`@zlikemario/helper/react`
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { useAsyncData, useCatchError, useLoadingEvent, useSyncedRef } from "@zlikemario/helper/react";
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
const [data, refresh] = useAsyncData(fetcher, { defaultData: [], onError: () => [] });
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const run = useCatchError(exec, { target: "load", timeout: 5000, onError: console.error });
|
|
207
|
+
await run("id");
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
const [run, isLoading] = useLoadingEvent(exec, { isAllowMulticall: false });
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const states = useSyncedRef({ execute, options });
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### vue
|
|
219
|
+
|
|
220
|
+
导入路径:`@zlikemario/helper/vue`
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import { useAsyncData, useCatchError, useLoadingEvent } from "@zlikemario/helper/vue";
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
const [data, refresh] = useAsyncData(fetcher, { defaultData: [], onError: () => [] });
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const run = useCatchError(exec, { target: "load", timeout: 5000, onError: console.error });
|
|
232
|
+
await run("id");
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
const [run, isLoading] = useLoadingEvent(exec, { isAllowMulticall: false });
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### types
|
|
240
|
+
|
|
241
|
+
导入路径:`@zlikemario/helper/types`
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
import type {
|
|
245
|
+
Numberish,
|
|
246
|
+
NumberString,
|
|
247
|
+
MaybeNull,
|
|
248
|
+
MaybeUndefined,
|
|
249
|
+
MaybePromise,
|
|
250
|
+
Defined,
|
|
251
|
+
AnyFn,
|
|
252
|
+
Noop,
|
|
253
|
+
} from "@zlikemario/helper/types";
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## 4. 语义与行为约定
|
|
257
|
+
|
|
258
|
+
- 数字类函数统一接受 `Numberish`,无效输入通常返回 `String(num)`。
|
|
259
|
+
- 大数计算使用 `BigNumber`,避免浮点误差。
|
|
260
|
+
- `sum`/`sumBy` 返回字符串类型(`NumberString`),方便精度控制。
|
|
261
|
+
- `simplifyNumber` 的单位固定为 K/M/B,保留 1 位小数。
|
|
262
|
+
- `Memoize` 默认缓存 key 为第一个参数;可通过 `computeKey` 自定义。
|
|
263
|
+
|
|
264
|
+
## 5. 代码位置索引 (src)
|
|
265
|
+
|
|
266
|
+
- [src/number.ts](src/number.ts)
|
|
267
|
+
- [src/utils.ts](src/utils.ts)
|
|
268
|
+
- [src/decorator.ts](src/decorator.ts)
|
|
269
|
+
- [src/decorator-old.ts](src/decorator-old.ts)
|
|
270
|
+
- [src/react](src/react)
|
|
271
|
+
- [src/vue](src/vue)
|
|
272
|
+
- [src/types.ts](src/types.ts)
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import type { MaybeUndefined } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* 用于加载异步数据并提供手动刷新能力的 React hook。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* const [data, refresh] = useAsyncData(fetcher, { defaultData, onError });
|
|
7
|
+
*
|
|
8
|
+
* - 组件挂载时调用一次 `fetcher`
|
|
9
|
+
* - 返回最新数据与刷新函数
|
|
10
|
+
* - 若提供 `onError`,其返回值会作为数据
|
|
11
|
+
*/
|
|
2
12
|
declare const useAsyncData: <R>(getDataFunc: () => Promise<R>, options?: Partial<{
|
|
3
13
|
defaultData: R;
|
|
4
14
|
onError: (error: unknown) => any;
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import type { MaybePromise, MaybeUndefined } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* 包装异步/同步任务,统一超时与错误处理。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* const run = useCatchError(exec, { target, timeout, isThrowError, onError });
|
|
7
|
+
*
|
|
8
|
+
* - 支持超时控制
|
|
9
|
+
* - 可选择是否继续抛错
|
|
10
|
+
* - onError 可用于埋点或兜底处理
|
|
11
|
+
*/
|
|
2
12
|
declare const useCatchError: <Args extends any[], R, IsThrowError extends boolean = false>(execute: (...args: Args) => MaybePromise<R>, options?: Partial<{
|
|
3
13
|
target: string;
|
|
4
14
|
timeout: number;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 包装异步执行函数,返回带 loading 状态的调用器。
|
|
3
|
+
*
|
|
4
|
+
* 用法:
|
|
5
|
+
* const [run, isLoading] = useLoadingEvent(exec, { isAllowMulticall });
|
|
6
|
+
*
|
|
7
|
+
* - 默认不允许并发,重复调用会复用上一次 Promise
|
|
8
|
+
* - 可通过 isAllowMulticall 允许并发调用
|
|
9
|
+
*/
|
|
1
10
|
declare const useLoadingEvent: <Args extends any[], R extends any>(execute: (...args: Args) => Promise<R>, options?: Partial<{
|
|
2
11
|
isAllowMulticall: boolean;
|
|
3
12
|
}>) => [(...args: Args) => Promise<R>, boolean];
|
package/dist/utils.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 m=(e=1500)=>new Promise(r=>setTimeout(r,e));function y(e,r=4,n=4,t="..."){return!e||e.length<=r+n?e:e.slice(0,r)+t+(n?e.slice(-n):"")}const i=e=>e===void 0,h=(...e)=>e.some(i),d=e=>!!e&&typeof e?.then=="function"&&typeof e?.catch=="function";function f(e){let r="";for(let n=0;n<e.length;n++)r+=String.fromCharCode(e[n]);return btoa(r)}function g(e){const r=atob(e),n=new Uint8Array(r.length);for(let t=0;t<r.length;t++)n[t]=r.charCodeAt(t);return n}function p(e){return f(new Uint8Array(e))}function c(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 r=e.split(".");return!(r.length<2||r.some(n=>!n.length)||r[r.length-1].length<2)}function T(e){const[r,...n]=e.toLowerCase().split("@");if(!r||!n.length||!/^[a-z0-9._-]+$/.test(r)||/^\.|\.$/.test(r)||/[.\-_]{2,}/.test(r)||/[.\-_](?![a-z0-9])/.test(r))return!1;for(const t of n)if(!c(t))return!1;return!0}const P=async(e,r)=>{try{return await e}catch(n){return r?.(n)}};async function w(e,r){const{errorMessage:n="Timeout",timeoutError:t,timeout:s=15e3}=r||{},o=typeof e=="function"?e():e,a=s>0?new Promise((u,l)=>setTimeout(()=>l(t||new Error(n)),s)):null;try{return Promise.race(a?[o,a]:[o])}catch(u){throw u}}const A=(e,r,n)=>e.some(i)?n:r(...e);exports.arrayBufferToBase64=p;exports.base64ToUint8Array=g;exports.computeWithDefinedParams=A;exports.encrypt=y;exports.isDomain=c;exports.isEmail=T;exports.isHasUndefined=h;exports.isPromise=d;exports.isUndefined=i;exports.preventTimeout=w;exports.sleep=m;exports.tryCatchAsync=P;exports.uint8ArrayToBase64=f;
|
package/dist/utils.js
CHANGED
|
@@ -1,62 +1,66 @@
|
|
|
1
|
-
const m = (e = 1500) => new Promise((
|
|
2
|
-
function h(e,
|
|
3
|
-
return !e || e.length <=
|
|
1
|
+
const m = (e = 1500) => new Promise((r) => setTimeout(r, e));
|
|
2
|
+
function h(e, r = 4, n = 4, t = "...") {
|
|
3
|
+
return !e || e.length <= r + n ? e : e.slice(0, r) + t + (n ? e.slice(-n) : "");
|
|
4
4
|
}
|
|
5
|
-
const
|
|
6
|
-
function
|
|
7
|
-
let
|
|
8
|
-
for (let
|
|
9
|
-
|
|
10
|
-
return btoa(
|
|
5
|
+
const a = (e) => e === void 0, y = (...e) => e.some(a), d = (e) => !!e && typeof e?.then == "function" && typeof e?.catch == "function";
|
|
6
|
+
function c(e) {
|
|
7
|
+
let r = "";
|
|
8
|
+
for (let n = 0; n < e.length; n++)
|
|
9
|
+
r += String.fromCharCode(e[n]);
|
|
10
|
+
return btoa(r);
|
|
11
11
|
}
|
|
12
12
|
function g(e) {
|
|
13
|
-
const
|
|
14
|
-
for (let t = 0; t <
|
|
15
|
-
|
|
16
|
-
return
|
|
13
|
+
const r = atob(e), n = new Uint8Array(r.length);
|
|
14
|
+
for (let t = 0; t < r.length; t++)
|
|
15
|
+
n[t] = r.charCodeAt(t);
|
|
16
|
+
return n;
|
|
17
17
|
}
|
|
18
18
|
function p(e) {
|
|
19
|
-
return
|
|
19
|
+
return c(new Uint8Array(e));
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function l(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
|
-
const
|
|
25
|
-
return !(
|
|
24
|
+
const r = e.split(".");
|
|
25
|
+
return !(r.length < 2 || r.some((n) => !n.length) || r[r.length - 1].length < 2);
|
|
26
26
|
}
|
|
27
27
|
function w(e) {
|
|
28
|
-
const [
|
|
29
|
-
if (!
|
|
30
|
-
for (const t of
|
|
31
|
-
if (!
|
|
28
|
+
const [r, ...n] = e.toLowerCase().split("@");
|
|
29
|
+
if (!r || !n.length || !/^[a-z0-9._-]+$/.test(r) || /^\.|\.$/.test(r) || /[.\-_]{2,}/.test(r) || /[.\-_](?![a-z0-9])/.test(r)) return !1;
|
|
30
|
+
for (const t of n)
|
|
31
|
+
if (!l(t)) return !1;
|
|
32
32
|
return !0;
|
|
33
33
|
}
|
|
34
|
-
const P = async (e,
|
|
34
|
+
const P = async (e, r) => {
|
|
35
35
|
try {
|
|
36
36
|
return await e;
|
|
37
|
-
} catch (
|
|
38
|
-
return
|
|
37
|
+
} catch (n) {
|
|
38
|
+
return r?.(n);
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
|
-
async function T(e,
|
|
42
|
-
const { errorMessage:
|
|
43
|
-
(
|
|
44
|
-
);
|
|
45
|
-
|
|
41
|
+
async function T(e, r) {
|
|
42
|
+
const { errorMessage: n = "Timeout", timeoutError: t, timeout: o = 15e3 } = r || {}, s = typeof e == "function" ? e() : e, i = o > 0 ? new Promise(
|
|
43
|
+
(u, f) => setTimeout(() => f(t || new Error(n)), o)
|
|
44
|
+
) : null;
|
|
45
|
+
try {
|
|
46
|
+
return Promise.race(i ? [s, i] : [s]);
|
|
47
|
+
} catch (u) {
|
|
48
|
+
throw u;
|
|
49
|
+
}
|
|
46
50
|
}
|
|
47
|
-
const A = (e,
|
|
51
|
+
const A = (e, r, n) => e.some(a) ? n : r(...e);
|
|
48
52
|
export {
|
|
49
53
|
p as arrayBufferToBase64,
|
|
50
54
|
g as base64ToUint8Array,
|
|
51
55
|
A as computeWithDefinedParams,
|
|
52
56
|
h as encrypt,
|
|
53
|
-
|
|
57
|
+
l as isDomain,
|
|
54
58
|
w as isEmail,
|
|
55
59
|
y as isHasUndefined,
|
|
56
60
|
d as isPromise,
|
|
57
|
-
|
|
61
|
+
a as isUndefined,
|
|
58
62
|
T as preventTimeout,
|
|
59
63
|
m as sleep,
|
|
60
64
|
P as tryCatchAsync,
|
|
61
|
-
|
|
65
|
+
c as uint8ArrayToBase64
|
|
62
66
|
};
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { type ComputedRef, type MaybeRef } from "vue";
|
|
2
2
|
import type { MaybeUndefined } from "../types";
|
|
3
|
+
/**
|
|
4
|
+
* 用于加载异步数据并提供手动刷新能力的 Vue hook。
|
|
5
|
+
*
|
|
6
|
+
* 用法:
|
|
7
|
+
* const [data, refresh] = useAsyncData(fetcher, { defaultData, onError });
|
|
8
|
+
*
|
|
9
|
+
* - 立即执行一次 `fetcher`
|
|
10
|
+
* - 返回计算属性与刷新函数
|
|
11
|
+
* - 若提供 `onError`,其返回值会作为数据
|
|
12
|
+
*/
|
|
3
13
|
declare const useAsyncData: <R>(getDataFunc: () => Promise<R>, options?: Partial<{
|
|
4
14
|
defaultData: MaybeRef<R>;
|
|
5
15
|
onError: (error: unknown) => any;
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import type { MaybePromise, MaybeUndefined } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* 包装异步/同步任务,统一超时与错误处理。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* const run = useCatchError(exec, { target, timeout, isThrowError, onError });
|
|
7
|
+
*
|
|
8
|
+
* - 支持超时控制
|
|
9
|
+
* - 可选择是否继续抛错
|
|
10
|
+
* - onError 可用于埋点或兜底处理
|
|
11
|
+
*/
|
|
2
12
|
declare const useCatchError: <Args extends any[], R, IsThrowError extends boolean = false>(execute: (...args: Args) => MaybePromise<R>, options?: Partial<{
|
|
3
13
|
target: string;
|
|
4
14
|
timeout: number;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { type ComputedRef } from "vue";
|
|
2
|
+
/**
|
|
3
|
+
* 包装异步执行函数,返回带 loading 状态的调用器。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* const [run, isLoading] = useLoadingEvent(exec, { isAllowMulticall });
|
|
7
|
+
*
|
|
8
|
+
* - 默认不允许并发,重复调用会复用上一次 Promise
|
|
9
|
+
* - 可通过 isAllowMulticall 允许并发调用
|
|
10
|
+
*/
|
|
2
11
|
declare const useLoadingEvent: <Args extends any[], R extends any>(execute: (...args: Args) => Promise<R>, options?: Partial<{
|
|
3
12
|
isAllowMulticall: boolean;
|
|
4
13
|
}>) => [(...args: Args) => Promise<R>, ComputedRef<boolean>];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zlikemario/helper",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A utility library with number operations and common helper functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"utility",
|
|
@@ -13,7 +13,9 @@
|
|
|
13
13
|
"type": "module",
|
|
14
14
|
"files": [
|
|
15
15
|
"dist",
|
|
16
|
-
"README.md"
|
|
16
|
+
"README.md",
|
|
17
|
+
"AGENTS.md",
|
|
18
|
+
"AGENTS_USE.md"
|
|
17
19
|
],
|
|
18
20
|
"repository": {
|
|
19
21
|
"type": "git",
|
|
@@ -86,7 +88,6 @@
|
|
|
86
88
|
},
|
|
87
89
|
"packageManager": "yarn@4.9.4+sha512.7b1cb0b62abba6a537b3a2ce00811a843bea02bcf53138581a6ae5b1bf563f734872bd47de49ce32a9ca9dcaff995aa789577ffb16811da7c603dcf69e73750b",
|
|
88
90
|
"dependencies": {
|
|
89
|
-
"bignumber.js": "^9.3.1"
|
|
90
|
-
"dayjs": "^1.11.18"
|
|
91
|
+
"bignumber.js": "^9.3.1"
|
|
91
92
|
}
|
|
92
93
|
}
|