@aitianyu.cn/types 0.0.1 → 0.0.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/dist/lib/index.js +25 -1
- package/dist/lib/types/AreaCode.js +1 -0
- package/dist/lib/types/Exception.js +6 -0
- package/dist/lib/types/Logs.js +7 -0
- package/dist/lib/types/Object.js +3 -0
- package/dist/lib/types/PathBase.js +109 -0
- package/dist/lib/types/Security.js +1 -0
- package/dist/lib/types/TMap.js +138 -0
- package/dist/lib/types/index.js +17 -0
- package/dist/lib/utilities/coding/Error.js +44 -0
- package/dist/lib/utilities/coding/Path.js +198 -0
- package/dist/lib/utilities/coding/index.js +10 -0
- package/dist/lib/utilities/core/Errors.js +73 -1
- package/dist/lib/utilities/core/Language.js +13 -0
- package/dist/lib/utilities/core/Log.js +2 -0
- package/dist/lib/utilities/core/index.js +21 -0
- package/dist/lib/utilities/core/object/Calculater.js +230 -0
- package/dist/lib/utilities/core/object/Helper.js +201 -0
- package/dist/lib/utilities/security/Guid.js +1 -0
- package/dist/lib/utilities/security/Hash.js +1 -0
- package/dist/lib/utilities/security/index.js +8 -0
- package/dist/types/index.d.ts +9 -2
- package/dist/types/types/AreaCode.d.ts +1 -0
- package/dist/types/types/Exception.d.ts +6 -0
- package/dist/types/types/Logs.d.ts +43 -0
- package/dist/types/types/Object.d.ts +18 -0
- package/dist/types/types/PathBase.d.ts +76 -0
- package/dist/types/types/Security.d.ts +14 -0
- package/dist/types/types/TMap.d.ts +80 -0
- package/dist/types/types/Types.d.ts +13 -0
- package/dist/types/types/index.d.ts +9 -0
- package/dist/types/utilities/coding/Error.d.ts +27 -0
- package/dist/types/utilities/coding/Path.d.ts +61 -0
- package/dist/types/utilities/coding/index.d.ts +3 -0
- package/dist/types/utilities/core/Errors.d.ts +41 -0
- package/dist/types/utilities/core/Language.d.ts +13 -0
- package/dist/types/utilities/core/Log.d.ts +2 -0
- package/dist/types/utilities/core/index.d.ts +6 -0
- package/dist/types/utilities/core/object/Calculater.d.ts +22 -0
- package/dist/types/utilities/core/object/Helper.d.ts +39 -0
- package/dist/types/utilities/security/Guid.d.ts +1 -0
- package/dist/types/utilities/security/Hash.d.ts +1 -0
- package/dist/types/utilities/security/index.d.ts +3 -0
- package/package.json +11 -3
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**@format */
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.ArgumentNullOrEmptyException = void 0;
|
|
4
|
+
exports.ObjectDiffMergeFailedException = exports.ObjectDiffApplyInvalidStatusException = exports.ObjectMergeStatusCheckFailedException = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = void 0;
|
|
5
5
|
const Exception_1 = require("../../types/Exception");
|
|
6
|
+
/** Argument null exception */
|
|
6
7
|
class ArgumentNullOrEmptyException extends Exception_1.Exception {
|
|
7
8
|
constructor(msg) {
|
|
8
9
|
super(msg);
|
|
@@ -12,3 +13,74 @@ class ArgumentNullOrEmptyException extends Exception_1.Exception {
|
|
|
12
13
|
}
|
|
13
14
|
}
|
|
14
15
|
exports.ArgumentNullOrEmptyException = ArgumentNullOrEmptyException;
|
|
16
|
+
/**
|
|
17
|
+
* Object Clone:
|
|
18
|
+
* Function type is not supported.
|
|
19
|
+
* Exception will be thrown if try to clone a function type object
|
|
20
|
+
*/
|
|
21
|
+
class ObjectCloneFunctionNotSupportException extends Exception_1.Exception {
|
|
22
|
+
constructor() {
|
|
23
|
+
super();
|
|
24
|
+
}
|
|
25
|
+
toString() {
|
|
26
|
+
return `不支持的类型 - 正在克隆为Function类型不支持`;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.ObjectCloneFunctionNotSupportException = ObjectCloneFunctionNotSupportException;
|
|
30
|
+
/**
|
|
31
|
+
* Object Clone:
|
|
32
|
+
* Object different status could not be matched.
|
|
33
|
+
* Exception will be thrown if the object diff could not to be merged
|
|
34
|
+
*/
|
|
35
|
+
class ObjectMergeStatusCheckFailedException extends Exception_1.Exception {
|
|
36
|
+
status;
|
|
37
|
+
path;
|
|
38
|
+
constructor(path, status) {
|
|
39
|
+
super();
|
|
40
|
+
this.path = path;
|
|
41
|
+
this.status = status === "add" ? "新增" : status === "del" ? "删除" : "修改";
|
|
42
|
+
}
|
|
43
|
+
toString() {
|
|
44
|
+
return `错误的合并状态 - ${this.status} 路径 ${this.path} 前后状态不一致`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ObjectMergeStatusCheckFailedException = ObjectMergeStatusCheckFailedException;
|
|
48
|
+
/**
|
|
49
|
+
* Object Clone:
|
|
50
|
+
* Object different status could not be applied.
|
|
51
|
+
* Exception will be thrown if the object diff could not to be applied
|
|
52
|
+
*/
|
|
53
|
+
class ObjectDiffApplyInvalidStatusException extends Exception_1.Exception {
|
|
54
|
+
value;
|
|
55
|
+
constructor(value) {
|
|
56
|
+
super();
|
|
57
|
+
this.value = value;
|
|
58
|
+
}
|
|
59
|
+
toString() {
|
|
60
|
+
let v2s = "";
|
|
61
|
+
try {
|
|
62
|
+
v2s = JSON.stringify(this.value);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
v2s = v2s || `[${(typeof this.value).toString()}]`;
|
|
66
|
+
}
|
|
67
|
+
return `无效的状态 - 值 ${v2s} 不能应用指定的状态`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.ObjectDiffApplyInvalidStatusException = ObjectDiffApplyInvalidStatusException;
|
|
71
|
+
/**
|
|
72
|
+
* Object Clone:
|
|
73
|
+
* Object different merge failed.
|
|
74
|
+
* Exception will be thrown if the object element could not be accessed
|
|
75
|
+
*/
|
|
76
|
+
class ObjectDiffMergeFailedException extends Exception_1.Exception {
|
|
77
|
+
path;
|
|
78
|
+
constructor(path) {
|
|
79
|
+
super();
|
|
80
|
+
this.path = path;
|
|
81
|
+
}
|
|
82
|
+
toString() {
|
|
83
|
+
return `对象合并遇到问题 - 路径 ${this.path} 存在无法访问的对象`;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.ObjectDiffMergeFailedException = ObjectDiffMergeFailedException;
|
|
@@ -536,10 +536,23 @@ function _areaCodeToString(eArea) {
|
|
|
536
536
|
return "zh_CN";
|
|
537
537
|
}
|
|
538
538
|
}
|
|
539
|
+
/**
|
|
540
|
+
* Parse area code to general string
|
|
541
|
+
*
|
|
542
|
+
* @param eArea the area code
|
|
543
|
+
* @returns return the general string of specified area code
|
|
544
|
+
*/
|
|
539
545
|
function parseAreaCode(eArea) {
|
|
540
546
|
return _areaCodeToString(eArea);
|
|
541
547
|
}
|
|
542
548
|
exports.parseAreaCode = parseAreaCode;
|
|
549
|
+
/**
|
|
550
|
+
* Convert the string to area code
|
|
551
|
+
*
|
|
552
|
+
* @param areaStr the source string
|
|
553
|
+
* @param forceArea a boolean value indicates whether needs to have a strict check for the source string. (That means if in strict mode and the string could not be converted, an unkonwn will be returned.)
|
|
554
|
+
* @returns return the area code
|
|
555
|
+
*/
|
|
543
556
|
function parseAreaString(areaStr, forceArea) {
|
|
544
557
|
return _parseAreaString(areaStr, forceArea);
|
|
545
558
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**@format */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ObjectHelper = exports.ObjectCalculater = exports.Performance = exports.Log = exports.parseAreaString = exports.parseAreaCode = exports.ObjectDiffMergeFailedException = exports.ObjectDiffApplyInvalidStatusException = exports.ObjectMergeStatusCheckFailedException = exports.ObjectCloneFunctionNotSupportException = exports.ArgumentNullOrEmptyException = void 0;
|
|
5
|
+
var Errors_1 = require("./Errors");
|
|
6
|
+
Object.defineProperty(exports, "ArgumentNullOrEmptyException", { enumerable: true, get: function () { return Errors_1.ArgumentNullOrEmptyException; } });
|
|
7
|
+
Object.defineProperty(exports, "ObjectCloneFunctionNotSupportException", { enumerable: true, get: function () { return Errors_1.ObjectCloneFunctionNotSupportException; } });
|
|
8
|
+
Object.defineProperty(exports, "ObjectMergeStatusCheckFailedException", { enumerable: true, get: function () { return Errors_1.ObjectMergeStatusCheckFailedException; } });
|
|
9
|
+
Object.defineProperty(exports, "ObjectDiffApplyInvalidStatusException", { enumerable: true, get: function () { return Errors_1.ObjectDiffApplyInvalidStatusException; } });
|
|
10
|
+
Object.defineProperty(exports, "ObjectDiffMergeFailedException", { enumerable: true, get: function () { return Errors_1.ObjectDiffMergeFailedException; } });
|
|
11
|
+
var Language_1 = require("./Language");
|
|
12
|
+
Object.defineProperty(exports, "parseAreaCode", { enumerable: true, get: function () { return Language_1.parseAreaCode; } });
|
|
13
|
+
Object.defineProperty(exports, "parseAreaString", { enumerable: true, get: function () { return Language_1.parseAreaString; } });
|
|
14
|
+
var Log_1 = require("./Log");
|
|
15
|
+
Object.defineProperty(exports, "Log", { enumerable: true, get: function () { return Log_1.Log; } });
|
|
16
|
+
Object.defineProperty(exports, "Performance", { enumerable: true, get: function () { return Log_1.Performance; } });
|
|
17
|
+
//// object
|
|
18
|
+
var Calculater_1 = require("./object/Calculater");
|
|
19
|
+
Object.defineProperty(exports, "ObjectCalculater", { enumerable: true, get: function () { return Calculater_1.ObjectCalculater; } });
|
|
20
|
+
var Helper_1 = require("./object/Helper");
|
|
21
|
+
Object.defineProperty(exports, "ObjectHelper", { enumerable: true, get: function () { return Helper_1.ObjectHelper; } });
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**@format */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ObjectCalculater = void 0;
|
|
5
|
+
const TMap_1 = require("../../../types/TMap");
|
|
6
|
+
const PathBase_1 = require("../../../types/PathBase");
|
|
7
|
+
const Helper_1 = require("./Helper");
|
|
8
|
+
const Errors_1 = require("../Errors");
|
|
9
|
+
class ObjectDiffCalculation {
|
|
10
|
+
preObj;
|
|
11
|
+
nextObj;
|
|
12
|
+
objStack;
|
|
13
|
+
diffs;
|
|
14
|
+
constructor(pre, next) {
|
|
15
|
+
this.preObj = pre;
|
|
16
|
+
this.nextObj = next;
|
|
17
|
+
this.objStack = [];
|
|
18
|
+
this.diffs = new TMap_1.TMap();
|
|
19
|
+
}
|
|
20
|
+
calculate() {
|
|
21
|
+
this._calculate(this.preObj, this.nextObj);
|
|
22
|
+
return this.diffs;
|
|
23
|
+
}
|
|
24
|
+
_calculate(pre, next) {
|
|
25
|
+
const isPreSimple = Helper_1.ObjectHelper.isSimpleDataType(pre);
|
|
26
|
+
const isNextSimple = Helper_1.ObjectHelper.isSimpleDataType(next);
|
|
27
|
+
// 如果是基本数据类型
|
|
28
|
+
if (isPreSimple && isNextSimple) {
|
|
29
|
+
// 如果数据不相同,则记录
|
|
30
|
+
if (pre !== next) {
|
|
31
|
+
const path = new PathBase_1.PathBase(this.objStack);
|
|
32
|
+
this.diffs.set(path, {
|
|
33
|
+
path: path.toString(),
|
|
34
|
+
old: Helper_1.ObjectHelper.clone(pre),
|
|
35
|
+
new: Helper_1.ObjectHelper.clone(next),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
// 直接返回
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// 获取新老对象是否为数组类型
|
|
42
|
+
const isPreArray = Array.isArray(pre);
|
|
43
|
+
const isNextArray = Array.isArray(next);
|
|
44
|
+
if (isPreArray || isNextArray) {
|
|
45
|
+
const path = new PathBase_1.PathBase(this.objStack);
|
|
46
|
+
if (isPreArray && isNextArray) {
|
|
47
|
+
// Pre为数组 Next为数组
|
|
48
|
+
const isSame = Helper_1.ObjectHelper.compareObjects(pre, next);
|
|
49
|
+
if (isSame === "different") {
|
|
50
|
+
this.diffs.set(path, {
|
|
51
|
+
path: path.toString(),
|
|
52
|
+
old: Helper_1.ObjectHelper.clone(pre),
|
|
53
|
+
new: Helper_1.ObjectHelper.clone(next),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
// Pre或Next为数组,另外一个为object类型
|
|
59
|
+
// Pre为简单对象 & Next为数组
|
|
60
|
+
// Pre为数组 & Next为简单对象
|
|
61
|
+
this.diffs.set(path, {
|
|
62
|
+
path: path.toString(),
|
|
63
|
+
old: Helper_1.ObjectHelper.clone(pre),
|
|
64
|
+
new: Helper_1.ObjectHelper.clone(next),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// 新老对象都是Object类型,按照Object类型进行比对
|
|
70
|
+
this._calculateObject(pre, next);
|
|
71
|
+
}
|
|
72
|
+
_calculateObject(pre, next) {
|
|
73
|
+
const preKeys = Object.keys(pre);
|
|
74
|
+
const nextKeys = Object.keys(next);
|
|
75
|
+
const sameItems = [];
|
|
76
|
+
// 寻找已经删除的
|
|
77
|
+
for (const key of preKeys) {
|
|
78
|
+
if (nextKeys.includes(key)) {
|
|
79
|
+
sameItems.push(key);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
// 已经被删除的对象,储存下来
|
|
83
|
+
this.objStack.push(key);
|
|
84
|
+
const path = new PathBase_1.PathBase(this.objStack);
|
|
85
|
+
this.diffs.set(path, {
|
|
86
|
+
path: path.toString(),
|
|
87
|
+
old: Helper_1.ObjectHelper.clone(pre[key]),
|
|
88
|
+
new: undefined,
|
|
89
|
+
deleted: true,
|
|
90
|
+
});
|
|
91
|
+
this.objStack.pop();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// 寻找新加的元素
|
|
95
|
+
for (const key of nextKeys) {
|
|
96
|
+
if (!preKeys.includes(key)) {
|
|
97
|
+
// 老的对象中不存在,新加的状态储存下来
|
|
98
|
+
this.objStack.push(key);
|
|
99
|
+
const path = new PathBase_1.PathBase(this.objStack);
|
|
100
|
+
this.diffs.set(path, {
|
|
101
|
+
path: path.toString(),
|
|
102
|
+
old: undefined,
|
|
103
|
+
new: Helper_1.ObjectHelper.clone(next[key]),
|
|
104
|
+
added: true,
|
|
105
|
+
});
|
|
106
|
+
this.objStack.pop();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// 依次遍历所有相同的元素,判断其是否存在改变
|
|
110
|
+
for (const key of sameItems) {
|
|
111
|
+
this.objStack.push(key);
|
|
112
|
+
this._calculate(pre[key], next[key]);
|
|
113
|
+
this.objStack.pop();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function __checkOldAndNewStatus(pre, next) {
|
|
118
|
+
return Helper_1.ObjectHelper.compareObjects(pre, next) === "same";
|
|
119
|
+
}
|
|
120
|
+
/** Object Calculation Manager of Tianyu to provide data changes delta calculation and merge */
|
|
121
|
+
class ObjectCalculater {
|
|
122
|
+
/**
|
|
123
|
+
* Compare two objects and get the delta changes between the two objects
|
|
124
|
+
*
|
|
125
|
+
* @param previous the previous object
|
|
126
|
+
* @param newest the newest object
|
|
127
|
+
* @returns return the defferents map
|
|
128
|
+
*/
|
|
129
|
+
static calculateDiff(previous, newest) {
|
|
130
|
+
const calculationEntity = new ObjectDiffCalculation(previous, newest);
|
|
131
|
+
return calculationEntity.calculate();
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Merge the object value status from one to another one following with the object different maps
|
|
135
|
+
*
|
|
136
|
+
* @param value the raw value(status)
|
|
137
|
+
* @param diffs the value changed status map array, contains multiply status maps and the status merge will be one by one.
|
|
138
|
+
* @param strict a boolean value used to whether the program needs a strict status check to avoid some data errors
|
|
139
|
+
* @returns return a new value status
|
|
140
|
+
*/
|
|
141
|
+
static mergeDiff(value, diffs, strict) {
|
|
142
|
+
const resultObj = { root: Helper_1.ObjectHelper.clone(value) };
|
|
143
|
+
if (0 === diffs.length) {
|
|
144
|
+
return resultObj.root;
|
|
145
|
+
}
|
|
146
|
+
for (const diff of diffs) {
|
|
147
|
+
const rootPathDiff = diff.get(new PathBase_1.PathBase([]));
|
|
148
|
+
if (rootPathDiff) {
|
|
149
|
+
if (strict && diff.size() > 1) {
|
|
150
|
+
throw new Errors_1.ObjectDiffApplyInvalidStatusException(resultObj.root);
|
|
151
|
+
}
|
|
152
|
+
if (strict && !__checkOldAndNewStatus(resultObj.root, rootPathDiff.old)) {
|
|
153
|
+
throw new Errors_1.ObjectMergeStatusCheckFailedException("", rootPathDiff.deleted ? "del" : rootPathDiff.added ? "add" : "modify");
|
|
154
|
+
}
|
|
155
|
+
// 直接设置新值
|
|
156
|
+
resultObj.root = Helper_1.ObjectHelper.clone(rootPathDiff.new);
|
|
157
|
+
// 原始值为简单数据类型 或 数组类型 需要根目录修改
|
|
158
|
+
}
|
|
159
|
+
else if (Helper_1.ObjectHelper.isSimpleDataType(resultObj.root) || Array.isArray(resultObj.root)) {
|
|
160
|
+
throw new Errors_1.ObjectDiffApplyInvalidStatusException(resultObj.root);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
// 没有根目录修改
|
|
164
|
+
// 并且对象不是简单类型
|
|
165
|
+
// 按照复杂值递归进行更改
|
|
166
|
+
// 依次遍历所有的路径
|
|
167
|
+
for (const [path, info] of diff) {
|
|
168
|
+
// 此处不应该进入条件
|
|
169
|
+
if (!!!path || !!!info)
|
|
170
|
+
continue;
|
|
171
|
+
// 获取路径并预处理
|
|
172
|
+
const pathDir = path.getDirs();
|
|
173
|
+
const endDir = pathDir.pop();
|
|
174
|
+
// 此处不应该进入条件
|
|
175
|
+
if (!!!endDir)
|
|
176
|
+
continue;
|
|
177
|
+
const mergeType = info.added ? "add" : info.deleted ? "del" : "modify";
|
|
178
|
+
// 依次查询元素路径
|
|
179
|
+
// 检查父元素是否不存在(error)
|
|
180
|
+
let obj = resultObj.root;
|
|
181
|
+
for (const key of pathDir) {
|
|
182
|
+
if (!!!obj[key]) {
|
|
183
|
+
// 如果父对象不存在 在严格模式下抛出异常
|
|
184
|
+
if (strict) {
|
|
185
|
+
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
obj[key] = {};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// 目标点向下移动
|
|
192
|
+
obj = obj[key];
|
|
193
|
+
}
|
|
194
|
+
// 再次检查对象状态 (理论上不应该进入此判断)
|
|
195
|
+
if (!!!obj)
|
|
196
|
+
throw new Errors_1.ObjectDiffMergeFailedException(info.path);
|
|
197
|
+
if (info.added) {
|
|
198
|
+
// 新增一个元素
|
|
199
|
+
// 严格模式下 不允许新增对象存在
|
|
200
|
+
if (strict && obj[endDir]) {
|
|
201
|
+
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
202
|
+
}
|
|
203
|
+
// 执行拷贝
|
|
204
|
+
obj[endDir] = Helper_1.ObjectHelper.clone(info.new);
|
|
205
|
+
}
|
|
206
|
+
else if (info.deleted) {
|
|
207
|
+
// 删除一个元素
|
|
208
|
+
// 严格模式下 不允许删除对象不存在
|
|
209
|
+
if (strict && !!!obj[endDir]) {
|
|
210
|
+
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
211
|
+
}
|
|
212
|
+
// 执行删除(如果对象存在)
|
|
213
|
+
!!obj[endDir] && delete obj[endDir];
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
// 修改一个元素
|
|
217
|
+
// 严格模式下 不允许修改对象不存在
|
|
218
|
+
if (strict && (!!!obj[endDir] || __checkOldAndNewStatus(info.old, obj[endDir]))) {
|
|
219
|
+
throw new Errors_1.ObjectMergeStatusCheckFailedException(info.path, mergeType);
|
|
220
|
+
}
|
|
221
|
+
// 执行修改
|
|
222
|
+
obj[endDir] = Helper_1.ObjectHelper.clone(info.new);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return resultObj.root;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
exports.ObjectCalculater = ObjectCalculater;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**@format */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ObjectHelper = void 0;
|
|
5
|
+
const Errors_1 = require("../Errors");
|
|
6
|
+
/** Object Helper of Tianyu to provide data type checking, objects comparing, validation, data clone and other functions */
|
|
7
|
+
class ObjectHelper {
|
|
8
|
+
/**
|
|
9
|
+
* check whether the specified value does extend from nodejs native type
|
|
10
|
+
*
|
|
11
|
+
* @param {any} value supported value type of tianyu store
|
|
12
|
+
* @returns {boolean} return true if the value is based on type number, string, boolean, function, otherwise false
|
|
13
|
+
*/
|
|
14
|
+
static isSimpleDataType(value) {
|
|
15
|
+
if (!!!value)
|
|
16
|
+
return true;
|
|
17
|
+
const typeofValue = typeof value;
|
|
18
|
+
return typeofValue === "boolean" || typeofValue === "string" || typeofValue === "number" || typeofValue === "symbol";
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* get a deep copy from source
|
|
22
|
+
*
|
|
23
|
+
* @param {any} sources the specified source value
|
|
24
|
+
* @returns {any} return a new value that is independent from sources
|
|
25
|
+
*/
|
|
26
|
+
static clone(source) {
|
|
27
|
+
return ObjectHelper._clone(source);
|
|
28
|
+
}
|
|
29
|
+
static _clone(source) {
|
|
30
|
+
if (typeof source === "function") {
|
|
31
|
+
throw new Errors_1.ObjectCloneFunctionNotSupportException();
|
|
32
|
+
}
|
|
33
|
+
// check if source is simple data type, return directly
|
|
34
|
+
if (ObjectHelper.isSimpleDataType(source)) {
|
|
35
|
+
return source;
|
|
36
|
+
}
|
|
37
|
+
if (Array.isArray(source)) {
|
|
38
|
+
// if is array type
|
|
39
|
+
return ObjectHelper._cloneArray(source);
|
|
40
|
+
}
|
|
41
|
+
// otherwise, source value is a record type
|
|
42
|
+
return ObjectHelper._cloneRecordType(source);
|
|
43
|
+
}
|
|
44
|
+
static _cloneArray(source) {
|
|
45
|
+
// create a new array to save result
|
|
46
|
+
const result = [];
|
|
47
|
+
if (0 === source.length) {
|
|
48
|
+
// if source value is empty array, return directly
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
// loop all the item in the source value and to clone the item
|
|
52
|
+
for (const item of source) {
|
|
53
|
+
result.push(ObjectHelper._clone(item));
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
static _cloneRecordType(source) {
|
|
58
|
+
// get all the record key from source object
|
|
59
|
+
const keys = Object.keys(source);
|
|
60
|
+
if (0 === keys.length) {
|
|
61
|
+
// if the object is empty, return an empty object
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
const result = {};
|
|
65
|
+
// loop all the items in the source object by the key name
|
|
66
|
+
for (const keyName of keys) {
|
|
67
|
+
const sourceItem = source[keyName];
|
|
68
|
+
result[keyName] = ObjectHelper._clone(sourceItem);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Check the object whether can be stringified
|
|
74
|
+
*
|
|
75
|
+
* @param obj be checked object
|
|
76
|
+
* @returns return true is valid, otherwise false
|
|
77
|
+
*/
|
|
78
|
+
static validateSerializable(obj) {
|
|
79
|
+
// if type is function, is not invaliable to be string
|
|
80
|
+
if (typeof obj === "function") {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
// if is simple data type, is valiable
|
|
84
|
+
if (ObjectHelper.isSimpleDataType(obj)) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
// if is array, check member
|
|
88
|
+
if (Array.isArray(obj)) {
|
|
89
|
+
for (const item of obj) {
|
|
90
|
+
// contains invalid member, return false
|
|
91
|
+
if (!ObjectHelper.validateSerializable(item)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
// if is object, check member
|
|
99
|
+
const keys = Object.keys(obj);
|
|
100
|
+
for (const keyName of keys) {
|
|
101
|
+
const item = obj[keyName];
|
|
102
|
+
// contains invalid member, return false
|
|
103
|
+
if (!ObjectHelper.validateSerializable(item)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// if the object is not an object type, return false
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Compare two or more objects are the same or different
|
|
116
|
+
*
|
|
117
|
+
* @param objs the objects which need to be compared
|
|
118
|
+
* @returns same - if all the objects are same, different - if at least one object is not same to other objects
|
|
119
|
+
* @description if there are only one parameter or no parameter is provided, same result will be returned
|
|
120
|
+
*/
|
|
121
|
+
static compareObjects(...objs) {
|
|
122
|
+
// if objs less than 2, is not comparable
|
|
123
|
+
if (objs.length < 2) {
|
|
124
|
+
return "same";
|
|
125
|
+
}
|
|
126
|
+
// if the first obj is simple data type, return different if other objects is not simple obj
|
|
127
|
+
// or the type is not totally same.
|
|
128
|
+
if (ObjectHelper.isSimpleDataType(objs[0])) {
|
|
129
|
+
return ObjectHelper._compareSimpleType(...objs);
|
|
130
|
+
}
|
|
131
|
+
// if the first obj is array type, check all the objects are array and to do the detail checking
|
|
132
|
+
if (Array.isArray(objs[0])) {
|
|
133
|
+
return ObjectHelper._compareArrayType(...objs);
|
|
134
|
+
}
|
|
135
|
+
return ObjectHelper._compareObjectType(...objs);
|
|
136
|
+
}
|
|
137
|
+
static _compareSimpleType(...objs) {
|
|
138
|
+
const firstObjType = typeof objs[0];
|
|
139
|
+
for (let i = 1; i < objs.length; ++i) {
|
|
140
|
+
if (firstObjType !== typeof objs[i] || objs[0] !== objs[i]) {
|
|
141
|
+
return "different";
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return "same";
|
|
145
|
+
}
|
|
146
|
+
static _compareArrayType(...objs) {
|
|
147
|
+
for (let i = 1; i < objs.length; ++i) {
|
|
148
|
+
// check the type and array length if the obj is array type
|
|
149
|
+
if (!Array.isArray(objs[i]) || objs[i].length !== objs[0].length) {
|
|
150
|
+
return "different";
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// loop each item of the array to have a detail checking
|
|
154
|
+
for (let j = 0; j < objs[0].length; ++j) {
|
|
155
|
+
// create a checking array
|
|
156
|
+
const items = [objs[0][j]];
|
|
157
|
+
for (let i = 1; i < objs.length; ++i) {
|
|
158
|
+
// add all the items of the each object which have the same index
|
|
159
|
+
items.push(objs[i][j]);
|
|
160
|
+
}
|
|
161
|
+
const cmpRes = ObjectHelper.compareObjects(...items);
|
|
162
|
+
if (cmpRes !== "same") {
|
|
163
|
+
return cmpRes;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return "same";
|
|
167
|
+
}
|
|
168
|
+
static _compareObjectType(...objs) {
|
|
169
|
+
try {
|
|
170
|
+
// other cases, the first obj is not simple obj and array, take checking as object
|
|
171
|
+
// start add all the objects keys
|
|
172
|
+
const keysOfObjs = [];
|
|
173
|
+
keysOfObjs.push(Object.keys(objs[0]));
|
|
174
|
+
// firstly, check the types of other objects
|
|
175
|
+
for (let i = 1; i < objs.length; ++i) {
|
|
176
|
+
keysOfObjs.push(Object.keys(objs[i]));
|
|
177
|
+
}
|
|
178
|
+
// check the keys are same or different
|
|
179
|
+
if (ObjectHelper.compareObjects(...keysOfObjs) !== "same") {
|
|
180
|
+
return "different";
|
|
181
|
+
}
|
|
182
|
+
// compare all the items
|
|
183
|
+
for (const key of keysOfObjs[0]) {
|
|
184
|
+
const items = [];
|
|
185
|
+
for (const obj of objs) {
|
|
186
|
+
items.push(obj[key]);
|
|
187
|
+
}
|
|
188
|
+
// get the items compare result
|
|
189
|
+
const cmpRes = ObjectHelper.compareObjects(...items);
|
|
190
|
+
if (cmpRes !== "same") {
|
|
191
|
+
return cmpRes;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return "same";
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return "different";
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
exports.ObjectHelper = ObjectHelper;
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.guid = void 0;
|
|
5
5
|
const perf_hooks_1 = require("perf_hooks");
|
|
6
|
+
/** Generate a Guid value string */
|
|
6
7
|
function guid() {
|
|
7
8
|
let d = new Date().getTime();
|
|
8
9
|
d += perf_hooks_1.performance.now(); //use high-precision timer if available
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**@format */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.hash = exports.guid = void 0;
|
|
5
|
+
var Guid_1 = require("./Guid");
|
|
6
|
+
Object.defineProperty(exports, "guid", { enumerable: true, get: function () { return Guid_1.guid; } });
|
|
7
|
+
var Hash_1 = require("./Hash");
|
|
8
|
+
Object.defineProperty(exports, "hash", { enumerable: true, get: function () { return Hash_1.hash; } });
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,10 +2,17 @@
|
|
|
2
2
|
export { AreaCode } from "./types/AreaCode";
|
|
3
3
|
export { Exception } from "./types/Exception";
|
|
4
4
|
export { LogLevel, type ILog, type IPerfRecorder } from "./types/Logs";
|
|
5
|
+
export { type IObjectDiffInfo, type ObjectDiffMap } from "./types/Object";
|
|
6
|
+
export { PathBase } from "./types/PathBase";
|
|
5
7
|
export { EncryptOption, type ICipher } from "./types/Security";
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
+
export { TMap } from "./types/TMap";
|
|
9
|
+
export { type MapOfBoolean, type MapOfStrings, type MapOfString, type MapOfType, type CallbackAction, type CallbackActionT, type IComparable, } from "./types/Types";
|
|
10
|
+
export { PathProcessorSourceLostException, PathDirectoryValidationFailException, PathDirAndFileConvertInvaild, } from "./utilities/coding/Error";
|
|
11
|
+
export { type PathTargetType, Path } from "./utilities/coding/Path";
|
|
12
|
+
export { ArgumentNullOrEmptyException, ObjectCloneFunctionNotSupportException, ObjectMergeStatusCheckFailedException, ObjectDiffApplyInvalidStatusException, ObjectDiffMergeFailedException, } from "./utilities/core/Errors";
|
|
8
13
|
export { parseAreaCode, parseAreaString } from "./utilities/core/Language";
|
|
9
14
|
export { Log, Performance } from "./utilities/core/Log";
|
|
15
|
+
export { ObjectCalculater } from "./utilities/core/object/Calculater";
|
|
16
|
+
export { ObjectHelper } from "./utilities/core/object/Helper";
|
|
10
17
|
export { guid } from "./utilities/security/Guid";
|
|
11
18
|
export { hash } from "./utilities/security/Hash";
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/**@format */
|
|
2
|
+
/** Exception base class */
|
|
2
3
|
export declare class Exception extends Error {
|
|
3
4
|
constructor(msg?: string, options?: ErrorOptions);
|
|
5
|
+
/**
|
|
6
|
+
* Get a string of the Exception object
|
|
7
|
+
*
|
|
8
|
+
* @returns return an error message
|
|
9
|
+
*/
|
|
4
10
|
toString(): string;
|
|
5
11
|
}
|