@aitianyu.cn/types 0.1.2 → 0.1.3

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 (66) hide show
  1. package/README.md +44 -0
  2. package/dist/lib/coding/Error.js +16 -6
  3. package/dist/lib/coding/Path.js +41 -20
  4. package/dist/lib/core/Environment.js +14 -0
  5. package/dist/lib/core/Errors.js +34 -13
  6. package/dist/lib/core/Language.js +60 -7
  7. package/dist/lib/core/Log.js +34 -2
  8. package/dist/lib/core/TypeConvertion.js +20 -0
  9. package/dist/lib/core/object/ArrayHelper.js +15 -4
  10. package/dist/lib/core/object/Bytes.js +8 -4
  11. package/dist/lib/core/object/Calculater.js +109 -44
  12. package/dist/lib/core/object/DataView.js +13 -4
  13. package/dist/lib/core/object/Integer.js +57 -36
  14. package/dist/lib/core/object/Json.js +22 -10
  15. package/dist/lib/core/object/ObjectHelper.js +38 -14
  16. package/dist/lib/core/object/StringHelper.js +35 -8
  17. package/dist/lib/index.js +47 -6
  18. package/dist/lib/security/Base32.js +22 -13
  19. package/dist/lib/security/Guid.js +18 -1
  20. package/dist/lib/security/Hash.js +10 -1
  21. package/dist/lib/security/QRCode.js +16 -4
  22. package/dist/lib/security/RSA.js +33 -20
  23. package/dist/lib/security/SHA.js +25 -15
  24. package/dist/lib/security/TOTP.js +31 -9
  25. package/dist/lib/types/AreaCode.js +140 -131
  26. package/dist/lib/types/Exception.js +17 -3
  27. package/dist/lib/types/Logs.js +13 -7
  28. package/dist/lib/types/PathBase.js +43 -23
  29. package/dist/lib/types/Security.js +6 -1
  30. package/dist/lib/types/TMap.js +48 -30
  31. package/dist/types/coding/Error.d.ts +16 -6
  32. package/dist/types/coding/Path.d.ts +45 -21
  33. package/dist/types/core/Environment.d.ts +14 -0
  34. package/dist/types/core/Errors.d.ts +34 -13
  35. package/dist/types/core/Language.d.ts +33 -7
  36. package/dist/types/core/Log.d.ts +34 -2
  37. package/dist/types/core/TypeConvertion.d.ts +20 -0
  38. package/dist/types/core/interface/ITJSON.d.ts +11 -0
  39. package/dist/types/core/interface/ITianyuType.d.ts +11 -0
  40. package/dist/types/core/object/ArrayHelper.d.ts +15 -4
  41. package/dist/types/core/object/Bytes.d.ts +8 -4
  42. package/dist/types/core/object/Calculater.d.ts +38 -10
  43. package/dist/types/core/object/DataView.d.ts +13 -4
  44. package/dist/types/core/object/Integer.d.ts +57 -36
  45. package/dist/types/core/object/Json.d.ts +22 -10
  46. package/dist/types/core/object/ObjectHelper.d.ts +38 -14
  47. package/dist/types/core/object/StringHelper.d.ts +35 -8
  48. package/dist/types/index.d.ts +44 -0
  49. package/dist/types/security/Base32.d.ts +31 -18
  50. package/dist/types/security/Guid.d.ts +19 -1
  51. package/dist/types/security/Hash.d.ts +10 -1
  52. package/dist/types/security/QRCode.d.ts +16 -4
  53. package/dist/types/security/RSA.d.ts +51 -28
  54. package/dist/types/security/SHA.d.ts +25 -15
  55. package/dist/types/security/TOTP.d.ts +31 -9
  56. package/dist/types/types/AreaCode.d.ts +140 -1
  57. package/dist/types/types/Exception.d.ts +17 -3
  58. package/dist/types/types/Logs.d.ts +59 -30
  59. package/dist/types/types/Object.d.ts +16 -7
  60. package/dist/types/types/PathBase.d.ts +43 -23
  61. package/dist/types/types/Security.d.ts +21 -8
  62. package/dist/types/types/TMap.d.ts +48 -30
  63. package/dist/types/types/Types.d.ts +42 -10
  64. package/doc/en/README.md +372 -0
  65. package/doc/zh/README.md +372 -0
  66. package/package.json +3 -2
