@ceale/util 1.17.0 → 1.18.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/README.md +1 -0
- package/dist/cjs/index.js +10 -8
- package/dist/esm/index.js +10 -8
- package/dist/types/class.d.ts +1 -1
- package/dist/types/error.d.ts +19 -7
- package/dist/types/type.d.ts +10 -6
- package/dist/types/uri.d.ts +6 -5
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/cjs/index.js
CHANGED
|
@@ -414,19 +414,21 @@ var CubicBezier = class {
|
|
|
414
414
|
};
|
|
415
415
|
|
|
416
416
|
// src/error.ts
|
|
417
|
-
var tryCatch = (
|
|
418
|
-
if (typeof
|
|
419
|
-
return parameter.then((data) => Object.assign([data, null], { data, error: null })).catch((error) => Object.assign([null, error], { data: null, error }));
|
|
420
|
-
}
|
|
421
|
-
if (typeof parameter === "function") {
|
|
417
|
+
var tryCatch = (func, catchClass) => {
|
|
418
|
+
if (typeof func === "function") {
|
|
422
419
|
try {
|
|
423
|
-
const data =
|
|
420
|
+
const data = func();
|
|
424
421
|
return Object.assign([data, null], { data, error: null });
|
|
425
422
|
} catch (error) {
|
|
426
|
-
return Object.assign([null, error], { data: null, error });
|
|
423
|
+
if (!catchClass || error instanceof catchClass) return Object.assign([null, error], { data: null, error });
|
|
424
|
+
throw error;
|
|
427
425
|
}
|
|
426
|
+
} else {
|
|
427
|
+
return func.then((data) => Object.assign([data, null], { data, error: null })).catch((error) => {
|
|
428
|
+
if (!catchClass || error instanceof catchClass) return Object.assign([null, error], { data: null, error });
|
|
429
|
+
throw error;
|
|
430
|
+
});
|
|
428
431
|
}
|
|
429
|
-
throw new TypeError("\u53C2\u6570\u7C7B\u578B\u9519\u8BEF\uFF0C\u5E94\u4E3A Promise \u6216\u51FD\u6570");
|
|
430
432
|
};
|
|
431
433
|
|
|
432
434
|
// src/type.ts
|
package/dist/esm/index.js
CHANGED
|
@@ -372,19 +372,21 @@ var CubicBezier = class {
|
|
|
372
372
|
};
|
|
373
373
|
|
|
374
374
|
// src/error.ts
|
|
375
|
-
var tryCatch = (
|
|
376
|
-
if (typeof
|
|
377
|
-
return parameter.then((data) => Object.assign([data, null], { data, error: null })).catch((error) => Object.assign([null, error], { data: null, error }));
|
|
378
|
-
}
|
|
379
|
-
if (typeof parameter === "function") {
|
|
375
|
+
var tryCatch = (func, catchClass) => {
|
|
376
|
+
if (typeof func === "function") {
|
|
380
377
|
try {
|
|
381
|
-
const data =
|
|
378
|
+
const data = func();
|
|
382
379
|
return Object.assign([data, null], { data, error: null });
|
|
383
380
|
} catch (error) {
|
|
384
|
-
return Object.assign([null, error], { data: null, error });
|
|
381
|
+
if (!catchClass || error instanceof catchClass) return Object.assign([null, error], { data: null, error });
|
|
382
|
+
throw error;
|
|
385
383
|
}
|
|
384
|
+
} else {
|
|
385
|
+
return func.then((data) => Object.assign([data, null], { data, error: null })).catch((error) => {
|
|
386
|
+
if (!catchClass || error instanceof catchClass) return Object.assign([null, error], { data: null, error });
|
|
387
|
+
throw error;
|
|
388
|
+
});
|
|
386
389
|
}
|
|
387
|
-
throw new TypeError("\u53C2\u6570\u7C7B\u578B\u9519\u8BEF\uFF0C\u5E94\u4E3A Promise \u6216\u51FD\u6570");
|
|
388
390
|
};
|
|
389
391
|
|
|
390
392
|
// src/type.ts
|
package/dist/types/class.d.ts
CHANGED
package/dist/types/error.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AnyClass } from "./type";
|
|
1
2
|
type Success<T> = {
|
|
2
3
|
data: T;
|
|
3
4
|
error: null;
|
|
@@ -12,28 +13,39 @@ type Failure<E> = {
|
|
|
12
13
|
data: null,
|
|
13
14
|
error: E
|
|
14
15
|
];
|
|
15
|
-
type Result<T, E
|
|
16
|
+
type Result<T, E = any> = Success<T> | Failure<E>;
|
|
16
17
|
interface tryCatch {
|
|
17
|
-
|
|
18
|
-
<
|
|
18
|
+
/** 捕获所有 */
|
|
19
|
+
<T>(func: (() => T)): Result<T>;
|
|
20
|
+
<T>(asyncFunc: Promise<T>): Promise<Result<T>>;
|
|
21
|
+
/** 捕获指定 */
|
|
22
|
+
<T, E extends AnyClass>(func: (() => T), catchClass: E): Result<T, InstanceType<E>>;
|
|
23
|
+
<T, E extends AnyClass>(func: Promise<T>, catchClass: E): Promise<Result<T, InstanceType<E>>>;
|
|
19
24
|
}
|
|
20
25
|
/**
|
|
21
|
-
* 尝试执行一个函数或Promise
|
|
26
|
+
* 尝试执行一个函数或Promise,返回一个对象,其中包含执行结果或被捕获的错误
|
|
22
27
|
*
|
|
23
28
|
* 该对象同时支持对象属性访问(.data/.error)和数组索引访问([0]/[1])两种方式
|
|
24
29
|
*
|
|
25
|
-
* @param
|
|
26
|
-
* @
|
|
30
|
+
* @param func 一个函数或者Promise对象
|
|
31
|
+
* @param catchClass 要捕获的错误类型(含子类),如果不指定,则捕获所有错误;如指定,则为被捕获的错误将继续上抛
|
|
32
|
+
* @returns 返回一个如后方例的对象`{ data, error } & [ data, error ]`
|
|
27
33
|
*
|
|
28
34
|
* @example
|
|
29
35
|
* tryCatch(() => func(...))
|
|
30
36
|
* // => [data, error] & {data, error}
|
|
31
37
|
* // data: func的返回值,error:函数执行过程中发生的错误
|
|
32
38
|
*
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* tryCatch(() => func(...), TypeError)
|
|
42
|
+
* // => [data, error] & {data, error}
|
|
43
|
+
* // data: func的返回值,error:函数执行过程中,发生的TypeError或其子类错误
|
|
44
|
+
*
|
|
33
45
|
* @example
|
|
34
46
|
* await tryCatch(asyncFunc(...))
|
|
35
47
|
* // => [data, error] & {data, error}
|
|
36
|
-
* // data: asyncFunc的返回值,error:asyncFunc执行过程中发生的错误
|
|
48
|
+
* // data: 异步函数asyncFunc的返回值,error:asyncFunc执行过程中发生的错误
|
|
37
49
|
*/
|
|
38
50
|
export declare const tryCatch: tryCatch;
|
|
39
51
|
export {};
|
package/dist/types/type.d.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一种介于 `any` 与 `object` 之间的类型
|
|
3
|
+
* - 与 `any` 类似,可以任意访问属性
|
|
4
|
+
* - 与 `object` 类似,排除了原始类型
|
|
5
|
+
*/
|
|
6
|
+
export type anyobject = Record<any, any>;
|
|
7
|
+
/**
|
|
8
|
+
* 表示任意的类或构造函数
|
|
9
|
+
*/
|
|
10
|
+
export type AnyClass = new (...args: any[]) => any;
|
|
1
11
|
/**
|
|
2
12
|
* 就地断言一个变量为指定类型
|
|
3
13
|
* @template Type 断言的类型,默认为`any`
|
|
@@ -12,12 +22,6 @@ export declare const assert: <Type = any>(variable: any) => asserts variable is
|
|
|
12
22
|
* @returns 传入的变量,且类型被拓展为指定类型
|
|
13
23
|
*/
|
|
14
24
|
export declare const expand: <Type>(variable: any) => asserts variable is (typeof variable & Type);
|
|
15
|
-
/**
|
|
16
|
-
* 一种介于 `any` 与 `object` 之间的类型
|
|
17
|
-
* - 与 `any` 类似,可以任意访问属性
|
|
18
|
-
* - 与 `object` 类似,排除了原始类型
|
|
19
|
-
*/
|
|
20
|
-
export type anyobject = Record<any, any>;
|
|
21
25
|
/**
|
|
22
26
|
* 定义一个枚举类型。
|
|
23
27
|
* 返回一个枚举对象,其中每个键名与值相同。
|
package/dist/types/uri.d.ts
CHANGED
|
@@ -10,15 +10,16 @@ export declare namespace uri {
|
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* // 基础拼接
|
|
13
|
-
* uri.join('/a/', 'b', '/c')
|
|
13
|
+
* uri.join('/a/', 'b', '/c')
|
|
14
|
+
* // "/a/b/c"
|
|
14
15
|
*
|
|
15
|
-
* @example
|
|
16
16
|
* // 路径解析
|
|
17
|
-
* uri.join('/a/b/../c')
|
|
17
|
+
* uri.join('/a/b/../c')
|
|
18
|
+
* // "/a/c"
|
|
18
19
|
*
|
|
19
|
-
* @example
|
|
20
20
|
* // 保留协议头
|
|
21
|
-
* uri.join("http://example.com", "a")
|
|
21
|
+
* uri.join("http://example.com", "a")
|
|
22
|
+
* // "http://example.com/a"
|
|
22
23
|
*/
|
|
23
24
|
const join: (...path: string[]) => string;
|
|
24
25
|
}
|