@zlikemario/helper 1.0.1 → 1.0.2

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_USE.md CHANGED
@@ -1,17 +1,37 @@
1
- # @zlikemario/helper - Agents Guide (Use)
1
+ # @zlikemario/helper · Agents Guide (Use)
2
2
 
3
- 面向“使用这个库”的 AI。目标:用最少 token 快速定位方法 + 看到即用的示例。
3
+ > 面向“**使用这个库**”的 AI
4
+ > 目标只有一个:**最少 token,最快定位方法,复制就能用。**
4
5
 
5
- ## 1. 快速入口
6
+ 这不是给人类看的长文档,是给 AI 当工具箱索引用的。别抒情,直接干活。
6
7
 
7
- - 默认入口:`@zlikemario/helper` 等价于 `@zlikemario/helper/number`
8
- - 子路径入口:`/number`、`/utils`、`/decorator`、`/decorator-old`、`/react`、`/vue`、`/types`
8
+ ---
9
9
 
10
- ## 2. 方法速查(只列名称,先定位,再看下面示例)
10
+ ## 1. 快速入口(先定位模块,再看示例)
11
+
12
+ - **默认入口**:`@zlikemario/helper`
13
+ 等价于:`@zlikemario/helper/number`
14
+
15
+ - **子路径入口**:
16
+ - `/number`
17
+ - `/utils`
18
+ - `/decorator`
19
+ - `/decorator-old`
20
+ - `/react`
21
+ - `/vue`
22
+ - `/types`
23
+
24
+ AI 使用策略:
25
+
26
+ > 先判断问题属于哪个模块 → 直接跳子路径 → 找对应函数示例。
27
+
28
+ ---
29
+
30
+ ## 2. 方法速查(只列名字,用来“搜脑子”)
11
31
 
12
32
  ### number
13
33
 
14
- `BigNumber` `isNumber` `simplifyNumber` `readabilityNumber` `toPercentage` `formatPrecision` `readableNumber` `sum` `sumBy`
34
+ `BigNumber` `trimFloatEndZero` `isComputableNumber` `unitFormat` `readabilityNumber` `toPercentage` `readableNumber` `sum` `sumBy`
15
35
 
16
36
  ### utils
17
37
 
@@ -35,22 +55,24 @@
35
55
 
36
56
  ### types
37
57
 
38
- `Numberish` `NumberString` `MaybeNull` `MaybeUndefined` `MaybePromise` `Defined` `AnyFn` `Noop`
58
+ `IntString` `DecimalsString` `Numberish` `NumberString` `MaybeNull` `MaybeUndefined` `MaybePromise` `Defined` `AnyFn` `Noop`
59
+
60
+ ---
39
61
 
40
- ## 3. 用法速览(按模块分组,一函数一用法)
62
+ ## 3. 用法速览(一函数一例,禁止废话)
41
63
 
42
64
  ### number
43
65
 
44
- 导入路径:`@zlikemario/helper/number`
66
+ **导入路径**:`@zlikemario/helper/number`
45
67
 
46
68
  ```ts
47
69
  import {
48
70
  BigNumber,
49
- isNumber,
50
- simplifyNumber,
71
+ trimFloatEndZero,
72
+ isComputableNumber,
73
+ unitFormat,
51
74
  readabilityNumber,
52
75
  toPercentage,
53
- formatPrecision,
54
76
  readableNumber,
55
77
  sum,
56
78
  sumBy,
@@ -58,13 +80,13 @@ import {
58
80
  ```
59
81
 
60
82
  ```ts
61
- isNumber("123");
62
- isNumber("12.3", true);
83
+ isComputableNumber("123");
84
+ isComputableNumber("12.3", true);
63
85
  ```
64
86
 
65
87
  ```ts
66
- simplifyNumber(1234567); // "1.2M"
67
- simplifyNumber("999", 2); // "999"
88
+ unitFormat(1234567); // "1.2M"
89
+ unitFormat("999", 2); // "999"
68
90
  ```
69
91
 
70
92
  ```ts
@@ -77,7 +99,7 @@ toPercentage(0.1234, 1, true); // "12.3"
77
99
  ```
78
100
 
79
101
  ```ts
80
- formatPrecision("1.234567", 2); // "1.23"
102
+ trimFloatEndZero("1234.100100"); // "1234.1001"
81
103
  ```
82
104
 
83
105
  ```ts
@@ -98,9 +120,11 @@ const v = new BigNumber("0.1").plus("0.2");
98
120
  v.toString(); // "0.3"
99
121
  ```