@@ -0,0 +1,372 @@
1
+ # @aitianyu.cn/types — 中文文档
2
+
3
+ > aitianyu 生态系统的通用基础模块(类型、函数、类)。
4
+ > 同时支持 Node.js 和浏览器运行环境。
5
+
6
+ ---
7
+
8
+ ## 目录
9
+
10
+ - [项目简介](#项目简介)
11
+ - [项目结构](#项目结构)
12
+ - [安装](#安装)
13
+ - [使用说明](#使用说明)
14
+ - [核心工具](#核心工具)
15
+ - [对象帮助工具](#对象帮助工具)
16
+ - [安全模块](#安全模块)
17
+ - [类型定义](#类型定义)
18
+ - [构建与测试](#构建与测试)
19
+
20
+ ---
21
+
22
+ ## 项目简介
23
+
24
+ `@aitianyu.cn/types` 是 aitianyu Node.js 生态系统的基础工具包,提供以下功能:
25
+
26
+ - **强类型基础定义** — 供所有 aitianyu 包共享的接口、枚举和泛型帮助类型。
27
+ - **核心运行时工具** — 结构化日志、运行环境检测、类型转换及自定义异常。
28
+ - **对象操作帮助工具** — 深度克隆、深度比较、字符串格式化、字节操作和 JSON 工具。
29
+ - **安全工具** — TOTP(RFC 6238)、Base32 编解码、UUID/GUID 生成、SHA 哈希、RSA 加密及二维码生成。
30
+ - **路径处理** — 跨平台文件/目录路径解析与校验。
31
+
32
+ 本库以 CommonJS 格式发布,并附带完整的 TypeScript 声明文件,可直接用于 TypeScript 或 JavaScript 项目。
33
+
34
+ ---
35
+
36
+ ## 项目结构
37
+
38
+ ```
39
+ tianyu-types/
40
+ ├── src/ # TypeScript 源代码
41
+ │ ├── index.ts # 主导出入口
42
+ │ ├── coding/ # 路径编码与错误类型
43
+ │ │ ├── Error.ts # 路径相关异常
44
+ │ │ └── Path.ts # Path 类及 PathTargetType 枚举
45
+ │ ├── core/ # 核心运行时工具
46
+ │ │ ├── Environment.ts # 运行环境检测(node / browser)
47
+ │ │ ├── Errors.ts # 自定义异常类
48
+ │ │ ├── Language.ts # 区域码 / 语言环境帮助函数
49
+ │ │ ├── Log.ts # 结构化日志与性能跟踪
50
+ │ │ ├── TypeConvertion.ts # 类型转换帮助函数(如 getBoolean)
51
+ │ │ ├── interface/
52
+ │ │ │ ├── ITJSON.ts # JSON 类型接口
53
+ │ │ │ └── ITianyuType.ts # aitianyu 基础类型接口
54
+ │ │ ├── object/ # 对象 / 数据结构帮助工具
55
+ │ │ │ ├── ArrayHelper.ts # 数组工具
56
+ │ │ │ ├── Bytes.ts # 随机字节缓冲区生成
57
+ │ │ │ ├── Calculater.ts # 数学计算
58
+ │ │ │ ├── DataView.ts # 二进制 DataView 工具
59
+ │ │ │ ├── Integer.ts # 位运算 / 随机整数帮助函数
60
+ │ │ │ ├── Json.ts # JSON 处理工具
61
+ │ │ │ ├── ObjectHelper.ts # 深度克隆、比较、校验
62
+ │ │ │ └── StringHelper.ts # 字符串格式化与序列化
63
+ │ │ └── type/ # 类型包装类
64
+ │ │ ├── TArray.ts
65
+ │ │ ├── TBoolean.ts
66
+ │ │ ├── TJSON.ts
67
+ │ │ ├── TNumber.ts
68
+ │ │ ├── TObject.ts
69
+ │ │ └── TString.ts
70
+ │ ├── security/ # 安全与密码学工具
71
+ │ │ ├── Base32.ts # Base32 编解码(RFC3548、RFC4648、Crockford)
72
+ │ │ ├── Guid.ts # UUID v4 生成
73
+ │ │ ├── Hash.ts # 通用哈希操作
74
+ │ │ ├── QRCode.ts # 二维码图片生成(base64)
75
+ │ │ ├── RSA.ts # RSA 加密 / 解密
76
+ │ │ ├── SHA.ts # SHA 哈希函数
77
+ │ │ └── TOTP.ts # 基于时间的一次性密码(RFC 6238)
78
+ │ └── types/ # 纯类型定义与接口
79
+ │ ├── AreaCode.ts # 国际区域码枚举(200+ 语言环境)
80
+ │ ├── Exception.ts # 基础 Exception 类
81
+ │ ├── Logs.ts # LogLevel 枚举与日志接口
82
+ │ ├── Object.ts # 对象差异追踪类型
83
+ │ ├── PathBase.ts # 抽象路径基类
84
+ │ ├── Security.ts # EncryptOption 枚举与 ICipher 接口
85
+ │ ├── TMap.ts # 泛型 TMap 数据结构
86
+ │ ├── Types.ts # 通用泛型类型与接口
87
+ │ └── index.ts # 类型桶形导出
88
+ ├── test/ # Jest 测试套件(目录结构镜像 src/)
89
+ ├── dist/ # 编译输出(自动生成,不提交)
90
+ │ ├── lib/ # CommonJS JavaScript 文件
91
+ │ └── types/ # TypeScript 声明文件
92
+ ├── package.json
93
+ ├── tsconfig.json
94
+ ├── jest.config.js
95
+ └── eslint.config.ts
96
+ ```
97
+
98
+ ---
99
+
100
+ ## 安装
101
+
102
+ ```bash
103
+ npm install @aitianyu.cn/types
104
+ ```
105
+
106
+ 运行要求:Node.js ≥ 14 或现代浏览器环境。
107
+
108
+ ---
109
+
110
+ ## 使用说明
111
+
112
+ ### 核心工具
113
+
114
+ #### 日志
115
+
116
+ ```typescript
117
+ import { Log, Performance } from "@aitianyu.cn/types";
118
+
119
+ Log.info("服务器已在 3000 端口启动");
120
+ Log.warn("调用了已废弃的 API");
121
+ Log.error("未处理的异常", err);
122
+ Log.debug("配置已加载", config);
123
+
124
+ // 性能跟踪
125
+ const perf = Performance.startPerf("db-query");
126
+ // ... 执行操作 ...
127
+ Performance.endPerf(perf);
128
+ ```
129
+
130
+ 可用日志级别:`DEBUG`、`INFO`、`WARNING`、`ERROR`、`FATAL`、`LOG`
131
+
132
+ #### 运行环境检测
133
+
134
+ ```typescript
135
+ import { Environment } from "@aitianyu.cn/types";
136
+
137
+ const env = Environment.runtimeEnv(); // "node" | "browser"
138
+ ```
139
+
140
+ #### 类型转换
141
+
142
+ ```typescript
143
+ import { getBoolean } from "@aitianyu.cn/types";
144
+
145
+ getBoolean(1); // true
146
+ getBoolean("false"); // false
147
+ getBoolean(null); // false
148
+ ```
149
+
150
+ #### 自定义异常
151
+
152
+ ```typescript
153
+ import { ArgumentNullOrEmptyException, Exception } from "@aitianyu.cn/types";
154
+
155
+ throw new ArgumentNullOrEmptyException("userId");
156
+ ```
157
+
158
+ 可用异常类:
159
+ - `ArgumentNullOrEmptyException` — 参数为空或 null
160
+ - `ObjectCloneFunctionNotSupportException` — 对象不支持克隆函数
161
+ - `ObjectMergeStatusCheckFailedException` — 对象合并状态校验失败
162
+ - `ObjectDiffApplyInvalidStatusException` — 差异应用状态无效
163
+ - `ObjectDiffMergeFailedException` — 差异合并失败
164
+ - `PathProcessorSourceLostException` — 路径处理器源丢失
165
+ - `PathDirectoryValidationFailException` — 路径目录校验失败
166
+
167
+ ---
168
+
169
+ ### 对象帮助工具
170
+
171
+ #### 字符串格式化
172
+
173
+ ```typescript
174
+ import { StringHelper } from "@aitianyu.cn/types";
175
+
176
+ // 位置占位符(类似 C# String.Format)
177
+ StringHelper.format("你好,{0}!你有 {1} 条消息。", "Alice", 5);
178
+ // => "你好,Alice!你有 5 条消息。"
179
+
180
+ // 安全序列化(循环引用时返回 "")
181
+ StringHelper.stringifySafe(someObject);
182
+ ```
183
+
184
+ #### 深度对象操作
185
+
186
+ ```typescript
187
+ import { ObjectHelper } from "@aitianyu.cn/types";
188
+
189
+ // 深度克隆
190
+ const copy = ObjectHelper.clone(original);
191
+
192
+ // 多对象深度相等性比较
193
+ const equal = ObjectHelper.compareObjects(obj1, obj2, obj3);
194
+
195
+ // 检测原始类型
196
+ ObjectHelper.isSimpleDataType(42); // true
197
+ ObjectHelper.isSimpleDataType({}); // false
198
+
199
+ // 校验 JSON 可序列化性
200
+ ObjectHelper.validateSerializable(data);
201
+ ```
202
+
203
+ #### 整数与字节工具
204
+
205
+ ```typescript
206
+ import { Integer, Bytes } from "@aitianyu.cn/types";
207
+
208
+ const n = Integer.random(0, 100); // 生成 [0, 100] 范围内的随机整数
209
+ const buf = Bytes.random(16); // 生成 16 字节的随机 Buffer
210
+
211
+ // 位运算帮助函数
212
+ Integer.left(value, bits); // 左移
213
+ Integer.right(value, bits); // 右移(有符号)
214
+ Integer.xor(a, b); // 异或
215
+ ```
216
+
217
+ #### 数组帮助工具
218
+
219
+ ```typescript
220
+ import { ArrayHelper } from "@aitianyu.cn/types";
221
+
222
+ // 数组操作工具方法
223
+ ```
224
+
225
+ ---
226
+
227
+ ### 安全模块
228
+
229
+ #### UUID / GUID 生成
230
+
231
+ ```typescript
232
+ import { guid } from "@aitianyu.cn/types";
233
+
234
+ guid(); // "550e8400-e29b-41d4-a716-446655440000"
235
+ guid("no-dash"); // "550e8400e29b41d4a716446655440000"
236
+ ```
237
+
238
+ #### TOTP(基于时间的一次性密码)
239
+
240
+ ```typescript
241
+ import { TOTP } from "@aitianyu.cn/types";
242
+
243
+ // 生成新的 TOTP 密钥
244
+ const secret = TOTP.generate();
245
+
246
+ // 获取当前 30 秒时间窗口内的 6 位验证码
247
+ const code = TOTP.code(secret);
248
+
249
+ // 生成 otpauth:// URI(用于认证器应用 / 二维码扫描)
250
+ const uri = TOTP.getUrl("MyApp", "user@example.com", secret);
251
+ ```
252
+
253
+ #### Base32 编解码
254
+
255
+ ```typescript
256
+ import { Base32 } from "@aitianyu.cn/types";
257
+
258
+ const encoded = Base32.encode(buffer); // 默认使用 RFC4648
259
+ const decoded = Base32.decode(encoded);
260
+ const rand = Base32.random(16); // 生成 16 字符随机 Base32 字符串
261
+
262
+ // 支持的格式:"RFC3548" | "RFC4648" | "RFC4648-HEX" | "Crockford"
263
+ Base32.encode(buffer, "Crockford");
264
+ ```
265
+
266
+ #### SHA 哈希
267
+
268
+ ```typescript
269
+ import { SHA } from "@aitianyu.cn/types";
270
+
271
+ SHA.sha256("输入数据");
272
+ SHA.hmac("密钥", "消息");
273
+ ```
274
+
275
+ #### RSA 加密
276
+
277
+ ```typescript
278
+ import { RSA } from "@aitianyu.cn/types";
279
+
280
+ const { publicKey, privateKey } = RSA.generateKeyPair();
281
+ const encrypted = RSA.encrypt(publicKey, "敏感文本");
282
+ const decrypted = RSA.decrypt(privateKey, encrypted);
283
+ ```
284
+
285
+ #### 二维码生成
286
+
287
+ ```typescript
288
+ import { QRCode } from "@aitianyu.cn/types";
289
+
290
+ // 返回 base64 编码的 PNG data URL
291
+ const dataUrl = await QRCode.getURL("https://example.com");
292
+ ```
293
+
294
+ ---
295
+
296
+ ### 类型定义
297
+
298
+ #### 通用泛型类型
299
+
300
+ ```typescript
301
+ import {
302
+ MapOfString,
303
+ MapOfStrings,
304
+ MapOfBoolean,
305
+ MapOfType,
306
+ KeyValuePair,
307
+ CallbackAction,
308
+ CallbackActionT,
309
+ IComparable,
310
+ } from "@aitianyu.cn/types";
311
+
312
+ const map: MapOfString = { key: "value" };
313
+ const flags: MapOfBoolean = { enabled: true };
314
+
315
+ const pair: KeyValuePair<string, number> = { key: "age", value: 30 };
316
+
317
+ const cb: CallbackActionT<string> = (value) => console.log(value);
318
+ ```
319
+
320
+ #### 区域码
321
+
322
+ ```typescript
323
+ import { AreaCode, parseAreaCode, parseAreaString } from "@aitianyu.cn/types";
324
+
325
+ const code = AreaCode.zh_CN; // 简体中文
326
+ const str = parseAreaString(AreaCode.en_US); // "en-US"
327
+ ```
328
+
329
+ #### 路径工具
330
+
331
+ ```typescript
332
+ import { Path, PathTargetType } from "@aitianyu.cn/types";
333
+
334
+ const p = new Path("/usr/local/bin/node", PathTargetType.File);
335
+ p.directory; // "/usr/local/bin"
336
+ p.fileName; // "node"
337
+ ```
338
+
339
+ #### TMap
340
+
341
+ ```typescript
342
+ import { TMap } from "@aitianyu.cn/types";
343
+
344
+ const map = new TMap<string, number>();
345
+ map.set("a", 1);
346
+ map.get("a"); // 1
347
+ ```
348
+
349
+ ---
350
+
351
+ ## 构建与测试
352
+
353
+ ```bash
354
+ # 安装依赖
355
+ npm install
356
+
357
+ # 编译 TypeScript,生成 dist/
358
+ npm run build
359
+
360
+ # 运行测试
361
+ npm test
362
+
363
+ # 运行测试并生成覆盖率报告
364
+ npm run test-all
365
+
366
+ # 代码检查
367
+ npm run eslint
368
+ ```
369
+
370
+ 构建输出目录 `dist/`:
371
+ - `dist/lib/` — CommonJS JavaScript 文件
372
+ - `dist/types/` — TypeScript 声明文件(`.d.ts`)
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@aitianyu.cn/types",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "description": "Common modules (types, functions, classes) for aitianyu",
6
6
  "main": "./dist/lib/index.js",
7
7
  "types": "./dist/types/index.d.ts",
8
8
  "files": [
9
- "dist"
9
+ "dist",
10
+ "doc"
10
11
  ],
11
12
  "type": "commonjs",
12
13
  "keywords": [