@aitianyu.cn/types 0.0.11 → 0.0.13
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 +54 -54
- package/dist/lib/types/AreaCode.js +269 -269
- package/dist/lib/types/Exception.js +19 -19
- package/dist/lib/types/Logs.js +20 -20
- package/dist/lib/types/Object.js +3 -3
- package/dist/lib/types/PathBase.js +109 -109
- package/dist/lib/types/Security.js +10 -10
- package/dist/lib/types/TMap.js +138 -138
- package/dist/lib/types/Types.js +3 -3
- package/dist/lib/types/index.js +17 -17
- package/dist/lib/utilities/coding/Error.js +44 -44
- package/dist/lib/utilities/coding/Path.js +198 -198
- package/dist/lib/utilities/coding/index.js +10 -10
- package/dist/lib/utilities/core/Errors.js +86 -86
- package/dist/lib/utilities/core/Language.js +559 -559
- package/dist/lib/utilities/core/Log.js +108 -108
- package/dist/lib/utilities/core/TypeConvertion.js +25 -25
- package/dist/lib/utilities/core/index.js +21 -21
- package/dist/lib/utilities/core/interface/ITJSON.js +3 -0
- package/dist/lib/utilities/core/interface/ITianyuType.js +3 -0
- package/dist/lib/utilities/core/object/ArrayHelper.js +35 -35
- package/dist/lib/utilities/core/object/Calculater.js +230 -230
- package/dist/lib/utilities/core/object/Helper.js +201 -201
- package/dist/lib/utilities/core/object/StringHelper.js +22 -22
- package/dist/lib/utilities/core/type/TArray.js +7 -0
- package/dist/lib/utilities/core/type/TBoolean.js +7 -0
- package/dist/lib/utilities/core/type/TJSON.js +7 -0
- package/dist/lib/utilities/core/type/TNumber.js +7 -0
- package/dist/lib/utilities/core/type/TObject.js +7 -0
- package/dist/lib/utilities/core/type/TString.js +7 -0
- package/dist/lib/utilities/security/Guid.js +20 -20
- package/dist/lib/utilities/security/Hash.js +15 -15
- package/dist/lib/utilities/security/index.js +8 -8
- package/dist/types/index.d.ts +21 -21
- package/dist/types/types/AreaCode.d.ts +135 -135
- package/dist/types/types/Exception.d.ts +11 -11
- package/dist/types/types/Logs.d.ts +64 -64
- package/dist/types/types/Object.d.ts +18 -18
- package/dist/types/types/PathBase.d.ts +76 -76
- package/dist/types/types/Security.d.ts +23 -23
- package/dist/types/types/TMap.d.ts +80 -80
- package/dist/types/types/Types.d.ts +25 -20
- package/dist/types/types/index.d.ts +9 -9
- package/dist/types/utilities/coding/Error.d.ts +27 -27
- package/dist/types/utilities/coding/Path.d.ts +61 -61
- package/dist/types/utilities/coding/index.d.ts +3 -3
- package/dist/types/utilities/core/Errors.d.ts +47 -47
- package/dist/types/utilities/core/Language.d.ts +17 -17
- package/dist/types/utilities/core/Log.d.ts +12 -12
- package/dist/types/utilities/core/TypeConvertion.d.ts +2 -2
- package/dist/types/utilities/core/index.d.ts +6 -6
- package/dist/types/utilities/core/interface/ITJSON.d.ts +5 -0
- package/dist/types/utilities/core/interface/ITianyuType.d.ts +5 -0
- package/dist/types/utilities/core/object/ArrayHelper.d.ts +11 -11
- package/dist/types/utilities/core/object/Calculater.d.ts +22 -22
- package/dist/types/utilities/core/object/Helper.d.ts +39 -39
- package/dist/types/utilities/core/object/StringHelper.d.ts +4 -4
- package/dist/types/utilities/core/type/TArray.d.ts +3 -0
- package/dist/types/utilities/core/type/TBoolean.d.ts +3 -0
- package/dist/types/utilities/core/type/TJSON.d.ts +3 -0
- package/dist/types/utilities/core/type/TNumber.d.ts +3 -0
- package/dist/types/utilities/core/type/TObject.d.ts +3 -0
- package/dist/types/utilities/core/type/TString.d.ts +3 -0
- package/dist/types/utilities/security/Guid.d.ts +3 -3
- package/dist/types/utilities/security/Hash.d.ts +3 -3
- package/dist/types/utilities/security/index.d.ts +3 -3
- package/package.json +51 -51
|
@@ -1,230 +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;
|
|
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;
|