100
122
 
123
+ ---
124
+
101
125
  ### utils
102
126
 
103
- 导入路径:`@zlikemario/helper/utils`
127
+ **导入路径**:`@zlikemario/helper/utils`
104
128
 
105
129
  ```ts
106
130
  import {
@@ -160,9 +184,11 @@ await preventTimeout(fetch("/api"), { timeout: 3000 });
160
184
  computeWithDefinedParams([1, undefined], (a, b) => a + b, 0); // 0
161
185
  ```
162
186
 
163
- ### decorator (TS 5.0 新装饰器)
187
+ ---
164
188
 
165
- 导入路径:`@zlikemario/helper/decorator`
189
+ ### decorator(TS 5.x 新装饰器)
190
+
191
+ **导入路径**:`@zlikemario/helper/decorator`
166
192
 
167
193
  ```ts
168
194
  import { Memoize } from "@zlikemario/helper/decorator";
@@ -175,9 +201,11 @@ class A {
175
201
  }
176
202
  ```
177
203
 
178
- ### decorator-old (旧装饰器语法)
204
+ ---
205
+
206
+ ### decorator-old(旧装饰器语法)
179
207
 
180
- 导入路径:`@zlikemario/helper/decorator-old`
208
+ **导入路径**:`@zlikemario/helper/decorator-old`
181
209
 
182
210
  ```ts
183
211
  import { Memoize } from "@zlikemario/helper/decorator-old";
@@ -190,20 +218,30 @@ class A {
190
218
  }
191
219
  ```
192
220
 
221
+ ---
222
+
193
223
  ### react
194
224
 
195
- 导入路径:`@zlikemario/helper/react`
225
+ **导入路径**:`@zlikemario/helper/react`
196
226
 
197
227
  ```ts
198
228
  import { useAsyncData, useCatchError, useLoadingEvent, useSyncedRef } from "@zlikemario/helper/react";
199
229
  ```
200
230
 
201
231
  ```ts
202
- const [data, refresh] = useAsyncData(fetcher, { defaultData: [], onError: () => [] });
232
+ const [data, refresh] = useAsyncData(fetcher, {
233
+ defaultData: [],
234
+ onError: () => [],
235
+ });
203
236
  ```
204
237
 
205
238
  ```ts
206
- const run = useCatchError(exec, { target: "load", timeout: 5000, onError: console.error });
239
+ const run = useCatchError(exec, {
240
+ target: "load",
241
+ timeout: 5000,
242
+ onError: console.error,
243
+ });
244
+
207
245
  await run("id");
208
246
  ```
209
247
 
@@ -215,20 +253,30 @@ const [run, isLoading] = useLoadingEvent(exec, { isAllowMulticall: false });
215
253
  const states = useSyncedRef({ execute, options });
216
254
  ```
217
255
 
256
+ ---
257
+
218
258
  ### vue
219
259
 
220
- 导入路径:`@zlikemario/helper/vue`
260
+ **导入路径**:`@zlikemario/helper/vue`
221
261
 
222
262
  ```ts
223
263
  import { useAsyncData, useCatchError, useLoadingEvent } from "@zlikemario/helper/vue";
224
264
  ```
225
265
 
226
266
  ```ts
227
- const [data, refresh] = useAsyncData(fetcher, { defaultData: [], onError: () => [] });
267
+ const [data, refresh] = useAsyncData(fetcher, {
268
+ defaultData: [],
269
+ onError: () => [],
270
+ });
228
271
  ```
229
272
 
230
273
  ```ts
231
- const run = useCatchError(exec, { target: "load", timeout: 5000, onError: console.error });
274
+ const run = useCatchError(exec, {
275
+ target: "load",
276
+ timeout: 5000,
277
+ onError: console.error,
278
+ });
279
+
232
280
  await run("id");
233
281
  ```
234
282
 
@@ -236,12 +284,16 @@ await run("id");
236
284
  const [run, isLoading] = useLoadingEvent(exec, { isAllowMulticall: false });
237
285
  ```
238
286
 
287
+ ---
288
+
239
289
  ### types
240
290
 
241
- 导入路径:`@zlikemario/helper/types`
291
+ **导入路径**:`@zlikemario/helper/types`
242
292
 
243
293
  ```ts
244
294
  import type {
295
+ IntString,
296
+ DecimalsString,
245
297
  Numberish,
246
298
  NumberString,
247
299
  MaybeNull,
@@ -253,20 +305,32 @@ import type {
253
305
  } from "@zlikemario/helper/types";
254
306
  ```
