@nickyzj2023/utils 1.0.24 → 1.0.26
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/index.js +1 -1
- package/dist/is.d.ts +26 -3
- package/dist/network.d.ts +1 -1
- package/dist/string.d.ts +7 -0
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/functions/camelToSnake.html +1 -1
- package/docs/functions/capitalize.html +1 -1
- package/docs/functions/decapitalize.html +1 -1
- package/docs/functions/fetcher.html +1 -1
- package/docs/functions/imageUrlToBase64.html +178 -0
- package/docs/functions/isFalsy.html +5 -2
- package/docs/functions/isNil.html +4 -1
- package/docs/functions/isObject.html +4 -2
- package/docs/functions/isPrimitive.html +4 -2
- package/docs/functions/isTruthy.html +178 -0
- package/docs/functions/mapKeys.html +1 -1
- package/docs/functions/mapValues.html +1 -1
- package/docs/functions/mergeObjects.html +1 -1
- package/docs/functions/sleep.html +1 -1
- package/docs/functions/snakeToCamel.html +1 -1
- package/docs/functions/timeLog.html +1 -1
- package/docs/functions/to.html +4 -4
- package/docs/functions/withCache.html +3 -3
- package/docs/modules.html +1 -1
- package/docs/types/DeepMapKeys.html +1 -1
- package/docs/types/DeepMapValues.html +1 -1
- package/docs/types/Falsy.html +1 -1
- package/docs/types/Primitive.html +1 -1
- package/docs/types/RequestInit.html +1 -1
- package/docs/types/SetTtl.html +1 -1
- package/package.json +1 -1
- package/src/is.ts +29 -3
- package/src/network.ts +10 -6
- package/src/string.ts +17 -0
package/package.json
CHANGED
package/src/is.ts
CHANGED
|
@@ -11,7 +11,10 @@ export type Falsy = false | 0 | -0 | 0n | "" | null | undefined;
|
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* 检测传入的值是否为**普通对象**
|
|
14
|
-
*
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const obj = { a: 1 };
|
|
17
|
+
* isObject(obj); // true
|
|
15
18
|
*/
|
|
16
19
|
export const isObject = (value: any): value is object => {
|
|
17
20
|
return value?.constructor === Object;
|
|
@@ -19,7 +22,10 @@ export const isObject = (value: any): value is object => {
|
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* 检测传入的值是否为**原始值**(number、string、boolean、symbol、bigint、undefined、null)
|
|
22
|
-
*
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* isPrimitive(1); // true
|
|
28
|
+
* isPrimitive([]); // false
|
|
23
29
|
*/
|
|
24
30
|
export const isPrimitive = (value: any): value is Primitive => {
|
|
25
31
|
return (
|
|
@@ -30,14 +36,34 @@ export const isPrimitive = (value: any): value is Primitive => {
|
|
|
30
36
|
};
|
|
31
37
|
|
|
32
38
|
/**
|
|
33
|
-
*
|
|
39
|
+
* 检测传入的值是否为**假值**(false、0、''、null、undefined、NaN等)
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* isFalsy(""); // true
|
|
43
|
+
* isFalsy(1); // false
|
|
34
44
|
*/
|
|
35
45
|
export const isFalsy = (value: any): value is Falsy => {
|
|
36
46
|
return !value;
|
|
37
47
|
};
|
|
38
48
|
|
|
49
|
+
/**
|
|
50
|
+
* 检测传入的值是否为**真值**
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* isTruthy(1); // true
|
|
54
|
+
* isTruthy(""); // false
|
|
55
|
+
*/
|
|
56
|
+
export const isTruthy = (value: any): value is any => {
|
|
57
|
+
return !!value;
|
|
58
|
+
};
|
|
59
|
+
|
|
39
60
|
/**
|
|
40
61
|
* 检测传入的值是否为**空值**(null、undefined)
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* isNil(null); // true
|
|
65
|
+
* isNil(undefined); // true
|
|
66
|
+
* isNil(1); // false
|
|
41
67
|
*/
|
|
42
68
|
export const isNil = (value: any): value is null | undefined => {
|
|
43
69
|
return value === null || value === undefined;
|
package/src/network.ts
CHANGED
|
@@ -75,6 +75,11 @@ export const fetcher = (baseURL = "", baseOptions: RequestInit = {}) => {
|
|
|
75
75
|
// 发送请求
|
|
76
76
|
const response = await fetch(url, options);
|
|
77
77
|
if (!response.ok) {
|
|
78
|
+
// 如果后端给了报错详情,则先解析再抛出
|
|
79
|
+
const contentType = response.headers.get("Content-Type");
|
|
80
|
+
if (contentType?.startsWith("application/json")) {
|
|
81
|
+
throw await response.json();
|
|
82
|
+
}
|
|
78
83
|
throw new Error(response.statusText);
|
|
79
84
|
}
|
|
80
85
|
|
|
@@ -111,13 +116,12 @@ export const fetcher = (baseURL = "", baseOptions: RequestInit = {}) => {
|
|
|
111
116
|
* @example
|
|
112
117
|
* const [error, response] = await to(fetcher().get<Blog>("/blogs/hello-world"));
|
|
113
118
|
*/
|
|
114
|
-
export const to = async <T,
|
|
119
|
+
export const to = async <T, E = Error>(
|
|
115
120
|
promise: Promise<T>,
|
|
116
|
-
): Promise<[null, T] | [
|
|
121
|
+
): Promise<[null, T] | [E, undefined]> => {
|
|
117
122
|
try {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return [error as U, undefined];
|
|
123
|
+
return [null, await promise];
|
|
124
|
+
} catch (e) {
|
|
125
|
+
return [e as E, undefined];
|
|
122
126
|
}
|
|
123
127
|
};
|
package/src/string.ts
CHANGED
|
@@ -40,3 +40,20 @@ export const capitalize = (s: string) => {
|
|
|
40
40
|
export const decapitalize = (s: string) => {
|
|
41
41
|
return s.charAt(0).toLowerCase() + s.slice(1);
|
|
42
42
|
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 图片地址转 base64 数据
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* imageUrlToBase64("https://example.com/image.jpg"); // "data:image/jpeg;base64,..."
|
|
49
|
+
*/
|
|
50
|
+
export const imageUrlToBase64 = async (imageUrl: string) => {
|
|
51
|
+
if (!imageUrl.startsWith("http")) {
|
|
52
|
+
throw new Error("图片地址必须以http或https开头");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const response = await fetch(imageUrl);
|
|
56
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
57
|
+
const base64 = Buffer.from(arrayBuffer).toString("base64");
|
|
58
|
+
return `data:image/jpeg;base64,${base64}`;
|
|
59
|
+
};
|