@ceale/util 1.12.0 → 1.12.1

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/cjs/index.js CHANGED
@@ -32,6 +32,7 @@ __export(exports_src, {
32
32
  waitSync: () => waitSync,
33
33
  wait: () => wait,
34
34
  uri: () => uri,
35
+ tryCatch: () => tryCatch,
35
36
  throttle: () => throttle,
36
37
  sleepAsync: () => sleepAsync,
37
38
  sleep: () => sleep,
@@ -409,3 +410,17 @@ class CubicBezier {
409
410
  }
410
411
  }
411
412
  }
413
+ // src/error.ts
414
+ var tryCatch = (parameter) => {
415
+ if (parameter instanceof Promise) {
416
+ return parameter.then((data) => ({ data, error: null })).catch((error) => ({ data: null, error }));
417
+ }
418
+ if (typeof parameter === "function") {
419
+ try {
420
+ return { data: parameter(), error: null };
421
+ } catch (error) {
422
+ return { data: null, error };
423
+ }
424
+ }
425
+ throw new TypeError("参数类型错误,必须是 Promise 或函数");
426
+ };
package/dist/esm/index.js CHANGED
@@ -363,10 +363,25 @@ class CubicBezier {
363
363
  }
364
364
  }
365
365
  }
366
+ // src/error.ts
367
+ var tryCatch = (parameter) => {
368
+ if (parameter instanceof Promise) {
369
+ return parameter.then((data) => ({ data, error: null })).catch((error) => ({ data: null, error }));
370
+ }
371
+ if (typeof parameter === "function") {
372
+ try {
373
+ return { data: parameter(), error: null };
374
+ } catch (error) {
375
+ return { data: null, error };
376
+ }
377
+ }
378
+ throw new TypeError("参数类型错误,必须是 Promise 或函数");
379
+ };
366
380
  export {
367
381
  waitSync,
368
382
  wait,
369
383
  uri,
384
+ tryCatch,
370
385
  throttle,
371
386
  sleepAsync,
372
387
  sleep,
@@ -1 +1,15 @@
1
+ type Success<T> = {
2
+ data: T;
3
+ error: null;
4
+ };
5
+ type Failure<E> = {
6
+ data: null;
7
+ error: E;
8
+ };
9
+ type Result<T, E extends unknown = Error> = Success<T> | Failure<E>;
10
+ interface tryCatch {
11
+ <T>(arg: Promise<T>): Promise<Result<T>>;
12
+ <V>(arg: (() => V)): Result<V>;
13
+ }
14
+ export declare const tryCatch: tryCatch;
1
15
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ceale/util",
3
3
  "author": "Ceale",
4
- "version": "1.12.0",
4
+ "version": "1.12.1",
5
5
  "module": "index.ts",
6
6
  "type": "module",
7
7
  "main": "dist/esm/index.js",