255
307
 
256
- ## 4. 语义与行为约定
308
+ ---
309
+
310
+ ## 4. 语义与行为约定(AI 必须遵守)
311
+
312
+ - 数字相关函数统一接受 `Numberish`
313
+ - 非法数字输入通常 **直接返回 `String(input)`**,不抛异常
314
+ - 所有高精度计算基于 `BigNumber`
315
+ - `sum` / `sumBy` **始终返回字符串**(`NumberString`)
316
+ - `unitFormat` 单位固定:`K / M / B`
317
+ - `Memoize`:
318
+ - 默认 key = 第一个参数
319
+ - 支持自定义 `computeKey`
320
+
321
+ ---
322
+
323
+ ## 5. 源码位置索引(用于深挖)
257
324
 
258
- - 数字类函数统一接受 `Numberish`,无效输入通常返回 `String(num)`。
259
- - 大数计算使用 `BigNumber`,避免浮点误差。
260
- - `sum`/`sumBy` 返回字符串类型(`NumberString`),方便精度控制。
261
- - `simplifyNumber` 的单位固定为 K/M/B,保留 1 位小数。
262
- - `Memoize` 默认缓存 key 为第一个参数;可通过 `computeKey` 自定义。
325
+ - `src/number.ts`
326
+ - `src/utils.ts`
327
+ - `src/decorator.ts`
328
+ - `src/decorator-old.ts`
329
+ - `src/react/*`
330
+ - `src/vue/*`
331
+ - `src/types.ts`
263
332
 
264
- ## 5. 代码位置索引 (src)
333
+ ---
265
334
 
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)
335
+ > 给 AI 的最后一句话:
336
+ > **别猜。按模块进,照例子抄。**
package/README.md CHANGED
@@ -17,13 +17,13 @@ pnpm add @zlikemario/helper
17
17
  ### Number Operations
18
18
 
