@nickyzj2023/utils 1.0.55 → 1.0.57

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.
Files changed (129) hide show
  1. package/README.md +1 -3
  2. package/dist/index.d.ts +446 -9
  3. package/dist/index.js +2 -3
  4. package/package.json +15 -4
  5. package/AGENTS.md +0 -167
  6. package/biome.json +0 -37
  7. package/dist/dom/index.d.ts +0 -1
  8. package/dist/dom/log.d.ts +0 -28
  9. package/dist/dom/timeLog.d.ts +0 -8
  10. package/dist/function/index.d.ts +0 -1
  11. package/dist/function/loop-until.d.ts +0 -36
  12. package/dist/function/loopUntil.d.ts +0 -22
  13. package/dist/hoc/index.d.ts +0 -1
  14. package/dist/hoc/with-cache.d.ts +0 -42
  15. package/dist/hoc/withCache.d.ts +0 -42
  16. package/dist/is/index.d.ts +0 -4
  17. package/dist/is/is-falsy.d.ts +0 -9
  18. package/dist/is/is-nil.d.ts +0 -9
  19. package/dist/is/is-object.d.ts +0 -8
  20. package/dist/is/is-primitive.d.ts +0 -9
  21. package/dist/is/isFalsy.d.ts +0 -9
  22. package/dist/is/isNil.d.ts +0 -9
  23. package/dist/is/isObject.d.ts +0 -8
  24. package/dist/is/isPrimitive.d.ts +0 -9
  25. package/dist/is/isTruthy.d.ts +0 -8
  26. package/dist/lru-cache.d.ts +0 -18
  27. package/dist/network/fetcher.d.ts +0 -56
  28. package/dist/network/get-real-url.d.ts +0 -2
  29. package/dist/network/getRealURL.d.ts +0 -2
  30. package/dist/network/image.d.ts +0 -59
  31. package/dist/network/index.d.ts +0 -4
  32. package/dist/network/to.d.ts +0 -9
  33. package/dist/number/index.d.ts +0 -1
  34. package/dist/number/random-int.d.ts +0 -7
  35. package/dist/number/randomInt.d.ts +0 -7
  36. package/dist/object/index.d.ts +0 -3
  37. package/dist/object/map-keys.d.ts +0 -17
  38. package/dist/object/map-values.d.ts +0 -21
  39. package/dist/object/mapKeys.d.ts +0 -17
  40. package/dist/object/mapValues.d.ts +0 -21
  41. package/dist/object/merge-objects.d.ts +0 -12
  42. package/dist/object/mergeObjects.d.ts +0 -12
  43. package/dist/string/case.d.ts +0 -32
  44. package/dist/string/compact.d.ts +0 -22
  45. package/dist/string/index.d.ts +0 -2
  46. package/dist/time/debounce.d.ts +0 -20
  47. package/dist/time/index.d.ts +0 -3
  48. package/dist/time/sleep.d.ts +0 -7
  49. package/dist/time/throttle.d.ts +0 -20
  50. package/docs/.nojekyll +0 -1
  51. package/docs/assets/hierarchy.js +0 -1
  52. package/docs/assets/highlight.css +0 -92
  53. package/docs/assets/icons.js +0 -18
  54. package/docs/assets/icons.svg +0 -1
  55. package/docs/assets/main.js +0 -60
  56. package/docs/assets/material-style.css +0 -262
  57. package/docs/assets/navigation.js +0 -1
  58. package/docs/assets/search.js +0 -1
  59. package/docs/assets/style.css +0 -1633
  60. package/docs/functions/camelToSnake.html +0 -178
  61. package/docs/functions/capitalize.html +0 -178
  62. package/docs/functions/compactStr.html +0 -182
  63. package/docs/functions/debounce.html +0 -187
  64. package/docs/functions/decapitalize.html +0 -178
  65. package/docs/functions/fetcher.html +0 -189
  66. package/docs/functions/getRealURL.html +0 -175
  67. package/docs/functions/imageUrlToBase64.html +0 -191
  68. package/docs/functions/isFalsy.html +0 -178
  69. package/docs/functions/isNil.html +0 -178
  70. package/docs/functions/isObject.html +0 -178
  71. package/docs/functions/isPrimitive.html +0 -178
  72. package/docs/functions/log.html +0 -180
  73. package/docs/functions/loopUntil.html +0 -186
  74. package/docs/functions/mapKeys.html +0 -180
  75. package/docs/functions/mapValues.html +0 -182
  76. package/docs/functions/mergeObjects.html +0 -184
  77. package/docs/functions/randomInt.html +0 -178
  78. package/docs/functions/sleep.html +0 -179
  79. package/docs/functions/snakeToCamel.html +0 -178
  80. package/docs/functions/throttle.html +0 -187
  81. package/docs/functions/to.html +0 -180
  82. package/docs/functions/withCache.html +0 -185
  83. package/docs/hierarchy.html +0 -174
  84. package/docs/index.html +0 -184
  85. package/docs/interfaces/LogOptions.html +0 -185
  86. package/docs/modules.html +0 -174
  87. package/docs/types/BunFetchOptions.html +0 -176
  88. package/docs/types/CamelToSnake.html +0 -174
  89. package/docs/types/Capitalize.html +0 -174
  90. package/docs/types/Decapitalize.html +0 -174
  91. package/docs/types/DeepMapKeys.html +0 -174
  92. package/docs/types/DeepMapValues.html +0 -174
  93. package/docs/types/Falsy.html +0 -174
  94. package/docs/types/ImageCompressionOptions.html +0 -188
  95. package/docs/types/Primitive.html +0 -174
  96. package/docs/types/RequestInit.html +0 -174
  97. package/docs/types/SetTtl.html +0 -174
  98. package/docs/types/SnakeToCamel.html +0 -174
  99. package/src/dom/index.ts +0 -1
  100. package/src/dom/log.ts +0 -72
  101. package/src/function/index.ts +0 -1
  102. package/src/function/loop-until.ts +0 -58
  103. package/src/hoc/index.ts +0 -1
  104. package/src/hoc/with-cache.ts +0 -117
  105. package/src/index.ts +0 -9
  106. package/src/is/index.ts +0 -4
  107. package/src/is/is-falsy.ts +0 -12
  108. package/src/is/is-nil.ts +0 -11
  109. package/src/is/is-object.ts +0 -10
  110. package/src/is/is-primitive.ts +0 -23
  111. package/src/network/fetcher.ts +0 -124
  112. package/src/network/get-real-url.ts +0 -18
  113. package/src/network/image.ts +0 -202
  114. package/src/network/index.ts +0 -4
  115. package/src/network/to.ts +0 -17
  116. package/src/number/index.ts +0 -1
  117. package/src/number/random-int.ts +0 -9
  118. package/src/object/index.ts +0 -3
  119. package/src/object/map-keys.ts +0 -50
  120. package/src/object/map-values.ts +0 -77
  121. package/src/object/merge-objects.ts +0 -50
  122. package/src/string/case.ts +0 -71
  123. package/src/string/compact.ts +0 -56
  124. package/src/string/index.ts +0 -2
  125. package/src/time/debounce.ts +0 -34
  126. package/src/time/index.ts +0 -3
  127. package/src/time/sleep.ts +0 -11
  128. package/src/time/throttle.ts +0 -34
  129. package/tsconfig.json +0 -32
