@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.
- package/README.md +44 -0
- package/dist/lib/coding/Error.js +16 -6
- package/dist/lib/coding/Path.js +41 -20
- package/dist/lib/core/Environment.js +14 -0
- package/dist/lib/core/Errors.js +34 -13
- package/dist/lib/core/Language.js +60 -7
- package/dist/lib/core/Log.js +34 -2
- package/dist/lib/core/TypeConvertion.js +20 -0
- package/dist/lib/core/object/ArrayHelper.js +15 -4
- package/dist/lib/core/object/Bytes.js +8 -4
- package/dist/lib/core/object/Calculater.js +109 -44
- package/dist/lib/core/object/DataView.js +13 -4
- package/dist/lib/core/object/Integer.js +57 -36
- package/dist/lib/core/object/Json.js +22 -10
- package/dist/lib/core/object/ObjectHelper.js +38 -14
- package/dist/lib/core/object/StringHelper.js +35 -8
- package/dist/lib/index.js +47 -6
- package/dist/lib/security/Base32.js +22 -13
- package/dist/lib/security/Guid.js +18 -1
- package/dist/lib/security/Hash.js +10 -1
- package/dist/lib/security/QRCode.js +16 -4
- package/dist/lib/security/RSA.js +33 -20
- package/dist/lib/security/SHA.js +25 -15
- package/dist/lib/security/TOTP.js +31 -9
- package/dist/lib/types/AreaCode.js +140 -131
- package/dist/lib/types/Exception.js +17 -3
- package/dist/lib/types/Logs.js +13 -7
- package/dist/lib/types/PathBase.js +43 -23
- package/dist/lib/types/Security.js +6 -1
- package/dist/lib/types/TMap.js +48 -30
- package/dist/types/coding/Error.d.ts +16 -6
- package/dist/types/coding/Path.d.ts +45 -21
- package/dist/types/core/Environment.d.ts +14 -0
- package/dist/types/core/Errors.d.ts +34 -13
- package/dist/types/core/Language.d.ts +33 -7
- package/dist/types/core/Log.d.ts +34 -2
- package/dist/types/core/TypeConvertion.d.ts +20 -0
- package/dist/types/core/interface/ITJSON.d.ts +11 -0
- package/dist/types/core/interface/ITianyuType.d.ts +11 -0
- package/dist/types/core/object/ArrayHelper.d.ts +15 -4
- package/dist/types/core/object/Bytes.d.ts +8 -4
- package/dist/types/core/object/Calculater.d.ts +38 -10
- package/dist/types/core/object/DataView.d.ts +13 -4
- package/dist/types/core/object/Integer.d.ts +57 -36
- package/dist/types/core/object/Json.d.ts +22 -10
- package/dist/types/core/object/ObjectHelper.d.ts +38 -14
- package/dist/types/core/object/StringHelper.d.ts +35 -8
- package/dist/types/index.d.ts +44 -0
- package/dist/types/security/Base32.d.ts +31 -18
- package/dist/types/security/Guid.d.ts +19 -1
- package/dist/types/security/Hash.d.ts +10 -1
- package/dist/types/security/QRCode.d.ts +16 -4
- package/dist/types/security/RSA.d.ts +51 -28
- package/dist/types/security/SHA.d.ts +25 -15
- package/dist/types/security/TOTP.d.ts +31 -9
- package/dist/types/types/AreaCode.d.ts +140 -1
- package/dist/types/types/Exception.d.ts +17 -3
- package/dist/types/types/Logs.d.ts +59 -30
- package/dist/types/types/Object.d.ts +16 -7
- package/dist/types/types/PathBase.d.ts +43 -23
- package/dist/types/types/Security.d.ts +21 -8
- package/dist/types/types/TMap.d.ts +48 -30
- package/dist/types/types/Types.d.ts +42 -10
- package/doc/en/README.md +372 -0
- package/doc/zh/README.md +372 -0
- package/package.json +3 -2
package/doc/en/README.md
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
# @aitianyu.cn/types — English Documentation
|
|
2
|
+
|
|
3
|
+
> Common modules (types, functions, classes) for the aitianyu ecosystem.
|
|
4
|
+
> Supports both Node.js and browser environments.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Table of Contents
|
|
9
|
+
|
|
10
|
+
- [Project Description](#project-description)
|
|
11
|
+
- [Project Structure](#project-structure)
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [Core Utilities](#core-utilities)
|
|
15
|
+
- [Object Helpers](#object-helpers)
|
|
16
|
+
- [Security](#security)
|
|
17
|
+
- [Type Definitions](#type-definitions)
|
|
18
|
+
- [Build & Test](#build--test)
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Project Description
|
|
23
|
+
|
|
24
|
+
`@aitianyu.cn/types` is the foundational utility package of the aitianyu Node.js ecosystem. It provides:
|
|
25
|
+
|
|
26
|
+
- **Strongly-typed base types** — interfaces, enums, and generic helpers used across all aitianyu packages.
|
|
27
|
+
- **Core runtime utilities** — structured logging, environment detection, type conversion, and custom exceptions.
|
|
28
|
+
- **Object manipulation helpers** — deep clone, deep compare, string formatting, byte operations, and JSON utilities.
|
|
29
|
+
- **Security utilities** — TOTP (RFC 6238), Base32 encoding, UUID/GUID generation, SHA hashing, RSA encryption, and QR code generation.
|
|
30
|
+
- **Path processing** — cross-platform file/directory path parsing and validation.
|
|
31
|
+
|
|
32
|
+
The library is published as CommonJS and ships full TypeScript declaration files, so it can be used directly in TypeScript or JavaScript projects.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Project Structure
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
tianyu-types/
|
|
40
|
+
├── src/ # TypeScript source files
|
|
41
|
+
│ ├── index.ts # Main export entry point
|
|
42
|
+
│ ├── coding/ # Path encoding and error types
|
|
43
|
+
│ │ ├── Error.ts # Path-related exceptions
|
|
44
|
+
│ │ └── Path.ts # Path class and PathTargetType enum
|
|
45
|
+
│ ├── core/ # Core runtime utilities
|
|
46
|
+
│ │ ├── Environment.ts # Runtime environment detection (node / browser)
|
|
47
|
+
│ │ ├── Errors.ts # Custom exception classes
|
|
48
|
+
│ │ ├── Language.ts # Area code / locale helpers
|
|
49
|
+
│ │ ├── Log.ts # Structured logging and performance tracking
|
|
50
|
+
│ │ ├── TypeConvertion.ts # Type conversion helpers (e.g. getBoolean)
|
|
51
|
+
│ │ ├── interface/
|
|
52
|
+
│ │ │ ├── ITJSON.ts # JSON type interface
|
|
53
|
+
│ │ │ └── ITianyuType.ts # Base aitianyu type interface
|
|
54
|
+
│ │ ├── object/ # Object / data-structure helpers
|
|
55
|
+
│ │ │ ├── ArrayHelper.ts # Array utilities
|
|
56
|
+
│ │ │ ├── Bytes.ts # Random byte buffer generation
|
|
57
|
+
│ │ │ ├── Calculater.ts # Mathematical calculations
|
|
58
|
+
│ │ │ ├── DataView.ts # Binary DataView utilities
|
|
59
|
+
│ │ │ ├── Integer.ts # Bitwise / random integer helpers
|
|
60
|
+
│ │ │ ├── Json.ts # JSON processing utilities
|
|
61
|
+
│ │ │ ├── ObjectHelper.ts # Deep clone, compare, validate
|
|
62
|
+
│ │ │ └── StringHelper.ts # String formatting and stringify
|
|
63
|
+
│ │ └── type/ # Typed wrapper classes
|
|
64
|
+
│ │ ├── TArray.ts
|
|
65
|
+
│ │ ├── TBoolean.ts
|
|
66
|
+
│ │ ├── TJSON.ts
|
|
67
|
+
│ │ ├── TNumber.ts
|
|
68
|
+
│ │ ├── TObject.ts
|
|
69
|
+
│ │ └── TString.ts
|
|
70
|
+
│ ├── security/ # Security and cryptography utilities
|
|
71
|
+
│ │ ├── Base32.ts # Base32 encode / decode (RFC3548, RFC4648, Crockford)
|
|
72
|
+
│ │ ├── Guid.ts # UUID v4 generation
|
|
73
|
+
│ │ ├── Hash.ts # Generic hash operations
|
|
74
|
+
│ │ ├── QRCode.ts # QR code image generation (base64)
|
|
75
|
+
│ │ ├── RSA.ts # RSA encryption / decryption
|
|
76
|
+
│ │ ├── SHA.ts # SHA hash functions
|
|
77
|
+
│ │ └── TOTP.ts # Time-based One-Time Password (RFC 6238)
|
|
78
|
+
│ └── types/ # Pure type definitions and interfaces
|
|
79
|
+
│ ├── AreaCode.ts # International area code enum (200+ locales)
|
|
80
|
+
│ ├── Exception.ts # Base Exception class
|
|
81
|
+
│ ├── Logs.ts # LogLevel enum and logging interfaces
|
|
82
|
+
│ ├── Object.ts # Object diff tracking types
|
|
83
|
+
│ ├── PathBase.ts # Abstract path base class
|
|
84
|
+
│ ├── Security.ts # EncryptOption enum and ICipher interface
|
|
85
|
+
│ ├── TMap.ts # Generic TMap data structure
|
|
86
|
+
│ ├── Types.ts # Common generic types and interfaces
|
|
87
|
+
│ └── index.ts # Type barrel export
|
|
88
|
+
├── test/ # Jest test suites (mirrors src/ structure)
|
|
89
|
+
├── dist/ # Compiled output (generated, not committed)
|
|
90
|
+
│ ├── lib/ # CommonJS JavaScript files
|
|
91
|
+
│ └── types/ # TypeScript declaration files
|
|
92
|
+
├── package.json
|
|
93
|
+
├── tsconfig.json
|
|
94
|
+
├── jest.config.js
|
|
95
|
+
└── eslint.config.ts
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Installation
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
npm install @aitianyu.cn/types
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Requirements: Node.js ≥ 14 or a modern browser environment.
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Usage
|
|
111
|
+
|
|
112
|
+
### Core Utilities
|
|
113
|
+
|
|
114
|
+
#### Logging
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { Log, Performance } from "@aitianyu.cn/types";
|
|
118
|
+
|
|
119
|
+
Log.info("Server started on port 3000");
|
|
120
|
+
Log.warn("Deprecated API called");
|
|
121
|
+
Log.error("Unhandled exception", err);
|
|
122
|
+
Log.debug("Config loaded", config);
|
|
123
|
+
|
|
124
|
+
// Performance tracking
|
|
125
|
+
const perf = Performance.startPerf("db-query");
|
|
126
|
+
// ... do work ...
|
|
127
|
+
Performance.endPerf(perf);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Available log levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `FATAL`, `LOG`
|
|
131
|
+
|
|
132
|
+
#### Environment Detection
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { Environment } from "@aitianyu.cn/types";
|
|
136
|
+
|
|
137
|
+
const env = Environment.runtimeEnv(); // "node" | "browser"
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### Type Conversion
|
|
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
|
+
#### Custom Exceptions
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { ArgumentNullOrEmptyException, Exception } from "@aitianyu.cn/types";
|
|
154
|
+
|
|
155
|
+
throw new ArgumentNullOrEmptyException("userId");
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Available exceptions:
|
|
159
|
+
- `ArgumentNullOrEmptyException`
|
|
160
|
+
- `ObjectCloneFunctionNotSupportException`
|
|
161
|
+
- `ObjectMergeStatusCheckFailedException`
|
|
162
|
+
- `ObjectDiffApplyInvalidStatusException`
|
|
163
|
+
- `ObjectDiffMergeFailedException`
|
|
164
|
+
- `PathProcessorSourceLostException`
|
|
165
|
+
- `PathDirectoryValidationFailException`
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
### Object Helpers
|
|
170
|
+
|
|
171
|
+
#### String Formatting
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
import { StringHelper } from "@aitianyu.cn/types";
|
|
175
|
+
|
|
176
|
+
// Positional placeholders (like C# String.Format)
|
|
177
|
+
StringHelper.format("Hello, {0}! You have {1} messages.", "Alice", 5);
|
|
178
|
+
// => "Hello, Alice! You have 5 messages."
|
|
179
|
+
|
|
180
|
+
// Safe stringify (returns "" on circular reference)
|
|
181
|
+
StringHelper.stringifySafe(someObject);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Deep Object Operations
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { ObjectHelper } from "@aitianyu.cn/types";
|
|
188
|
+
|
|
189
|
+
// Deep clone
|
|
190
|
+
const copy = ObjectHelper.clone(original);
|
|
191
|
+
|
|
192
|
+
// Deep equality check across multiple objects
|
|
193
|
+
const equal = ObjectHelper.compareObjects(obj1, obj2, obj3);
|
|
194
|
+
|
|
195
|
+
// Check primitive types
|
|
196
|
+
ObjectHelper.isSimpleDataType(42); // true
|
|
197
|
+
ObjectHelper.isSimpleDataType({}); // false
|
|
198
|
+
|
|
199
|
+
// Validate JSON-serialisability
|
|
200
|
+
ObjectHelper.validateSerializable(data);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### Integer & Byte Utilities
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
import { Integer, Bytes } from "@aitianyu.cn/types";
|
|
207
|
+
|
|
208
|
+
const n = Integer.random(0, 100); // random int in [0, 100]
|
|
209
|
+
const buf = Bytes.random(16); // 16-byte random Buffer
|
|
210
|
+
|
|
211
|
+
// Bitwise helpers
|
|
212
|
+
Integer.left(value, bits);
|
|
213
|
+
Integer.right(value, bits);
|
|
214
|
+
Integer.xor(a, b);
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
#### Array Helpers
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { ArrayHelper } from "@aitianyu.cn/types";
|
|
221
|
+
|
|
222
|
+
// Utility methods for array manipulation
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
### Security
|
|
228
|
+
|
|
229
|
+
#### UUID / GUID Generation
|
|
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 (Time-based One-Time Password)
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import { TOTP } from "@aitianyu.cn/types";
|
|
242
|
+
|
|
243
|
+
// Generate a new TOTP secret key
|
|
244
|
+
const secret = TOTP.generate();
|
|
245
|
+
|
|
246
|
+
// Get a 6-digit code for the current 30-second window
|
|
247
|
+
const code = TOTP.code(secret);
|
|
248
|
+
|
|
249
|
+
// Build an otpauth:// URI (for use with authenticator apps / QR codes)
|
|
250
|
+
const uri = TOTP.getUrl("MyApp", "user@example.com", secret);
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
#### Base32 Encoding
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
import { Base32 } from "@aitianyu.cn/types";
|
|
257
|
+
|
|
258
|
+
const encoded = Base32.encode(buffer); // RFC4648 by default
|
|
259
|
+
const decoded = Base32.decode(encoded);
|
|
260
|
+
const rand = Base32.random(16); // 16-char random Base32 string
|
|
261
|
+
|
|
262
|
+
// Supported types: "RFC3548" | "RFC4648" | "RFC4648-HEX" | "Crockford"
|
|
263
|
+
Base32.encode(buffer, "Crockford");
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
#### SHA Hashing
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
import { SHA } from "@aitianyu.cn/types";
|
|
270
|
+
|
|
271
|
+
SHA.sha256("input data");
|
|
272
|
+
SHA.hmac("secret", "message");
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
#### RSA Encryption
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
import { RSA } from "@aitianyu.cn/types";
|
|
279
|
+
|
|
280
|
+
const { publicKey, privateKey } = RSA.generateKeyPair();
|
|
281
|
+
const encrypted = RSA.encrypt(publicKey, "secret text");
|
|
282
|
+
const decrypted = RSA.decrypt(privateKey, encrypted);
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
#### QR Code Generation
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
import { QRCode } from "@aitianyu.cn/types";
|
|
289
|
+
|
|
290
|
+
// Returns a base64-encoded PNG data URL
|
|
291
|
+
const dataUrl = await QRCode.getURL("https://example.com");
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
---
|
|
295
|
+
|
|
296
|
+
### Type Definitions
|
|
297
|
+
|
|
298
|
+
#### Common Generic Types
|
|
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
|
+
#### Area Codes
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
import { AreaCode, parseAreaCode, parseAreaString } from "@aitianyu.cn/types";
|
|
324
|
+
|
|
325
|
+
const code = AreaCode.zh_CN; // Chinese Simplified
|
|
326
|
+
const str = parseAreaString(AreaCode.en_US); // "en-US"
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
#### Path Utilities
|
|
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
|
+
## Build & Test
|
|
352
|
+
|
|
353
|
+
```bash
|
|
354
|
+
# Install dependencies
|
|
355
|
+
npm install
|
|
356
|
+
|
|
357
|
+
# Compile TypeScript and produce dist/
|
|
358
|
+
npm run build
|
|
359
|
+
|
|
360
|
+
# Run tests
|
|
361
|
+
npm test
|
|
362
|
+
|
|
363
|
+
# Run tests with coverage
|
|
364
|
+
npm run test-all
|
|
365
|
+
|
|
366
|
+
# Lint
|
|
367
|
+
npm run eslint
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Build output is placed in `dist/`:
|
|
371
|
+
- `dist/lib/` — CommonJS JavaScript
|
|
372
|
+
- `dist/types/` — TypeScript declaration files (`.d.ts`)
|