19
19
  ```typescript
20
- import { BigNumber, isNumber, simplifyNumber } from '@zlikemario/helper/number'
20
+ import { BigNumber, isComputableNumber, unitFormat } from '@zlikemario/helper/number'
21
21
 
22
22
  // Check if a value is a number
23
- console.log(isNumber('123')) // true
23
+ console.log(isComputableNumber('123')) // true
24
24
 
25
25
  // Simplify large numbers
26
- console.log(simplifyNumber(1234567)) // "1.2M"
26
+ console.log(unitFormat(1234567)) // "1.2M"
27
27
 
28
28
  // Use BigNumber for precise calculations
29
29
  const result = new BigNumber('0.1').plus('0.2')
@@ -50,11 +50,11 @@ console.log(isEmail('user@example.com')) // true
50
50
  ### Number Module (`@zlikemario/helper/number`)
51
51
 
52
52
  - `BigNumber` - BigNumber.js instance for precise calculations
53
- - `isNumber(num, isInt?)` - Check if value is a number
54
- - `simplifyNumber(num, decimal?, rm?)` - Simplify large numbers (1.2K, 3.4M)
53
+ - `trimFloatEndZero(num)` - Remove trailing zeros from decimal strings
54
+ - `isComputableNumber(num, isInt?)` - Check if value is a computable number
55
+ - `unitFormat(num, decimals?, rm?)` - Simplify large numbers (1.2K, 3.4M)
55
56
  - `readabilityNumber(num)` - Add thousand separators
56
57
  - `toPercentage(num, precision?, isHiddenUnit?)` - Convert to percentage
57
- - `formatPrecision(num, precision?)` - Format with specific precision
58
58
  - `readableNumber(num, decimals?)` - Advanced readable formatting
59
59
  - `sum(data)` - Sum array of numbers
60
60
  - `sumBy(data, key)` - Sum by object property or function
package/dist/utils.cjs CHANGED
@@ -1 +1 @@
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;
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(typeof e=="function"?e():e)}catch(n){return r?.(n)}};async function w(e,r){const{errorMessage:n="Timeout",timeoutError:t,timeout:o=15e3}=r||{},s=typeof e=="function"?e():e,a=o>0?new Promise((u,l)=>setTimeout(()=>l(t||new Error(n)),o)):null;try{return Promise.race(a?[s,a]:[s])}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.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 常用操作的工具函数集合
3
3
  */
4
- import type { MaybeUndefined } from "./types";
4
+ import type { MaybePromise, MaybeUndefined } from "./types";
5
5
  /**
6
6
  * 延迟指定的时间间隔(毫秒)
7
7
  * @param interval - 延迟的毫秒数,默认 1500ms
@@ -70,10 +70,10 @@ export declare function isDomain(text: string): boolean;
70
70
  export declare function isEmail(text: string): boolean;
71
71
  /**
72
72
  * 异步 try-catch 包装器
73
- * @param p - 要执行的 Promise
73
+ * @param p - 要执行的 Promise | (() => any)
74
74
  * @param catchFn - 可选的错误处理函数
75
75
  */
76
- export declare const tryCatchAsync: <T, FR = any, F extends ((error: unknown) => FR) | undefined = undefined>(p: Promise<T>, catchFn?: F) => Promise<T | (F extends Function ? FR : undefined)>;
76
+ export declare const tryCatchAsync: <T, FR = any, F extends ((error: unknown) => FR) | undefined = undefined>(p: (() => MaybePromise<T>) | MaybePromise<T>, catchFn?: F) => Promise<T | (F extends Function ? FR : undefined)>;
77
77
  /**
78
78
  * 执行回调函数或 Promise,并确保在指定的超时时间内完成
79
79
  * 如果操作未能及时完成,返回的 Promise 将以超时错误被拒绝
package/dist/utils.js CHANGED
@@ -1,19 +1,19 @@
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) : "");
1
+ const m = (e = 1500) => new Promise((n) => setTimeout(n, e));
2
+ function h(e, n = 4, r = 4, t = "...") {
3
+ return !e || e.length <= n + r ? e : e.slice(0, n) + t + (r ? e.slice(-r) : "");
4
4
  }
5
5
  const a = (e) => e === void 0, y = (...e) => e.some(a), d = (e) => !!e && typeof e?.then == "function" && typeof e?.catch == "function";
6
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);
7
+ let n = "";
8
+ for (let r = 0; r < e.length; r++)
9
+ n += String.fromCharCode(e[r]);
10
+ return btoa(n);
11
11
  }
12
12
  function g(e) {
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;
13
+ const n = atob(e), r = new Uint8Array(n.length);
14
+ for (let t = 0; t < n.length; t++)
15
+ r[t] = n.charCodeAt(t);
16
+ return r;
17
17
  }
18
18
  function p(e) {
19
19
  return c(new Uint8Array(e));
@@ -21,34 +21,34 @@ function p(e) {
21
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 r = e.split(".");
25
- return !(r.length < 2 || r.some((n) => !n.length) || r[r.length - 1].length < 2);
24
+ const n = e.split(".");
25
+ return !(n.length < 2 || n.some((r) => !r.length) || n[n.length - 1].length < 2);
26
26
  }
27
27
  function w(e) {
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)
28
+ const [n, ...r] = e.toLowerCase().split("@");
29
+ if (!n || !r.length || !/^[a-z0-9._-]+$/.test(n) || /^\.|\.$/.test(n) || /[.\-_]{2,}/.test(n) || /[.\-_](?![a-z0-9])/.test(n)) return !1;
30
+ for (const t of r)
31
31
  if (!l(t)) return !1;
32
32
  return !0;
33
33
  }
34
- const P = async (e, r) => {
34
+ const P = async (e, n) => {
35
35
  try {
36
- return await e;
37
- } catch (n) {
38
- return r?.(n);
36
+ return await (typeof e == "function" ? e() : e);
37
+ } catch (r) {
38
+ return n?.(r);
39
39
  }
40
40
  };
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)
41
+ async function T(e, n) {
42
+ const { errorMessage: r = "Timeout", timeoutError: t, timeout: o = 15e3 } = n || {}, i = typeof e == "function" ? e() : e, s = o > 0 ? new Promise(
43
+ (u, f) => setTimeout(() => f(t || new Error(r)), o)
44
44
  ) : null;
45
45
  try {
46
- return Promise.race(i ? [s, i] : [s]);
46
+ return Promise.race(s ? [i, s] : [i]);
47
47
  } catch (u) {
48
48
  throw u;
49
49
  }
50
50
  }
51
- const A = (e, r, n) => e.some(a) ? n : r(...e);
51
+ const A = (e, n, r) => e.some(a) ? r : n(...e);
52
52
  export {
53
53
  p as arrayBufferToBase64,
54
54
  g as base64ToUint8Array,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlikemario/helper",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A utility library with number operations and common helper functions",
5
5
  "keywords": [
6
6
  "utility",