package/AGENTS.md DELETED
@@ -1,167 +0,0 @@
1
- # AGENTS.md
2
-
3
- This file contains essential information for AI coding agents working on this project.
4
-
5
- ## Project Overview
6
-
7
- 这是一个前端工具库(Frontend Utility Library),提供常用的 TypeScript/JavaScript 工具函数,帮助开发者简化日常开发任务。项目使用 Bun 作为运行时和包管理器,发布到 npm 仓库 `@nickyzj2023/utils`。
8
-
9
- ## Technology Stack
10
-
11
- - **Runtime**: [Bun](https://bun.com) v1.3.2+ - 快速的 JavaScript 运行时和包管理器
12
- - **Language**: TypeScript 5.9.3+
13
- - **Build Tool**: Bun 内置打包器 + TypeScript 编译器
14
- - **Linting/Formatting**: Biome 2.3.14
15
- - **Documentation**: TypeDoc 0.28.16 + Material Theme
16
- - **Dependencies**: 零运行时依赖,仅开发依赖
17
-
18
- ## Project Structure
19
-
20
- ```
21
- .
22
- ├── src/ # 源代码目录
23
- │ ├── index.ts # 入口文件,导出所有模块
24
- │ ├── dom/ # DOM 相关工具
25
- │ │ ├── index.ts # 模块导出
26
- │ │ └── timeLog.ts # 带时间的 console.log
27
- │ ├── function/ # 函数控制工具
28
- │ │ ├── index.ts # 模块导出
29
- │ │ └── loopUntil.ts # 循环执行直到满足条件
30
- │ ├── hoc/ # 高阶函数
31
- │ │ ├── index.ts # 模块导出
32
- │ │ └── withCache.ts # 带缓存的高阶函数
33
- │ ├── is/ # 类型判断工具
34
- │ │ ├── index.ts # 模块导出
35
- │ │ ├── isFalsy.ts # 假值判断
36
- │ │ ├── isNil.ts # 空值判断
37
- │ │ ├── isObject.ts # 普通对象判断
38
- │ │ ├── isPrimitive.ts # 原始值判断
39
- │ │ └── isTruthy.ts # 真值判断
40
- │ ├── network/ # 网络请求工具
41
- │ │ ├── index.ts # 模块导出
42
- │ │ ├── fetcher.ts # 基于 Fetch API 的请求客户端
43
- │ │ ├── getRealURL.ts # 从响应头获取真实链接
44
- │ │ ├── image.ts # 图片 URL 转 Base64
45
- │ │ └── to.ts # Go 语言风格的异步处理
46
- │ ├── number/ # 数字工具
47
- │ │ ├── index.ts # 模块导出
48
- │ │ └── randomInt.ts # 生成随机整数
49
- │ ├── object/ # 对象操作工具
50
- │ │ ├── index.ts # 模块导出
51
- │ │ ├── mapKeys.ts # 递归处理对象 key
52
- │ │ ├── mapValues.ts # 递归处理对象 value
53
- │ │ └── mergeObjects.ts # 深度合并对象
54
- │ ├── string/ # 字符串处理工具
55
- │ │ ├── index.ts # 模块导出
56
- │ │ ├── case.ts # 命名法转换(snake/camel/capitalize)
57
- │ │ └── compact.ts # 字符串压缩为单行
58
- │ ├── time/ # 时间控制工具
59
- │ │ ├── index.ts # 模块导出
60
- │ │ ├── debounce.ts # 防抖函数
61
- │ │ ├── sleep.ts # 延迟执行
62
- │ │ └── throttle.ts # 节流函数
63
- │ └── lru-cache.ts # LRU 缓存实现(默认导出)
64
- ├── dist/ # 构建输出目录(包含 .js 和 .d.ts 文件)
65
- ├── docs/ # TypeDoc 生成的文档网站
66
- ├── package.json # 包配置
67
- ├── tsconfig.json # TypeScript 配置
68
- ├── biome.json # Biome 代码规范配置
69
- ├── bun.lock # Bun 锁定文件
70
- └── .gitignore # Git 忽略规则
71
- ```
72
-
73
- ## Build Commands
74
-
75
- ```bash
76
- # 完整构建(打包 + 类型声明 + 文档)
77
- bun run build
78
-
79
- # 仅生成文档
80
- bun run docs
81
- ```
82
-
83
- 构建流程:
84
- 1. `bun build --target=bun --outdir ./dist --minify ./src/index.ts --packages external` - 使用 Bun 打包并压缩
85
- 2. `tsc` - 生成 TypeScript 类型声明文件(.d.ts)
86
- 3. `typedoc src/index.ts --plugin typedoc-material-theme` - 生成 API 文档
87
-
88
- ## Code Style Guidelines
89
-
90
- 项目使用 **Biome** 进行代码格式化和静态检查,配置见 `biome.json`:
91
-
92
- - **缩进**: Tab(非空格)
93
- - **引号**: 双引号(double)
94
- - **规则**:
95
- - 启用推荐规则
96
- - 允许未使用的变量和函数参数(`noUnusedVariables: off`, `noUnusedFunctionParameters: off`)
97
- - 允许显式使用 `any` 类型(`noExplicitAny: off`)
98
- - 允许非空断言(`noNonNullAssertion: off`)
99
- - 不强制使用模板字符串(`useTemplate: off`)
100
-
101
- ### 代码规范
102
-
103
- 1. **注释语言**: 使用中文编写 JSDoc 注释
104
- 2. **导出方式**: 所有工具函数使用命名导出(named exports)
105
- 3. **类型定义**:
106
- - 工具类型(如 `Primitive`, `Falsy`, `SnakeToCamel`)与实现放在同一文件
107
- - 复杂类型使用 TypeScript 模板字面量类型进行转换
108
- 4. **函数风格**:
109
- - 优先使用箭头函数
110
- - 泛型函数使用 `<T>` 语法
111
- - 类型守卫函数返回 `value is Type`
112
-
113
- ## Module Organization
114
-
115
- 项目按功能领域划分模块,每个模块是一个子目录:
116
-
117
- | 模块 | 功能描述 | 主要导出 |
118
- |------|---------|---------|
119
- | `is` | 类型判断 | `isObject`, `isPrimitive`, `isFalsy`, `isTruthy`, `isNil`, `Primitive`, `Falsy` |
120
- | `network` | 网络请求 | `fetcher`, `to`, `getRealURL`, `imageUrlToBase64`, `RequestInit`, `ImageCompressionOptions` |
121
- | `object` | 对象操作 | `mergeObjects`, `mapKeys`, `mapValues`, `DeepMapKeys`, `DeepMapValues` |
122
- | `string` | 字符串处理 | `snakeToCamel`, `camelToSnake`, `capitalize`, `decapitalize`, `compactStr` |
123
- | `time` | 时间控制 | `sleep`, `debounce`, `throttle` |
124
- | `function` | 函数控制 | `loopUntil` |
125
- | `hoc` | 高阶函数 | `withCache`, `SetTtl` |
126
- | `lru-cache` | 缓存实现 | `LRUCache`(默认导出) |
127
- | `dom` | DOM 工具 | `timeLog` |
128
- | `number` | 数字工具 | `randomInt` |
129
-
130
- 每个模块目录结构:
131
- - `index.ts` - 集中导出该模块的所有功能
132
- - 具体实现文件(如 `sleep.ts`, `debounce.ts` 等)
133
-
134
- ## Testing
135
-
136
- **当前项目没有测试框架配置**。如需添加测试,建议使用:
137
- - Bun 内置测试运行器(`bun:test`)
138
-
139
- ## Publishing
140
-
141
- 项目发布到 npm 仓库:
142
- - 包名: `@nickyzj2023/utils`
143
- - 版本: 见 `package.json` 中的 `version` 字段
144
- - 入口: `dist/index.js`(ES Module)
145
- - 类型: `dist/index.d.ts`
146
-
147
- ## Security Considerations
148
-
149
- 1. **敏感信息**: `.npmrc` 文件包含 npm 认证令牌,已在 `.gitignore` 中配置忽略
150
- 2. **网络请求**: `fetcher` 函数会解析 JSON 错误响应,注意潜在的安全风险
151
- 3. **动态导入**: `imageUrlToBase64` 使用动态导入尝试加载 `sharp`,避免在浏览器环境报错
152
-
153
- ## Development Workflow
154
-
155
- 1. 修改源代码(`src/` 目录)
156
- 2. 运行 `bun run build` 构建项目
157
- 3. 检查生成的类型声明文件(`dist/*.d.ts`)
158
- 4. 查看文档(`docs/` 目录或启动本地服务器查看)
159
- 5. 提交更改
160
-
161
- ## Important Notes
162
-
163
- - 项目使用 ES Module(`"type": "module"`),不支持 CommonJS
164
- - 构建目标为 Bun 运行时(`--target=bun`)
165
- - `imageUrlToBase64` 功能在浏览器环境使用 OffscreenCanvas 压缩,在 Node.js/Bun 环境尝试使用 sharp(可选依赖)
166
- - `fetcher` 函数基于 Fetch API,在 Bun 环境下支持 `proxy` 选项
167
- - 所有工具函数均为纯函数,无副作用
package/biome.json DELETED
@@ -1,37 +0,0 @@
1
- {
2
- "$schema": "https://biomejs.dev/schemas/2.4.3/schema.json",
3
- "vcs": {
4
- "enabled": true,
5
- "clientKind": "git",
6
- "useIgnoreFile": true
7
- },
8
- "files": {
9
- "ignoreUnknown": false
10
- },
11
- "formatter": {
12
- "enabled": true,
13
- "indentStyle": "tab"
14
- },
15
- "linter": {
16
- "enabled": true,
17
- "rules": {
18
- "recommended": true,
19
- "correctness": {
20
- "noUnusedVariables": "off",
21
- "noUnusedFunctionParameters": "off"
22
- },
23
- "suspicious": {
24
- "noExplicitAny": "off"
25
- },
26
- "style": {
27
- "noNonNullAssertion": "off",
28
- "useTemplate": "off"
29
- }
30
- }
31
- },
32
- "javascript": {
33
- "formatter": {
34
- "quoteStyle": "double"
35
- }
36
- }
37
- }
@@ -1 +0,0 @@
1
- export * from "./log";
package/dist/dom/log.d.ts DELETED
@@ -1,28 +0,0 @@
1
- /**
2
- * log 配置选项
3
- */
4
- export interface LogOptions {
5
- /**
6
- * 是否显示时间
7
- * @default true
8
- */
9
- time?: boolean;
10
- /**
11
- * 是否显示调用者文件名
12
- * @default true
13
- */
14
- fileName?: boolean;
15
- }
16
- /**
17
- * 带额外信息的 console.log
18
- * @param message - 日志消息,支持单条消息或消息数组
19
- * @param options - 配置选项
20
- *
21
- * @example
22
- * log("调试信息"); // "[14:30:00] [index.ts:15] 调试信息"
23
- * log("调试信息", { time: false }); // "[index.ts:15] 调试信息"
24
- * log("调试信息", { fileName: false }); // "[14:30:00] 调试信息"
25
- * log("调试信息", { time: false, fileName: false }); // "调试信息"
26
- * log(["消息1", "消息2"]); // "[14:30:00] [index.ts:15] 消息1 消息2"
27
- */
28
- export declare const log: (message: any | any[], options?: LogOptions) => void;
@@ -1,8 +0,0 @@
1
- /**
2
- * 附带时间的 console.log
3
- * @param args
4
- *
5
- * @example
6
- * timeLog("Hello", "World"); // 14:30:00 Hello World
7
- */
8
- export declare const timeLog: (...args: any[]) => void;
@@ -1 +0,0 @@
1
- export * from "./loop-until";
@@ -1,36 +0,0 @@
1
- /**
2
- * 循环执行函数,直到符合停止条件
3
- *
4
- * @example
5
- * // 循环请求大语言模型,直到其不再调用工具
6
- * loopUntil(
7
- * async () => {
8
- * const completion = await chatCompletions();
9
- * completion.tool_calls?.forEach(chooseAndHandleTool)
10
- * return completion;
11
- * },
12
- * {
13
- * shouldStop: (completion) => !completion.tool_calls,
14
- * },
15
- * ),
16
- *
17
- * @example
18
- * // 不传递 shouldStop,执行 3 次后正常返回最后结果
19
- * loopUntil(
20
- * () => {
21
- * return doSomething();
22
- * },
23
- * {
24
- * maxRetries: 3,
25
- * },
26
- * ),
27
- */
28
- export declare const loopUntil: <T>(fn: (count: number) => T | Promise<T>, options?: {
29
- /**
30
- * 最大循环次数
31
- * @default 5
32
- */
33
- maxRetries?: number;
34
- /** 停止循环条件。如果未传递,则执行 maxRetries 次后退出并返回最后结果 */
35
- shouldStop?: (result: T) => boolean;
36
- }) => Promise<T>;
@@ -1,22 +0,0 @@
1
- /**
2
- * 循环执行函数,直到符合停止条件
3
- *
4
- * @example
5
- * // 循环请求大语言模型,直到其不再调用工具
6
- * loopUntil(
7
- * async () => {
8
- * const completion = await chatCompletions();
9
- * completion.tool_calls?.forEach(chooseAndHandleTool)
10
- * return completion;
11
- * },
12
- * {
13
- * shouldStop: (completion) => !completion.tool_calls,
14
- * },
15
- * ),
16
- */
17
- export declare const loopUntil: <T>(fn: (count: number) => Promise<T>, options?: {
18
- /** 最大循环次数,默认 5 次 */
19
- maxRetries?: number;
20
- /** 停止循环条件,默认立即停止 */
21
- shouldStop?: (result: T) => boolean;
22
- }) => Promise<T>;
@@ -1 +0,0 @@
1
- export * from "./with-cache";
@@ -1,42 +0,0 @@
1
- export type SetTtl = (seconds: number) => void;
2
- /**
3
- * 创建一个带缓存的高阶函数
4
- *
5
- * @template Args 被包装函数的参数类型数组
6
- * @template Result 被包装函数的返回类型
7
- *
8
- * @param fn 需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间
9
- * @param ttlSeconds 以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖
10
- *
11
- * @returns 返回包装后的函数,以及缓存相关的额外方法
12
- *
13
- * @example
14
- * // 异步函数示例
15
- * const fetchData = withCache(async function (url: string) {
16
- * const data = await fetch(url).then((res) => res.json());
17
- * this.setTtl(data.expiresIn); // 根据实际情况调整过期时间
18
- * return data;
19
- * });
20
- *
21
- * await fetchData(urlA);
22
- * await fetchData(urlA); // 使用缓存结果
23
- * await fetchData(urlB);
24
- * await fetchData(urlB); // 使用缓存结果
25
- *
26
- * fetchData.clear(); // 清除缓存
27
- * await fetchData(urlA); // 重新请求
28
- * await fetchData(urlB); // 重新请求
29
- *
30
- * // 缓存过期前
31
- * await sleep();
32
- * fetchData.updateTtl(180); // 更新 ttl 并为所有未过期的缓存续期
33
- * await fetchData(urlA); // 使用缓存结果
34
- * await fetchData(urlB); // 使用缓存结果
35
- */
36
- export declare const withCache: <Args extends any[], Result>(fn: (this: {
37
- setTtl: SetTtl;
38
- }, ...args: Args) => Result, ttlSeconds?: number) => {
39
- (...args: Args): Result;
40
- clear(): void;
41
- updateTtl(seconds: number): void;
42
- };
@@ -1,42 +0,0 @@
1
- export type SetTtl = (seconds: number) => void;
2
- /**
3
- * 创建一个带缓存的高阶函数
4
- *
5
- * @template Args 被包装函数的参数类型数组
6
- * @template Result 被包装函数的返回类型
7
- *
8
- * @param fn 需要被缓存的函数,参数里附带的 setTtl 方法用于根据具体情况改写过期时间
9
- * @param ttlSeconds 以秒为单位的过期时间,-1 表示永不过期,默认 -1,会被回调函数里的 setTtl() 覆盖
10
- *
11
- * @returns 返回包装后的函数,以及缓存相关的额外方法
12
- *
13
- * @example
14
- * // 异步函数示例
15
- * const fetchData = withCache(async function (url: string) {
16
- * const data = await fetch(url).then((res) => res.json());
17
- * this.setTtl(data.expiresIn); // 根据实际情况调整过期时间
18
- * return data;
19
- * });
20
- *
21
- * await fetchData(urlA);
22
- * await fetchData(urlA); // 使用缓存结果
23
- * await fetchData(urlB);
24
- * await fetchData(urlB); // 使用缓存结果
25
- *
26
- * fetchData.clear(); // 清除缓存
27
- * await fetchData(urlA); // 重新请求
28
- * await fetchData(urlB); // 重新请求
29
- *
30
- * // 缓存过期前
31
- * await sleep();
32
- * fetchData.updateTtl(180); // 更新 ttl 并为所有未过期的缓存续期
33
- * await fetchData(urlA); // 使用缓存结果
34
- * await fetchData(urlB); // 使用缓存结果
35
- */
36
- export declare const withCache: <Args extends any[], Result>(fn: (this: {
37
- setTtl: SetTtl;
38
- }, ...args: Args) => Result, ttlSeconds?: number) => {
39
- (...args: Args): Result;
40
- clear(): void;
41
- updateTtl(seconds: number): void;
42
- };
@@ -1,4 +0,0 @@
1
- export * from "./is-falsy";
2
- export * from "./is-nil";
3
- export * from "./is-object";
4
- export * from "./is-primitive";
@@ -1,9 +0,0 @@
1
- export type Falsy = false | 0 | -0 | 0n | "" | null | undefined;
2
- /**
3
- * 检测传入的值是否为**假值**(false、0、''、null、undefined、NaN等)
4
- *
5
- * @example
6
- * isFalsy(""); // true
7
- * isFalsy(1); // false
8
- */
9
- export declare const isFalsy: (value: any) => value is Falsy;
@@ -1,9 +0,0 @@
1
- /**
2
- * 检测传入的值是否为**空值**(null、undefined)
3
- *
4
- * @example
5
- * isNil(null); // true
6
- * isNil(undefined); // true
7
- * isNil(1); // false
8
- */
9
- export declare const isNil: (value: any) => value is null | undefined;
@@ -1,8 +0,0 @@
1
- /**
2
- * 检测传入的值是否为**普通对象**
3
- *
4
- * @example
5
- * const obj = { a: 1 };
6
- * isObject(obj); // true
7
- */
8
- export declare const isObject: (value: any) => value is Record<string, any>;
@@ -1,9 +0,0 @@
1
- export type Primitive = number | string | boolean | symbol | bigint | undefined | null;
2
- /**
3
- * 检测传入的值是否为**原始值**(number、string、boolean、symbol、bigint、undefined、null)
4
- *
5
- * @example
6
- * isPrimitive(1); // true
7
- * isPrimitive([]); // false
8
- */
9
- export declare const isPrimitive: (value: any) => value is Primitive;
@@ -1,9 +0,0 @@
1
- export type Falsy = false | 0 | -0 | 0n | "" | null | undefined;
2
- /**
3
- * 检测传入的值是否为**假值**(false、0、''、null、undefined、NaN等)
4
- *
5
- * @example
6
- * isFalsy(""); // true
7
- * isFalsy(1); // false
8
- */
9
- export declare const isFalsy: (value: any) => value is Falsy;
@@ -1,9 +0,0 @@
1
- /**
2
- * 检测传入的值是否为**空值**(null、undefined)
3
- *
4
- * @example
5
- * isNil(null); // true
6
- * isNil(undefined); // true
7
- * isNil(1); // false
8
- */
9
- export declare const isNil: (value: any) => value is null | undefined;
@@ -1,8 +0,0 @@
1
- /**
2
- * 检测传入的值是否为**普通对象**
3
- *
4
- * @example
5
- * const obj = { a: 1 };
6
- * isObject(obj); // true
7
- */
8
- export declare const isObject: (value: any) => value is Record<string, any>;
@@ -1,9 +0,0 @@
1
- export type Primitive = number | string | boolean | symbol | bigint | undefined | null;
2
- /**
3
- * 检测传入的值是否为**原始值**(number、string、boolean、symbol、bigint、undefined、null)
4
- *
5
- * @example
6
- * isPrimitive(1); // true
7
- * isPrimitive([]); // false
8
- */
9
- export declare const isPrimitive: (value: any) => value is Primitive;
@@ -1,8 +0,0 @@
1
- /**
2
- * 检测传入的值是否为**真值**
3
- *
4
- * @example
5
- * isTruthy(1); // true
6
- * isTruthy(""); // false
7
- */
8
- export declare const isTruthy: (value: any) => value is any;
@@ -1,18 +0,0 @@
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
- declare class LRUCache<K, V> {
11
- private cache;
12
- private maxSize;
13
- constructor(maxSize?: number);
14
- get(key: K): V | undefined;
15
- set(key: K, value: V): void;
16
- has(key: K): boolean;
17
- }
18
- export default LRUCache;
@@ -1,56 +0,0 @@
1
- export type BunFetchOptions = {
2
- /** 代理服务器配置(仅 Bun 支持) */
3
- proxy?: string;
4
- };
5
- export type RequestInit = globalThis.RequestInit & BunFetchOptions & {
6
- params?: Record<string, any>;
7
- parser?: (response: Response) => Promise<any>;
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
- * - 支持 proxy 选项(仅在 Bun 环境有效)
22
- *
23
- * @example
24
- *
25
- * // 用法1:直接发送请求
26
- * const res = await fetcher().get<Blog>("https://nickyzj.run:3030/blogs/hello-world");
27
- *
28
- * // 用法2:创建实例
29
- * const api = fetcher("https://nickyzj.run:3030", { headers: { Authorization: "Bearer token" } });
30
- * const res = await api.get<Blog>("/blogs/hello-world", { headers: {...}, params: { page: 1 } }); // 与实例相同的 headers 会覆盖上去,params 会转成 ?page=1 跟到 url 后面
31
- *
32
- * // 用法3:使用代理(仅 Bun 环境)
33
- * const api = fetcher("https://api.example.com", {
34
- * proxy: "http://127.0.0.1:7890"
35
- * });
36
- *
37
- * // 安全处理请求结果
38
- * const [error, data] = await to(api.get<Blog>("/blogs/hello-world"));
39
- * if (error) {
40
- * console.error(error);
41
- * return;
42
- * }
43
- * console.log(data);
44
- *
45
- * // 缓存请求结果
46
- * const getBlogs = withCache(api.get);
47
- * await getBlogs("/blogs");
48
- * await sleep();
49
- * await getBlogs("/blogs"); // 不发请求,使用缓存
50
- */
51
- export declare const fetcher: (baseURL?: string, baseOptions?: RequestInit) => {
52
- get: <T>(url: string, options?: Omit<RequestInit, "method">) => Promise<T>;
53
- post: <T>(url: string, body: any, options?: Omit<RequestInit, "method" | "body">) => Promise<T>;
54
- put: <T>(url: string, body: any, options?: Omit<RequestInit, "method" | "body">) => Promise<T>;
55
- delete: <T>(url: string, options?: Omit<RequestInit, "method" | "body">) => Promise<T>;
56
- };
@@ -1,2 +0,0 @@
1
- /** 从 url 响应头获取真实链接 */
2
- export declare const getRealURL: (originURL: string) => Promise<string>;
@@ -1,2 +0,0 @@
1
- /** 从 url 响应头获取真实链接 */
2
- export declare const getRealURL: (originURL: string) => Promise<string>;
@@ -1,59 +0,0 @@
1
- /**
2
- * 图片压缩选项
3
- */
4
- export type ImageCompressionOptions = {
5
- /** 压缩比率,默认 0.92 */
6
- quality?: number;
7
- /**
8
- * 自定义压缩函数,用于覆盖默认压缩行为
9
- * @param arrayBuffer 图片的 ArrayBuffer 数据
10
- * @param mime 图片的 MIME 类型
11
- * @param quality 压缩质量
12
- * @returns 压缩后的 base64 字符串
13
- */
14
- compressor?: (arrayBuffer: ArrayBuffer, mime: string, quality: number) => Promise<string> | string;
15
- /**
16
- * 自定义 fetch 函数,用于使用自己封装的请求库读取图片
17
- * 必须返回符合 Web 标准的 Response 对象
18
- * @param url 图片地址
19
- * @returns Promise<Response>
20
- */
21
- fetcher?: (url: string) => Promise<Response>;
22
- };
23
- /**
24
- * 图片地址转 base64 数据
25
- *
26
- * @param imageUrl 图片地址
27
- * @param options 可选配置
28
- * @param options.quality 压缩比率,默认 0.92
29
- * @param options.compressor 自定义压缩函数,用于覆盖默认压缩行为
30
- *
31
- * @example
32
- * // 基本用法(浏览器自动使用 Canvas 压缩,Node.js/Bun 自动检测并使用 sharp)
33
- * imageUrlToBase64("https://example.com/image.jpg");
34
- *
35
- * @example
36
- * // 使用自定义 fetch 函数(如 axios 封装)
37
- * imageUrlToBase64("https://example.com/image.jpg", {
38
- * fetcher: async (url) => {
39
- * // 使用 axios 或其他请求库,但必须返回 Response 对象
40
- * const response = await axios.get(url, { responseType: 'arraybuffer' });
41
- * return new Response(response.data, {
42
- * status: response.status,
43
- * statusText: response.statusText,
44
- * headers: response.headers
45
- * });
46
- * }
47
- * });
48
- *
49
- * @example
50
- * // 使用自定义压缩函数覆盖默认行为
51
- * imageUrlToBase64("https://example.com/image.jpg", {
52
- * quality: 0.8,
53
- * compressor: async (buffer, mime, quality) => {
54
- * // 自定义压缩逻辑
55
- * return `data:${mime};base64,...`;
56
- * }
57
- * });
58
- */
59
- export declare const imageUrlToBase64: (imageUrl: string, options?: ImageCompressionOptions) => Promise<string>;
@@ -1,4 +0,0 @@
1
- export * from "./fetcher";
2
- export * from "./get-real-url";
3
- export * from "./image";
4
- export * from "./to";
@@ -1,9 +0,0 @@
1
- /**
2
- * Go 语言风格的异步处理方式
3
- * @param promise 一个能被 await 的异步函数
4
- * @returns 如果成功,返回 [null, 异步函数结果],否则返回 [Error, undefined]
5
- *
6
- * @example
7
- * const [error, response] = await to(fetcher().get<Blog>("/blogs/hello-world"));
8
- */
9
- export declare const to: <T, E = Error>(promise: Promise<T>) => Promise<[null, T] | [E, undefined]>;
@@ -1 +0,0 @@
1
- export * from "./random-int";