@nickyzj2023/utils 1.0.18 → 1.0.21
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 +35 -35
- package/biome.json +40 -0
- package/dist/index.js +1 -1
- package/dist/is.d.ts +4 -0
- package/dist/network.d.ts +17 -13
- package/dist/object.d.ts +2 -4
- package/dist/string.d.ts +18 -4
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/functions/camelToSnake.html +3 -3
- package/docs/functions/capitalize.html +178 -0
- package/docs/functions/decapitalize.html +178 -0
- package/docs/functions/fetcher.html +9 -8
- package/docs/functions/isFalsy.html +175 -0
- package/docs/functions/isObject.html +1 -1
- package/docs/functions/isPrimitive.html +1 -1
- package/docs/functions/mapKeys.html +1 -1
- package/docs/functions/mapValues.html +1 -1
- package/docs/functions/mergeObjects.html +3 -5
- package/docs/functions/sleep.html +1 -1
- package/docs/functions/snakeToCamel.html +3 -3
- package/docs/functions/timeLog.html +1 -1
- package/docs/functions/to.html +1 -1
- 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/Primitive.html +1 -1
- package/docs/types/RequestInit.html +1 -1
- package/docs/types/SetTtl.html +1 -1
- package/package.json +2 -2
- package/src/dom.ts +10 -10
- package/src/hoc.ts +117 -115
- package/src/index.ts +7 -7
- package/src/is.ts +35 -21
- package/src/lru-cache.ts +50 -50
- package/src/network.ts +120 -111
- package/src/object.ts +163 -165
- package/src/string.ts +42 -22
- package/src/time.ts +11 -11
- package/tsconfig.json +32 -32
package/src/lru-cache.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 简易 LRU 缓存
|
|
3
|
-
* @example
|
|
4
|
-
* const cache = new LRUCache<string, number>(2);
|
|
5
|
-
* cache.set("a", 1);
|
|
6
|
-
* cache.set("b", 2);
|
|
7
|
-
* cache.set("c", 3); // 缓存已满,a 被淘汰
|
|
8
|
-
* cache.get("a"); // undefined
|
|
9
|
-
*/
|
|
10
|
-
class LRUCache<K, V> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export default LRUCache;
|
|
1
|
+
/**
|
|
2
|
+
* 简易 LRU 缓存
|
|
3
|
+
* @example
|
|
4
|
+
* const cache = new LRUCache<string, number>(2);
|
|
5
|
+
* cache.set("a", 1);
|
|
6
|
+
* cache.set("b", 2);
|
|
7
|
+
* cache.set("c", 3); // 缓存已满,a 被淘汰
|
|
8
|
+
* cache.get("a"); // undefined
|
|
9
|
+
*/
|
|
10
|
+
class LRUCache<K, V> {
|
|
11
|
+
private cache: Map<K, V>;
|
|
12
|
+
private maxSize: number;
|
|
13
|
+
|
|
14
|
+
constructor(maxSize = 10) {
|
|
15
|
+
this.cache = new Map();
|
|
16
|
+
this.maxSize = maxSize;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get(key: K): V | undefined {
|
|
20
|
+
const value = this.cache.get(key);
|
|
21
|
+
if (!value) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
// 重置缓存顺序
|
|
25
|
+
this.cache.delete(key);
|
|
26
|
+
this.cache.set(key, value);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
set(key: K, value: V) {
|
|
31
|
+
// 刷新缓存
|
|
32
|
+
if (this.cache.has(key)) {
|
|
33
|
+
this.cache.delete(key);
|
|
34
|
+
}
|
|
35
|
+
// 删除最旧的缓存
|
|
36
|
+
else if (this.cache.size >= this.maxSize) {
|
|
37
|
+
const oldestKey = [...this.cache.keys()][0];
|
|
38
|
+
if (oldestKey) {
|
|
39
|
+
this.cache.delete(oldestKey);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.cache.set(key, value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
has(key: K) {
|
|
46
|
+
return this.cache.has(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default LRUCache;
|
package/src/network.ts
CHANGED
|
@@ -1,111 +1,120 @@
|
|
|
1
|
-
import { isObject } from "./is";
|
|
2
|
-
import { mergeObjects } from "./object";
|
|
3
|
-
|
|
4
|
-
export type RequestInit = BunFetchRequestInit & {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*
|
|
11
|
-
* @param
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* -
|
|
17
|
-
* -
|
|
18
|
-
* -
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* const
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
1
|
+
import { isObject } from "./is";
|
|
2
|
+
import { mergeObjects } from "./object";
|
|
3
|
+
|
|
4
|
+
export type RequestInit = BunFetchRequestInit & {
|
|
5
|
+
params?: Record<string, string>;
|
|
6
|
+
parser?: (response: Response) => Promise<any>;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 基于 Fetch API 的请求客户端
|
|
11
|
+
* @param baseURL 接口前缀
|
|
12
|
+
* @param baseOptions 客户端级别的请求体,后续调用时传递相同参数会覆盖上去
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* 特性:
|
|
16
|
+
* - 合并实例、调用时的相同请求体
|
|
17
|
+
* - 在 params 里传递对象,自动转换为 queryString
|
|
18
|
+
* - 在 body 里传递对象,自动 JSON.stringify
|
|
19
|
+
* - 可选择使用 to() 转换请求结果为 [Error, Response]
|
|
20
|
+
* - 可选择使用 withCache() 缓存请求结果
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
*
|
|
24
|
+
* // 用法1:直接发送请求
|
|
25
|
+
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
|
|
26
|
+
*
|
|
27
|
+
* // 用法2:创建实例
|
|
28
|
+
* const api = fetcher("https://nickyzj.run:3030", { headers: { Authorization: "Bearer token" } });
|
|
29
|
+
* const res = await api.get<Blog>("/blogs/hello-world", { headers: {...}, params: { page: 1 } }); // 与实例相同的 headers 会覆盖上去,params 会转成 ?page=1 跟到 url 后面
|
|
30
|
+
*
|
|
31
|
+
* // 安全处理请求结果
|
|
32
|
+
* const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
|
|
33
|
+
* if (error) {
|
|
34
|
+
* console.error(error);
|
|
35
|
+
* return;
|
|
36
|
+
* }
|
|
37
|
+
* console.log(data);
|
|
38
|
+
*
|
|
39
|
+
* // 缓存请求结果
|
|
40
|
+
* const getBlogs = withCache(api.get);
|
|
41
|
+
* await getBlogs("/blogs");
|
|
42
|
+
* await sleep();
|
|
43
|
+
* await getBlogs("/blogs"); // 不发请求,使用缓存
|
|
44
|
+
*/
|
|
45
|
+
export const fetcher = (baseURL = "", baseOptions: RequestInit = {}) => {
|
|
46
|
+
const myFetch = async <T>(path: string, requestOptions: RequestInit = {}) => {
|
|
47
|
+
// 构建完整 URL
|
|
48
|
+
const url = new URL(baseURL ? `${baseURL}${path}` : path);
|
|
49
|
+
|
|
50
|
+
// 合并 options
|
|
51
|
+
const { params, parser, ...options } = mergeObjects(
|
|
52
|
+
baseOptions,
|
|
53
|
+
requestOptions,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
// 转换 params 为查询字符串
|
|
57
|
+
if (isObject(params)) {
|
|
58
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
59
|
+
url.searchParams.append(key, value.toString());
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 转换 body 为字符串
|
|
64
|
+
if (isObject(options.body)) {
|
|
65
|
+
options.body = JSON.stringify(options.body);
|
|
66
|
+
options.headers = {
|
|
67
|
+
...options.headers,
|
|
68
|
+
"Content-Type": "application/json",
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 发送请求
|
|
73
|
+
const response = await fetch(url, options);
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
throw new Error(response.statusText);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const data = await (parser?.(response) ?? response.json());
|
|
79
|
+
return data as T;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
get: <T>(url: string, options?: Omit<RequestInit, "method">) =>
|
|
84
|
+
myFetch<T>(url, { ...options, method: "GET" }),
|
|
85
|
+
|
|
86
|
+
post: <T>(
|
|
87
|
+
url: string,
|
|
88
|
+
body: any,
|
|
89
|
+
options?: Omit<RequestInit, "method" | "body">,
|
|
90
|
+
) => myFetch<T>(url, { ...options, method: "POST", body }),
|
|
91
|
+
|
|
92
|
+
put: <T>(
|
|
93
|
+
url: string,
|
|
94
|
+
body: any,
|
|
95
|
+
options?: Omit<RequestInit, "method" | "body">,
|
|
96
|
+
) => myFetch<T>(url, { ...options, method: "PUT", body }),
|
|
97
|
+
|
|
98
|
+
delete: <T>(url: string, options?: Omit<RequestInit, "method" | "body">) =>
|
|
99
|
+
myFetch<T>(url, { ...options, method: "DELETE" }),
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Go 语言风格的异步处理方式
|
|
105
|
+
* @param promise 一个能被 await 的异步函数
|
|
106
|
+
* @returns 如果成功,返回 [null, 异步函数结果],否则返回 [Error, undefined]
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* const [error, response] = await to(fetcher().get<Blog>("/blogs/hello-world"));
|
|
110
|
+
*/
|
|
111
|
+
export const to = async <T, U = Error>(
|
|
112
|
+
promise: Promise<T>,
|
|
113
|
+
): Promise<[null, T] | [U, undefined]> => {
|
|
114
|
+
try {
|
|
115
|
+
const response = await promise;
|
|
116
|
+
return [null, response];
|
|
117
|
+
} catch (error) {
|
|
118
|
+
return [error as U, undefined];
|
|
119
|
+
}
|
|
120
|
+
};
|