@nickyzj2023/utils 1.0.19 → 1.0.22
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 +4 -0
- package/dist/network.d.ts +17 -13
- package/dist/object.d.ts +2 -4
- package/dist/string.d.ts +4 -4
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/functions/camelToSnake.html +2 -2
- package/docs/functions/capitalize.html +2 -2
- package/docs/functions/decapitalize.html +2 -2
- 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 +2 -2
- 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 +1 -2
- package/src/dom.ts +1 -1
- package/src/hoc.ts +75 -73
- package/src/is.ts +21 -7
- package/src/lru-cache.ts +33 -33
- package/src/network.ts +72 -63
- package/src/object.ts +101 -103
- package/src/string.ts +4 -4
- package/src/time.ts +3 -3
package/src/hoc.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type CacheEntry = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
value: any;
|
|
3
|
+
expiresAt: number; // 时间戳(毫秒)
|
|
4
4
|
};
|
|
5
5
|
|
|
6
6
|
export type SetTtl = (seconds: number) => void;
|
|
@@ -40,76 +40,78 @@ export type SetTtl = (seconds: number) => void;
|
|
|
40
40
|
* await fetchData(urlB); // 使用缓存结果
|
|
41
41
|
*/
|
|
42
42
|
export const withCache = <Args extends any[], Result>(
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
fn: (this: { setTtl: SetTtl }, ...args: Args) => Result,
|
|
44
|
+
ttlSeconds = -1,
|
|
45
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
46
|
+
const cache = new Map<string, CacheEntry>();
|
|
47
|
+
|
|
48
|
+
const wrapped = (...args: Args): Result => {
|
|
49
|
+
const key = JSON.stringify(args);
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
const entry = cache.get(key);
|
|
52
|
+
|
|
53
|
+
// 命中缓存且未过期
|
|
54
|
+
if (entry && now < entry.expiresAt) {
|
|
55
|
+
return entry.value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let expiresAt = ttlSeconds === -1 ? Infinity : now + ttlSeconds * 1000;
|
|
59
|
+
|
|
60
|
+
// 创建上下文,包含 setTtl 方法
|
|
61
|
+
const thisArg = {
|
|
62
|
+
setTtl: (ttl: number) => {
|
|
63
|
+
expiresAt = now + ttl * 1000;
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const result = fn.apply(thisArg, args);
|
|
68
|
+
|
|
69
|
+
// 异步函数:缓存 Promise 的 resolved 值
|
|
70
|
+
if (result instanceof Promise) {
|
|
71
|
+
const promise = result.then((resolved) => {
|
|
72
|
+
cache.set(key, {
|
|
73
|
+
value: resolved,
|
|
74
|
+
expiresAt,
|
|
75
|
+
});
|
|
76
|
+
return resolved;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// 将 promise 先塞进去避免重复请求
|
|
80
|
+
cache.set(key, {
|
|
81
|
+
value: promise,
|
|
82
|
+
expiresAt,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return promise as Result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 同步函数缓存
|
|
89
|
+
cache.set(key, {
|
|
90
|
+
value: result,
|
|
91
|
+
expiresAt,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** 手动清除缓存 */
|
|
98
|
+
wrapped.clear = () => cache.clear();
|
|
99
|
+
|
|
100
|
+
/** 更新 TTL,同时刷新所有未过期缓存的时间 */
|
|
101
|
+
wrapped.updateTtl = (seconds: number) => {
|
|
102
|
+
// 更新默认 TTL
|
|
103
|
+
ttlSeconds = seconds;
|
|
104
|
+
|
|
105
|
+
// 给未过期缓存续期
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
const newExpiresAt = now + seconds * 1000;
|
|
108
|
+
for (const [key, entry] of cache.entries()) {
|
|
109
|
+
if (entry.expiresAt > now) {
|
|
110
|
+
entry.expiresAt = newExpiresAt;
|
|
111
|
+
cache.set(key, entry);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return wrapped;
|
|
115
117
|
};
|
package/src/is.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
export type Primitive =
|
|
1
|
+
export type Primitive =
|
|
2
|
+
| number
|
|
3
|
+
| string
|
|
4
|
+
| boolean
|
|
5
|
+
| symbol
|
|
6
|
+
| bigint
|
|
7
|
+
| undefined
|
|
8
|
+
| null;
|
|
2
9
|
|
|
3
10
|
/**
|
|
4
11
|
* 检测传入的值是否为**普通对象**
|
|
5
12
|
* @returns 如果是普通对象,返回 true,否则返回 false
|
|
6
13
|
*/
|
|
7
14
|
export const isObject = (value: any): value is object => {
|
|
8
|
-
|
|
15
|
+
return value?.constructor === Object;
|
|
9
16
|
};
|
|
10
17
|
|
|
11
18
|
/**
|
|
@@ -13,9 +20,16 @@ export const isObject = (value: any): value is object => {
|
|
|
13
20
|
* @returns 如果是原始值,返回 true,否则返回 false
|
|
14
21
|
*/
|
|
15
22
|
export const isPrimitive = (value: any): value is Primitive => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
return (
|
|
24
|
+
value === null ||
|
|
25
|
+
value === undefined ||
|
|
26
|
+
(typeof value !== "object" && typeof value !== "function")
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 检测传入的值是否为**false值**(false、0、''、null、undefined、NaN等)
|
|
32
|
+
*/
|
|
33
|
+
export const isFalsy = (value: any) => {
|
|
34
|
+
return !value;
|
|
21
35
|
};
|
package/src/lru-cache.ts
CHANGED
|
@@ -8,43 +8,43 @@
|
|
|
8
8
|
* cache.get("a"); // undefined
|
|
9
9
|
*/
|
|
10
10
|
class LRUCache<K, V> {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
private cache: Map<K, V>;
|
|
12
|
+
private maxSize: number;
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
constructor(maxSize = 10) {
|
|
15
|
+
this.cache = new Map();
|
|
16
|
+
this.maxSize = maxSize;
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
has(key: K) {
|
|
46
|
+
return this.cache.has(key);
|
|
47
|
+
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export default LRUCache;
|
package/src/network.ts
CHANGED
|
@@ -2,93 +2,102 @@ import { isObject } from "./is";
|
|
|
2
2
|
import { mergeObjects } from "./object";
|
|
3
3
|
|
|
4
4
|
export type RequestInit = BunFetchRequestInit & {
|
|
5
|
-
|
|
5
|
+
params?: Record<string, any>;
|
|
6
|
+
parser?: (response: Response) => Promise<any>;
|
|
6
7
|
};
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* 基于 Fetch API 的请求客户端
|
|
10
|
-
* @param baseURL
|
|
11
|
-
* @param
|
|
11
|
+
* @param baseURL 接口前缀
|
|
12
|
+
* @param baseOptions 客户端级别的请求体,后续调用时传递相同参数会覆盖上去
|
|
12
13
|
*
|
|
13
14
|
* @remarks
|
|
14
15
|
* 特性:
|
|
15
|
-
* -
|
|
16
|
-
* - 在
|
|
17
|
-
* -
|
|
16
|
+
* - 合并实例、调用时的相同请求体
|
|
17
|
+
* - 在 params 里传递对象,自动转换为 queryString
|
|
18
|
+
* - 在 body 里传递对象,自动 JSON.stringify
|
|
19
|
+
* - 可选择使用 to() 转换请求结果为 [Error, Response]
|
|
18
20
|
* - 可选择使用 withCache() 缓存请求结果
|
|
19
21
|
*
|
|
20
22
|
* @example
|
|
21
|
-
* // 用法1:创建客户端
|
|
22
|
-
* const api = fetcher("https://nickyzj.run:3030", { headers: { Authorization: "Bearer token" } });
|
|
23
|
-
* const res = await api.get<Blog>("/blogs/hello-world");
|
|
24
23
|
*
|
|
25
|
-
* // 用法
|
|
24
|
+
* // 用法1:直接发送请求
|
|
26
25
|
* const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
|
|
27
26
|
*
|
|
28
|
-
* //
|
|
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
|
+
* // 安全处理请求结果
|
|
29
32
|
* const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
|
|
30
33
|
* if (error) {
|
|
31
34
|
* console.error(error);
|
|
32
35
|
* return;
|
|
33
36
|
* }
|
|
37
|
+
* console.log(data);
|
|
34
38
|
*
|
|
35
39
|
* // 缓存请求结果
|
|
36
40
|
* const getBlogs = withCache(api.get);
|
|
37
41
|
* await getBlogs("/blogs");
|
|
38
|
-
* await sleep(
|
|
39
|
-
* await getBlogs("/blogs"); //
|
|
42
|
+
* await sleep();
|
|
43
|
+
* await getBlogs("/blogs"); // 不发请求,使用缓存
|
|
40
44
|
*/
|
|
41
|
-
export const fetcher = (baseURL = "",
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
+
);
|
|
48
55
|
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
// 转换 params 为查询字符串
|
|
57
|
+
if (isObject(params)) {
|
|
58
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
59
|
+
url.searchParams.append(key, value.toString());
|
|
60
|
+
});
|
|
61
|
+
}
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
+
}
|
|
60
71
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
72
|
+
// 发送请求
|
|
73
|
+
const response = await fetch(url, options);
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
throw new Error(response.statusText);
|
|
76
|
+
}
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
78
|
+
const data = await (parser?.(response) ?? response.json());
|
|
79
|
+
return data as T;
|
|
80
|
+
};
|
|
70
81
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
82
|
+
return {
|
|
83
|
+
get: <T>(url: string, options?: Omit<RequestInit, "method">) =>
|
|
84
|
+
myFetch<T>(url, { ...options, method: "GET" }),
|
|
74
85
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
86
|
+
post: <T>(
|
|
87
|
+
url: string,
|
|
88
|
+
body: any,
|
|
89
|
+
options?: Omit<RequestInit, "method" | "body">,
|
|
90
|
+
) => myFetch<T>(url, { ...options, method: "POST", body }),
|
|
80
91
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
put: <T>(
|
|
93
|
+
url: string,
|
|
94
|
+
body: any,
|
|
95
|
+
options?: Omit<RequestInit, "method" | "body">,
|
|
96
|
+
) => myFetch<T>(url, { ...options, method: "PUT", body }),
|
|
86
97
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
) => createRequest<T>(url, { ...options, method: "DELETE" }),
|
|
91
|
-
};
|
|
98
|
+
delete: <T>(url: string, options?: Omit<RequestInit, "method" | "body">) =>
|
|
99
|
+
myFetch<T>(url, { ...options, method: "DELETE" }),
|
|
100
|
+
};
|
|
92
101
|
};
|
|
93
102
|
|
|
94
103
|
/**
|
|
@@ -100,12 +109,12 @@ export const fetcher = (baseURL = "", defaultOptions: RequestInit = {}) => {
|
|
|
100
109
|
* const [error, response] = await to(fetcher().get<Blog>("/blogs/hello-world"));
|
|
101
110
|
*/
|
|
102
111
|
export const to = async <T, U = Error>(
|
|
103
|
-
|
|
112
|
+
promise: Promise<T>,
|
|
104
113
|
): Promise<[null, T] | [U, undefined]> => {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
114
|
+
try {
|
|
115
|
+
const response = await promise;
|
|
116
|
+
return [null, response];
|
|
117
|
+
} catch (error) {
|
|
118
|
+
return [error as U, undefined];
|
|
119
|
+
}
|
|
111
120
|
};
|