@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
|
@@ -6,27 +6,51 @@ const TMap_1 = require("../../types/TMap");
|
|
|
6
6
|
const PathBase_1 = require("../../types/PathBase");
|
|
7
7
|
const ObjectHelper_1 = require("./ObjectHelper");
|
|
8
8
|
const Errors_1 = require("../Errors");
|
|
9
|
+
/**
|
|
10
|
+
* Internal helper class that walks two object snapshots and collects
|
|
11
|
+
* all field-level differences into an ObjectDiffMap.
|
|
12
|
+
*
|
|
13
|
+
* 内部帮助类,遍历两个对象快照并将所有字段级差异收集到 ObjectDiffMap 中。
|
|
14
|
+
*/
|
|
9
15
|
class ObjectDiffCalculation {
|
|
10
16
|
preObj;
|
|
11
17
|
nextObj;
|
|
18
|
+
/** Stack of property names tracking the current traversal path. / 跟踪当前遍历路径的属性名堆栈。 */
|
|
12
19
|
objStack;
|
|
13
20
|
diffs;
|
|
21
|
+
/**
|
|
22
|
+
* @param pre the previous (old) object snapshot / 之前(旧)的对象快照
|
|
23
|
+
* @param next the next (new) object snapshot / 之后(新)的对象快照
|
|
24
|
+
*/
|
|
14
25
|
constructor(pre, next) {
|
|
15
26
|
this.preObj = pre;
|
|
16
27
|
this.nextObj = next;
|
|
17
28
|
this.objStack = [];
|
|
18
29
|
this.diffs = new TMap_1.TMap();
|
|
19
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Run the diff calculation and return the collected differences.
|
|
33
|
+
* 执行差异计算并返回收集到的差异映射表。
|
|
34
|
+
*
|
|
35
|
+
* @returns the map of all detected differences / 所有检测到的差异映射表
|
|
36
|
+
*/
|
|
20
37
|
calculate() {
|
|
21
38
|
this._calculate(this.preObj, this.nextObj);
|
|
22
39
|
return this.diffs;
|
|
23
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Recursively compare `pre` and `next` at the current path depth.
|
|
43
|
+
* Dispatches to primitive, array, or object comparison as appropriate.
|
|
44
|
+
*
|
|
45
|
+
* 在当前路径深度递归比较 `pre` 和 `next`。
|
|
46
|
+
* 根据类型分发到原始值、数组或对象比较逻辑。
|
|
47
|
+
*/
|
|
24
48
|
_calculate(pre, next) {
|
|
25
49
|
const isPreSimple = ObjectHelper_1.ObjectHelper.isSimpleDataType(pre);
|
|
26
50
|
const isNextSimple = ObjectHelper_1.ObjectHelper.isSimpleDataType(next);
|
|
27
|
-
// 如果是基本数据类型
|
|
51
|
+
// 如果是基本数据类型 / Both values are primitive types
|
|
28
52
|
if (isPreSimple && isNextSimple) {
|
|
29
|
-
// 如果数据不相同,则记录
|
|
53
|
+
// 如果数据不相同,则记录 / Record if the values differ
|
|
30
54
|
if (pre !== next) {
|
|
31
55
|
const path = new PathBase_1.PathBase(this.objStack);
|
|
32
56
|
this.diffs.set(path, {
|
|
@@ -35,16 +59,16 @@ class ObjectDiffCalculation {
|
|
|
35
59
|
new: ObjectHelper_1.ObjectHelper.clone(next),
|
|
36
60
|
});
|
|
37
61
|
}
|
|
38
|
-
// 直接返回
|
|
62
|
+
// 直接返回 / No further recursion needed
|
|
39
63
|
return;
|
|
40
64
|
}
|
|
41
|
-
// 获取新老对象是否为数组类型
|
|
65
|
+
// 获取新老对象是否为数组类型 / Determine whether either value is an array
|
|
42
66
|
const isPreArray = Array.isArray(pre);
|
|
43
67
|
const isNextArray = Array.isArray(next);
|
|
44
68
|
if (isPreArray || isNextArray) {
|
|
45
69
|
const path = new PathBase_1.PathBase(this.objStack);
|
|
46
70
|
if (isPreArray && isNextArray) {
|
|
47
|
-
// Pre为数组 Next为数组
|
|
71
|
+
// Pre为数组 Next为数组 / Both are arrays — compare deeply
|
|
48
72
|
const isSame = ObjectHelper_1.ObjectHelper.compareObjects(pre, next);
|
|
49
73
|
if (isSame === "different") {
|
|
50
74
|
this.diffs.set(path, {
|
|
@@ -55,7 +79,7 @@ class ObjectDiffCalculation {
|
|
|
55
79
|
}
|
|
56
80
|
}
|
|
57
81
|
else {
|
|
58
|
-
// Pre或Next为数组,另外一个为object类型
|
|
82
|
+
// Pre或Next为数组,另外一个为object类型 / One is an array, the other is an object — always a diff
|
|
59
83
|
// Pre为简单对象 & Next为数组
|
|
60
84
|
// Pre为数组 & Next为简单对象
|
|
61
85
|
this.diffs.set(path, {
|
|
@@ -66,20 +90,29 @@ class ObjectDiffCalculation {
|
|
|
66
90
|
}
|
|
67
91
|
return;
|
|
68
92
|
}
|
|
69
|
-
// 新老对象都是Object类型,按照Object类型进行比对
|
|
93
|
+
// 新老对象都是Object类型,按照Object类型进行比对 / Both are plain objects — recurse by key
|
|
70
94
|
this._calculateObject(pre, next);
|
|
71
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Compare two plain objects key by key.
|
|
98
|
+
* Records deletions (keys only in `pre`), additions (keys only in `next`),
|
|
99
|
+
* and recurses for keys present in both.
|
|
100
|
+
*
|
|
101
|
+
* 逐键比较两个普通对象。
|
|
102
|
+
* 记录删除项(仅存在于 `pre` 中的键)、新增项(仅存在于 `next` 中的键),
|
|
103
|
+
* 并对两者共有的键递归比较。
|
|
104
|
+
*/
|
|
72
105
|
_calculateObject(pre, next) {
|
|
73
106
|
const preKeys = Object.keys(pre);
|
|
74
107
|
const nextKeys = Object.keys(next);
|
|
75
108
|
const sameItems = [];
|
|
76
|
-
// 寻找已经删除的
|
|
109
|
+
// 寻找已经删除的 / Find keys that were removed in next
|
|
77
110
|
for (const key of preKeys) {
|
|
78
111
|
if (nextKeys.includes(key)) {
|
|
79
112
|
sameItems.push(key);
|
|
80
113
|
}
|
|
81
114
|
else {
|
|
82
|
-
// 已经被删除的对象,储存下来
|
|
115
|
+
// 已经被删除的对象,储存下来 / Key no longer exists — record as deleted
|
|
83
116
|
this.objStack.push(key);
|
|
84
117
|
const path = new PathBase_1.PathBase(this.objStack);
|
|
85
118
|
this.diffs.set(path, {
|
|
@@ -91,10 +124,10 @@ class ObjectDiffCalculation {
|
|
|
91
124
|
this.objStack.pop();
|
|
92
125
|
}
|
|
93
126
|
}
|
|
94
|
-
// 寻找新加的元素
|
|
127
|
+
// 寻找新加的元素 / Find keys that were added in next
|
|
95
128
|
for (const key of nextKeys) {
|
|
96
129
|
if (!preKeys.includes(key)) {
|
|
97
|
-
// 老的对象中不存在,新加的状态储存下来
|
|
130
|
+
// 老的对象中不存在,新加的状态储存下来 / Key did not exist before — record as added
|
|
98
131
|
this.objStack.push(key);
|
|
99
132
|
const path = new PathBase_1.PathBase(this.objStack);
|
|
100
133
|
this.diffs.set(path, {
|
|
@@ -106,7 +139,7 @@ class ObjectDiffCalculation {
|
|
|
106
139
|
this.objStack.pop();
|
|
107
140
|
}
|
|
108
141
|
}
|
|
109
|
-
// 依次遍历所有相同的元素,判断其是否存在改变
|
|
142
|
+
// 依次遍历所有相同的元素,判断其是否存在改变 / Recurse into keys present in both objects
|
|
110
143
|
for (const key of sameItems) {
|
|
111
144
|
this.objStack.push(key);
|
|
112
145
|
this._calculate(pre[key], next[key]);
|
|
@@ -114,29 +147,61 @@ class ObjectDiffCalculation {
|
|
|
114
147
|
}
|
|
115
148
|
}
|
|
116
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Check whether `pre` and `next` are deeply equal.
|
|
152
|
+
* 检查 `pre` 与 `next` 是否深度相等。
|
|
153
|
+
*/
|
|
117
154
|
function __checkOldAndNewStatus(pre, next) {
|
|
118
155
|
return ObjectHelper_1.ObjectHelper.compareObjects(pre, next) === "same";
|
|
119
156
|
}
|
|
120
|
-
/**
|
|
157
|
+
/**
|
|
158
|
+
* Object diff calculation and merge manager.
|
|
159
|
+
* Provides two static operations:
|
|
160
|
+
* 1. calculateDiff — compute the field-level delta between two object snapshots.
|
|
161
|
+
* 2. mergeDiff — apply one or more diff maps to an object to advance its state.
|
|
162
|
+
*
|
|
163
|
+
* 对象差异计算与合并管理器。
|
|
164
|
+
* 提供两个静态操作:
|
|
165
|
+
* 1. calculateDiff — 计算两个对象快照之间的字段级差异。
|
|
166
|
+
* 2. mergeDiff — 将一个或多个差异映射表应用到对象以推进其状态。
|
|
167
|
+
*/
|
|
121
168
|
class ObjectCalculater {
|
|
122
169
|
/**
|
|
123
|
-
*
|
|
170
|
+
* Compute the field-level differences between two object snapshots.
|
|
171
|
+
* The result is an ObjectDiffMap keyed by the path of each changed field.
|
|
172
|
+
*
|
|
173
|
+
* 计算两个对象快照之间的字段级差异。
|
|
174
|
+
* 结果是以每个变更字段路径为键的 ObjectDiffMap。
|
|
124
175
|
*
|
|
125
|
-
* @param previous the previous
|
|
126
|
-
* @param newest the
|
|
127
|
-
* @returns
|
|
176
|
+
* @param previous the previous (old) state / 之前(旧)状态的对象
|
|
177
|
+
* @param newest the next (new) state / 之后(新)状态的对象
|
|
178
|
+
* @returns a map of all field differences / 所有字段差异的映射表
|
|
128
179
|
*/
|
|
129
180
|
static calculateDiff(previous, newest) {
|
|
130
181
|
const calculationEntity = new ObjectDiffCalculation(previous, newest);
|
|
131
182
|
return calculationEntity.calculate();
|
|
132
183
|
}
|
|
133
184
|
/**
|
|
134
|
-
*
|
|
185
|
+
* Apply an ordered sequence of diff maps to a value, producing the final merged state.
|
|
186
|
+
* Each diff map is applied in array order (earlier diffs are applied first).
|
|
187
|
+
*
|
|
188
|
+
* In strict mode, every merge step is validated:
|
|
189
|
+
* - Root-level diffs must not coexist with other diffs in the same map.
|
|
190
|
+
* - The current state must match the `old` value recorded in the diff entry.
|
|
191
|
+
* - Added fields must not already exist; deleted fields must still exist.
|
|
192
|
+
*
|
|
193
|
+
* 将有序的差异映射表序列应用到值上,生成最终合并状态。
|
|
194
|
+
* 每个差异映射表按数组顺序依次应用(先应用靠前的)。
|
|
195
|
+
*
|
|
196
|
+
* 在严格模式下,每步合并都会进行校验:
|
|
197
|
+
* - 根级差异不能与同一映射表中的其他差异共存。
|
|
198
|
+
* - 当前状态必须与差异条目中记录的 `old` 值匹配。
|
|
199
|
+
* - 新增字段不能已经存在;删除字段必须仍然存在。
|
|
135
200
|
*
|
|
136
|
-
* @param value the
|
|
137
|
-
* @param diffs
|
|
138
|
-
* @param strict
|
|
139
|
-
* @returns
|
|
201
|
+
* @param value the base value to apply diffs onto / 要应用差异的基础值
|
|
202
|
+
* @param diffs an ordered array of diff maps to apply / 要依次应用的差异映射表数组
|
|
203
|
+
* @param strict when true, validates pre/post state on every change; defaults to false / 为 true 时对每次变更校验前后状态,默认为 false
|
|
204
|
+
* @returns the resulting value after all diffs have been applied / 应用所有差异后的最终值
|
|
140
205
|
*/
|
|
141
206
|
static mergeDiff(value, diffs, strict) {
|
|
142
207
|
const resultObj = { root: ObjectHelper_1.ObjectHelper.clone(value) };
|
|
@@ -156,39 +221,39 @@ class ObjectCalculater {
|
|
|
156
221
|
? /* istanbul ignore next */ "add"
|
|
157
222
|
: "modify");
|
|
158
223
|
}
|
|
159
|
-
// 直接设置新值
|
|
224
|
+
// 直接设置新值 / Replace the entire root with the new value
|
|
160
225
|
resultObj.root = ObjectHelper_1.ObjectHelper.clone(rootPathDiff.new);
|
|
161
226
|
// 原始值为简单数据类型 或 数组类型 需要根目录修改
|
|
227
|
+
// Primitive or array root — must use a root-level diff entry
|
|
162
228
|
}
|
|
163
229
|
else if (ObjectHelper_1.ObjectHelper.isSimpleDataType(resultObj.root) || Array.isArray(resultObj.root)) {
|
|
164
230
|
throw new Errors_1.ObjectDiffApplyInvalidStatusException(resultObj.root);
|
|
165
231
|
}
|
|
166
232
|
else {
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
// 依次遍历所有的路径
|
|
233
|
+
// 没有根目录修改,按照复杂值递归进行更改
|
|
234
|
+
// No root-level change — apply each path entry individually
|
|
235
|
+
// 依次遍历所有的路径 / Iterate over every diff entry in this map
|
|
171
236
|
for (const [path, info] of diff) {
|
|
172
|
-
// 此处不应该进入条件
|
|
237
|
+
// 此处不应该进入条件 / Should not happen in normal usage
|
|
173
238
|
/* istanbul ignore if */
|
|
174
239
|
if (!!!path || !!!info) {
|
|
175
240
|
continue;
|
|
176
241
|
}
|
|
177
|
-
// 获取路径并预处理
|
|
242
|
+
// 获取路径并预处理 / Extract the path segments and isolate the terminal key
|
|
178
243
|
const pathDir = path.getDirs();
|
|
179
244
|
const endDir = pathDir.pop();
|
|
180
|
-
// 此处不应该进入条件
|
|
245
|
+
// 此处不应该进入条件 / Should not happen in normal usage
|
|
181
246
|
/* istanbul ignore if */
|
|
182
247
|
if (!!!endDir) {
|
|
183
248
|
continue;
|
|
184
249
|
}
|
|
185
250
|
const mergeType = info.added ? "add" : info.deleted ? "del" : "modify";
|
|
186
|
-
//
|
|
187
|
-
//
|
|
251
|
+
// 依次查询元素路径,检查父元素是否不存在
|
|
252
|
+
// Traverse parent path segments, creating missing ones in non-strict mode
|
|
188
253
|
let obj = resultObj.root;
|
|
189
254
|
for (const key of pathDir) {
|
|
190
255
|
if (!!!obj[key]) {
|
|
191
|
-
// 如果父对象不存在 在严格模式下抛出异常
|
|
256
|
+
// 如果父对象不存在 在严格模式下抛出异常 / Missing parent — throw in strict mode
|
|
192
257
|
if (strict) {
|
|
193
258
|
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
194
259
|
}
|
|
@@ -196,39 +261,39 @@ class ObjectCalculater {
|
|
|
196
261
|
obj[key] = {};
|
|
197
262
|
}
|
|
198
263
|
}
|
|
199
|
-
// 目标点向下移动
|
|
264
|
+
// 目标点向下移动 / Advance to the next level
|
|
200
265
|
obj = obj[key];
|
|
201
266
|
}
|
|
202
|
-
//
|
|
267
|
+
// 再次检查对象状态(理论上不应该进入此判断)/ Sanity check — should not reach here
|
|
203
268
|
/* istanbul ignore if */
|
|
204
269
|
if (!!!obj) {
|
|
205
270
|
throw new Errors_1.ObjectDiffMergeFailedException(info.path);
|
|
206
271
|
}
|
|
207
272
|
if (info.added) {
|
|
208
|
-
// 新增一个元素
|
|
209
|
-
// 严格模式下 不允许新增对象存在
|
|
273
|
+
// 新增一个元素 / Apply an addition
|
|
274
|
+
// 严格模式下 不允许新增对象存在 / In strict mode, the key must not already exist
|
|
210
275
|
if (strict && obj[endDir]) {
|
|
211
276
|
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
212
277
|
}
|
|
213
|
-
// 执行拷贝
|
|
278
|
+
// 执行拷贝 / Set the new value
|
|
214
279
|
obj[endDir] = ObjectHelper_1.ObjectHelper.clone(info.new);
|
|
215
280
|
}
|
|
216
281
|
else if (info.deleted) {
|
|
217
|
-
// 删除一个元素
|
|
218
|
-
// 严格模式下 不允许删除对象不存在
|
|
282
|
+
// 删除一个元素 / Apply a deletion
|
|
283
|
+
// 严格模式下 不允许删除对象不存在 / In strict mode, the key must still exist
|
|
219
284
|
if (strict && !!!obj[endDir]) {
|
|
220
285
|
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
221
286
|
}
|
|
222
|
-
//
|
|
287
|
+
// 执行删除(如果对象存在)/ Delete the property if it exists
|
|
223
288
|
!!obj[endDir] && delete obj[endDir];
|
|
224
289
|
}
|
|
225
290
|
else {
|
|
226
|
-
// 修改一个元素
|
|
227
|
-
// 严格模式下 不允许修改对象不存在
|
|
291
|
+
// 修改一个元素 / Apply a modification
|
|
292
|
+
// 严格模式下 不允许修改对象不存在 / In strict mode, the key must exist and match the recorded old value
|
|
228
293
|
if (strict && (!!!obj[endDir] || __checkOldAndNewStatus(info.old, obj[endDir]))) {
|
|
229
294
|
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
230
295
|
}
|
|
231
|
-
// 执行修改
|
|
296
|
+
// 执行修改 / Overwrite with the new value
|
|
232
297
|
obj[endDir] = ObjectHelper_1.ObjectHelper.clone(info.new);
|
|
233
298
|
}
|
|
234
299
|
}
|
|
@@ -2,13 +2,22 @@
|
|
|
2
2
|
/** @format */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.DataView = void 0;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* DataView utility class for creating a globalThis.DataView from typed array buffers.
|
|
7
|
+
* 用于从类型化数组缓冲区创建 globalThis.DataView 的工具类。
|
|
8
|
+
*/
|
|
6
9
|
class DataView {
|
|
7
10
|
/**
|
|
8
|
-
* Create a
|
|
11
|
+
* Create a globalThis.DataView wrapping the given buffer.
|
|
12
|
+
* Supports ArrayBuffer, Int8Array, Uint8Array, and Uint8ClampedArray as input.
|
|
13
|
+
* Throws an Error for unsupported input types.
|
|
9
14
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
15
|
+
* 创建一个包装给定缓冲区的 globalThis.DataView。
|
|
16
|
+
* 支持 ArrayBuffer、Int8Array、Uint8Array 和 Uint8ClampedArray 作为输入。
|
|
17
|
+
* 不支持的输入类型将抛出 Error。
|
|
18
|
+
*
|
|
19
|
+
* @param data the source data buffer / 源数据缓冲区
|
|
20
|
+
* @returns a DataView over the given buffer / 对给定缓冲区的 DataView 视图
|
|
12
21
|
*/
|
|
13
22
|
static parse(data) {
|
|
14
23
|
if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
|
|
@@ -6,15 +6,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.Integer = void 0;
|
|
8
8
|
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Integer utility class providing random number generation,
|
|
11
|
+
* byte conversion, and safe bitwise operations.
|
|
12
|
+
*
|
|
13
|
+
* 整数工具类,提供随机数生成、字节转换及安全位运算操作。
|
|
14
|
+
*/
|
|
10
15
|
class Integer {
|
|
11
16
|
/**
|
|
12
|
-
*
|
|
13
|
-
* min
|
|
17
|
+
* Generate a cryptographically random integer in the range [min, max).
|
|
18
|
+
* min is clamped to 0; max defaults to 2^48-1 (281474976710655).
|
|
14
19
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
20
|
+
* 在 [min, max) 范围内生成密码学安全的随机整数。
|
|
21
|
+
* min 最小为 0;max 默认为 2^48-1(281474976710655)。
|
|
22
|
+
*
|
|
23
|
+
* @param min lower bound (inclusive), defaults to 0 / 下界(含),默认为 0
|
|
24
|
+
* @param max upper bound (exclusive), defaults to 281474976710655 / 上界(不含),默认为 281474976710655
|
|
25
|
+
* @returns the random integer / 随机整数
|
|
18
26
|
*/
|
|
19
27
|
static random(min, max) {
|
|
20
28
|
const minNum = Math.max(min ?? 0, 0);
|
|
@@ -22,10 +30,11 @@ class Integer {
|
|
|
22
30
|
return crypto_1.default.randomInt(Math.min(minNum, maxNum), Math.max(minNum, maxNum));
|
|
23
31
|
}
|
|
24
32
|
/**
|
|
25
|
-
*
|
|
33
|
+
* Convert a number to an 8-byte (big-endian) number array.
|
|
34
|
+
* 将数值转换为 8 字节(大端序)数字数组。
|
|
26
35
|
*
|
|
27
|
-
* @param srcValue source
|
|
28
|
-
* @returns
|
|
36
|
+
* @param srcValue the source number / 源数值
|
|
37
|
+
* @returns an 8-element array of byte values / 包含 8 个字节值的数组
|
|
29
38
|
*/
|
|
30
39
|
static toBytes(srcValue) {
|
|
31
40
|
const bytes = [];
|
|
@@ -37,47 +46,57 @@ class Integer {
|
|
|
37
46
|
return bytes;
|
|
38
47
|
}
|
|
39
48
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
49
|
+
* Left-shift `value` by `times` bits.
|
|
50
|
+
* Only the lowest 6 bits of `times` are used to prevent overflow.
|
|
51
|
+
*
|
|
52
|
+
* 将 `value` 左移 `times` 位。
|
|
53
|
+
* 仅使用 `times` 的低 6 位以防止溢出。
|
|
42
54
|
*
|
|
43
|
-
* @param value source value
|
|
44
|
-
* @param times
|
|
45
|
-
* @returns
|
|
55
|
+
* @param value source value / 源数值
|
|
56
|
+
* @param times number of bits to shift / 移位位数
|
|
57
|
+
* @returns shifted result / 移位结果
|
|
46
58
|
*/
|
|
47
59
|
static left(value, times) {
|
|
48
60
|
// eslint-disable-next-line no-bitwise
|
|
49
61
|
return value << (times & 0x3f);
|
|
50
62
|
}
|
|
51
63
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
64
|
+
* Signed right-shift `value` by `times` bits.
|
|
65
|
+
* Only the lowest 6 bits of `times` are used to prevent overflow.
|
|
54
66
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
67
|
+
* 对 `value` 进行有符号右移 `times` 位。
|
|
68
|
+
* 仅使用 `times` 的低 6 位以防止溢出。
|
|
69
|
+
*
|
|
70
|
+
* @param value source value / 源数值
|
|
71
|
+
* @param times number of bits to shift / 移位位数
|
|
72
|
+
* @returns shifted result / 移位结果
|
|
58
73
|
*/
|
|
59
74
|
static right(value, times) {
|
|
60
75
|
// eslint-disable-next-line no-bitwise
|
|
61
76
|
return value >> (times & 0x3f);
|
|
62
77
|
}
|
|
63
78
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
79
|
+
* Unsigned right-shift `value` by `times` bits.
|
|
80
|
+
* Only the lowest 6 bits of `times` are used to prevent overflow.
|
|
81
|
+
*
|
|
82
|
+
* 对 `value` 进行无符号右移 `times` 位。
|
|
83
|
+
* 仅使用 `times` 的低 6 位以防止溢出。
|
|
66
84
|
*
|
|
67
|
-
* @param value source value
|
|
68
|
-
* @param times
|
|
69
|
-
* @returns
|
|
85
|
+
* @param value source value / 源数值
|
|
86
|
+
* @param times number of bits to shift / 移位位数
|
|
87
|
+
* @returns shifted result / 移位结果
|
|
70
88
|
*/
|
|
71
89
|
static rightUnsigned(value, times) {
|
|
72
90
|
// eslint-disable-next-line no-bitwise
|
|
73
91
|
return value >>> (times & 0x3f);
|
|
74
92
|
}
|
|
75
93
|
/**
|
|
76
|
-
* or
|
|
94
|
+
* Bitwise OR of value1 and one or more additional values.
|
|
95
|
+
* 对 value1 与一个或多个附加值进行按位或运算。
|
|
77
96
|
*
|
|
78
|
-
* @param value1 the first
|
|
79
|
-
* @param values
|
|
80
|
-
* @returns
|
|
97
|
+
* @param value1 the first operand / 第一个操作数
|
|
98
|
+
* @param values additional operands / 附加操作数
|
|
99
|
+
* @returns the bitwise OR result / 按位或结果
|
|
81
100
|
*/
|
|
82
101
|
static or(value1, ...values) {
|
|
83
102
|
let value = value1;
|
|
@@ -88,11 +107,12 @@ class Integer {
|
|
|
88
107
|
return value;
|
|
89
108
|
}
|
|
90
109
|
/**
|
|
91
|
-
* and
|
|
110
|
+
* Bitwise AND of value1 and one or more additional values.
|
|
111
|
+
* 对 value1 与一个或多个附加值进行按位与运算。
|
|
92
112
|
*
|
|
93
|
-
* @param value1 the first
|
|
94
|
-
* @param values
|
|
95
|
-
* @returns
|
|
113
|
+
* @param value1 the first operand / 第一个操作数
|
|
114
|
+
* @param values additional operands / 附加操作数
|
|
115
|
+
* @returns the bitwise AND result / 按位与结果
|
|
96
116
|
*/
|
|
97
117
|
static and(value1, ...values) {
|
|
98
118
|
let value = value1;
|
|
@@ -103,11 +123,12 @@ class Integer {
|
|
|
103
123
|
return value;
|
|
104
124
|
}
|
|
105
125
|
/**
|
|
106
|
-
*
|
|
126
|
+
* Bitwise XOR of value1 and value2.
|
|
127
|
+
* 对 value1 和 value2 进行按位异或运算。
|
|
107
128
|
*
|
|
108
|
-
* @param value1 the first
|
|
109
|
-
* @param value2 the second
|
|
110
|
-
* @returns
|
|
129
|
+
* @param value1 the first operand / 第一个操作数
|
|
130
|
+
* @param value2 the second operand / 第二个操作数
|
|
131
|
+
* @returns the XOR result / 异或结果
|
|
111
132
|
*/
|
|
112
133
|
static xor(value1, value2) {
|
|
113
134
|
// eslint-disable-next-line no-bitwise
|
|
@@ -2,26 +2,38 @@
|
|
|
2
2
|
/** @format */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Json = void 0;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* JSON utility class providing safe and unsafe JSON parse helpers.
|
|
7
|
+
* JSON 工具类,提供安全与非安全的 JSON 解析帮助方法。
|
|
8
|
+
*/
|
|
6
9
|
class Json {
|
|
7
10
|
/**
|
|
8
|
-
* @deprecated
|
|
11
|
+
* @deprecated Use parseSafe instead.
|
|
9
12
|
*
|
|
10
|
-
*
|
|
11
|
-
* This is an unsafe
|
|
13
|
+
* Parse a JSON string into a JavaScript object.
|
|
14
|
+
* This is an unsafe operation — it throws a SyntaxError if the string is invalid.
|
|
15
|
+
* Prefer parseSafe for production use.
|
|
12
16
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
17
|
+
* 将 JSON 字符串解析为 JavaScript 对象。
|
|
18
|
+
* 这是一个不安全的操作——若字符串无效则抛出 SyntaxError。
|
|
19
|
+
* 生产环境建议使用 parseSafe。
|
|
20
|
+
*
|
|
21
|
+
* @param src the JSON source string / JSON 源字符串
|
|
22
|
+
* @returns the parsed JavaScript object / 解析后的 JavaScript 对象
|
|
15
23
|
*/
|
|
16
24
|
static parse(src) {
|
|
17
25
|
return JSON.parse(src);
|
|
18
26
|
}
|
|
19
27
|
/**
|
|
20
|
-
*
|
|
28
|
+
* Parse a JSON string into a JavaScript object safely.
|
|
29
|
+
* Returns the specified fallback value (default: null) if parsing fails.
|
|
30
|
+
*
|
|
31
|
+
* 安全地将 JSON 字符串解析为 JavaScript 对象。
|
|
32
|
+
* 解析失败时返回指定的回退值(默认为 null)。
|
|
21
33
|
*
|
|
22
|
-
* @param src source string
|
|
23
|
-
* @param failed value
|
|
24
|
-
* @returns
|
|
34
|
+
* @param src the JSON source string / JSON 源字符串
|
|
35
|
+
* @param failed the value to return on parse failure, defaults to null / 解析失败时的返回值,默认为 null
|
|
36
|
+
* @returns the parsed object, or the fallback value on failure / 解析后的对象,失败时返回回退值
|
|
25
37
|
*/
|
|
26
38
|
static parseSafe(src, failed = null) {
|
|
27
39
|
try {
|
|
@@ -3,13 +3,23 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.ObjectHelper = void 0;
|
|
5
5
|
const Errors_1 = require("../Errors");
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* Object utility class providing data type checking, deep cloning,
|
|
8
|
+
* serialization validation, and multi-object comparison.
|
|
9
|
+
*
|
|
10
|
+
* 对象工具类,提供数据类型检测、深度克隆、序列化校验和多对象比较功能。
|
|
11
|
+
*/
|
|
7
12
|
class ObjectHelper {
|
|
8
13
|
/**
|
|
9
|
-
*
|
|
14
|
+
* Check whether the given value is a JavaScript primitive type
|
|
15
|
+
* (boolean, string, number, symbol, null, or undefined).
|
|
10
16
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
17
|
+
* 检查给定值是否为 JavaScript 原始类型
|
|
18
|
+
* (boolean、string、number、symbol、null 或 undefined)。
|
|
19
|
+
*
|
|
20
|
+
* @param value the value to inspect / 要检查的值
|
|
21
|
+
* @returns true if the value is a primitive type, false for objects and arrays
|
|
22
|
+
* 原始类型返回 true,对象和数组返回 false
|
|
13
23
|
*/
|
|
14
24
|
static isSimpleDataType(value) {
|
|
15
25
|
if (!!!value) {
|
|
@@ -19,10 +29,16 @@ class ObjectHelper {
|
|
|
19
29
|
return typeofValue === "boolean" || typeofValue === "string" || typeofValue === "number" || typeofValue === "symbol";
|
|
20
30
|
}
|
|
21
31
|
/**
|
|
22
|
-
*
|
|
32
|
+
* Create a deep copy of the given value.
|
|
33
|
+
* Arrays and plain objects are recursively cloned.
|
|
34
|
+
* Throws ObjectCloneFunctionNotSupportException if the source contains a function.
|
|
35
|
+
*
|
|
36
|
+
* 创建给定值的深度副本。
|
|
37
|
+
* 数组和普通对象将递归克隆。
|
|
38
|
+
* 若源对象包含函数值,则抛出 ObjectCloneFunctionNotSupportException。
|
|
23
39
|
*
|
|
24
|
-
* @param
|
|
25
|
-
* @returns
|
|
40
|
+
* @param source the value to clone / 要克隆的值
|
|
41
|
+
* @returns a new deep-copied value independent from the source / 与源对象独立的深度副本
|
|
26
42
|
*/
|
|
27
43
|
static clone(source) {
|
|
28
44
|
return ObjectHelper._clone(source);
|
|
@@ -71,10 +87,14 @@ class ObjectHelper {
|
|
|
71
87
|
return result;
|
|
72
88
|
}
|
|
73
89
|
/**
|
|
74
|
-
*
|
|
90
|
+
* Verify that the given value can be safely passed through JSON.stringify.
|
|
91
|
+
* Returns false for function types or objects containing function-valued properties.
|
|
75
92
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
93
|
+
* 验证给定值是否可安全通过 JSON.stringify 序列化。
|
|
94
|
+
* 函数类型或包含函数属性的对象返回 false。
|
|
95
|
+
*
|
|
96
|
+
* @param obj the value to validate / 要校验的值
|
|
97
|
+
* @returns true if the value is JSON-serializable / 可 JSON 序列化时返回 true
|
|
78
98
|
*/
|
|
79
99
|
static validateSerializable(obj) {
|
|
80
100
|
// if type is function, is not invaliable to be string
|
|
@@ -113,11 +133,15 @@ class ObjectHelper {
|
|
|
113
133
|
}
|
|
114
134
|
}
|
|
115
135
|
/**
|
|
116
|
-
* Compare two or more
|
|
136
|
+
* Compare two or more values for deep equality.
|
|
137
|
+
* If fewer than two arguments are provided, "same" is returned.
|
|
138
|
+
*
|
|
139
|
+
* 对两个或多个值进行深度相等性比较。
|
|
140
|
+
* 若传入参数少于两个,则返回 "same"。
|
|
117
141
|
*
|
|
118
|
-
* @param objs the
|
|
119
|
-
* @returns same
|
|
120
|
-
*
|
|
142
|
+
* @param objs the values to compare / 要比较的值
|
|
143
|
+
* @returns "same" if all values are deeply equal, "different" otherwise
|
|
144
|
+
* 所有值深度相等时返回 "same",否则返回 "different"
|
|
121
145
|
*/
|
|
122
146
|
static compareObjects(...objs) {
|
|
123
147
|
// if objs less than 2, is not comparable
|