law-common 10.28.2-beta.7 → 10.28.2-beta.8
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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class ArrayChangeTracker<T> {
|
|
2
|
+
private incomingArray;
|
|
3
|
+
private existingArray;
|
|
4
|
+
private keyProperty;
|
|
5
|
+
private unchanged;
|
|
6
|
+
private added;
|
|
7
|
+
private updated;
|
|
8
|
+
private deleted;
|
|
9
|
+
private notFound;
|
|
10
|
+
constructor(incomingArray: T[] | undefined, existingArray: T[] | undefined, keyProperty: keyof T);
|
|
11
|
+
private get existingMap();
|
|
12
|
+
unchangedItems(): T[];
|
|
13
|
+
addedItems(): T[];
|
|
14
|
+
updatedItems(): T[];
|
|
15
|
+
deletedItems(): {
|
|
16
|
+
id: T[keyof T];
|
|
17
|
+
}[];
|
|
18
|
+
notFoundItems(): {
|
|
19
|
+
id: T[keyof T];
|
|
20
|
+
}[];
|
|
21
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayChangeTracker = void 0;
|
|
4
|
+
function deepEqual(a, b) {
|
|
5
|
+
if (a === b)
|
|
6
|
+
return true;
|
|
7
|
+
if (a == null || b == null)
|
|
8
|
+
return false;
|
|
9
|
+
if (typeof a !== typeof b)
|
|
10
|
+
return false;
|
|
11
|
+
if (typeof a !== "object")
|
|
12
|
+
return a === b;
|
|
13
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
14
|
+
return false;
|
|
15
|
+
if (Array.isArray(a)) {
|
|
16
|
+
if (a.length !== b.length)
|
|
17
|
+
return false;
|
|
18
|
+
return a.every((v, i) => deepEqual(v, b[i]));
|
|
19
|
+
}
|
|
20
|
+
const keysA = Object.keys(a).sort();
|
|
21
|
+
const keysB = Object.keys(b).sort();
|
|
22
|
+
if (keysA.length !== keysB.length)
|
|
23
|
+
return false;
|
|
24
|
+
return keysA.every((key) => deepEqual(a[key], b[key]));
|
|
25
|
+
}
|
|
26
|
+
function categorizeChanges(incoming, existing) {
|
|
27
|
+
console.log("Categorizing changes between incoming and existing items");
|
|
28
|
+
console.log("Incoming items:", incoming);
|
|
29
|
+
console.log("Existing items:", existing);
|
|
30
|
+
const existingMap = new Map(existing.filter((item) => item.id !== undefined).map((item) => [item.id, item]));
|
|
31
|
+
const incomingIds = new Set(incoming.filter((item) => item.id !== undefined).map((item) => item.id));
|
|
32
|
+
const deleted = [];
|
|
33
|
+
const notFound = [];
|
|
34
|
+
const unchanged = [];
|
|
35
|
+
const updated = [];
|
|
36
|
+
const added = [];
|
|
37
|
+
for (const item of incoming) {
|
|
38
|
+
if (item.id === undefined) {
|
|
39
|
+
added.push(item);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const id = item.id;
|
|
43
|
+
if (Object.keys(item).length === 1) {
|
|
44
|
+
deleted.push({ id });
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (!existingMap.has(id)) {
|
|
48
|
+
added.push(item);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const existingItem = existingMap.get(id);
|
|
52
|
+
if (deepEqual(item, existingItem)) {
|
|
53
|
+
unchanged.push(item);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
updated.push(item);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (const [id] of existingMap) {
|
|
60
|
+
if (!incomingIds.has(id)) {
|
|
61
|
+
notFound.push({ id });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return { unchanged, added, updated, deleted, notFound };
|
|
65
|
+
}
|
|
66
|
+
class ArrayChangeTracker {
|
|
67
|
+
constructor(incomingArray = [], existingArray = [], keyProperty) {
|
|
68
|
+
this.unchanged = [];
|
|
69
|
+
this.added = [];
|
|
70
|
+
this.updated = [];
|
|
71
|
+
this.deleted = [];
|
|
72
|
+
this.notFound = [];
|
|
73
|
+
this.incomingArray = [...incomingArray];
|
|
74
|
+
this.existingArray = [...existingArray];
|
|
75
|
+
this.keyProperty = keyProperty;
|
|
76
|
+
}
|
|
77
|
+
get existingMap() {
|
|
78
|
+
return new Map(this.existingArray.filter((item) => item[this.keyProperty] !== undefined).map((item) => [item[this.keyProperty], item]));
|
|
79
|
+
}
|
|
80
|
+
unchangedItems() {
|
|
81
|
+
const existingMap = this.existingMap;
|
|
82
|
+
this.unchanged = [];
|
|
83
|
+
for (const incomingItem of this.incomingArray) {
|
|
84
|
+
const isIdentifierPresent = incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
|
|
85
|
+
const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
|
|
86
|
+
const isExistingItem = existingMap.has(incomingItem[this.keyProperty]);
|
|
87
|
+
if (!isIdentifierPresent || !isExistingItem || onlyIdentifierPresent) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const id = incomingItem[this.keyProperty];
|
|
91
|
+
const existingItem = existingMap.get(id);
|
|
92
|
+
if (deepEqual(incomingItem, existingItem)) {
|
|
93
|
+
this.unchanged.push(incomingItem);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return this.unchanged;
|
|
97
|
+
}
|
|
98
|
+
addedItems() {
|
|
99
|
+
this.added = [];
|
|
100
|
+
const existingMap = this.existingMap;
|
|
101
|
+
for (const incomingItem of this.incomingArray) {
|
|
102
|
+
if (!existingMap.has(incomingItem[this.keyProperty])) {
|
|
103
|
+
this.added.push(incomingItem);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return this.added;
|
|
108
|
+
}
|
|
109
|
+
updatedItems() {
|
|
110
|
+
this.updated = [];
|
|
111
|
+
const existingMap = this.existingMap;
|
|
112
|
+
for (const incomingItem of this.incomingArray) {
|
|
113
|
+
const isExistingItem = existingMap.has(incomingItem[this.keyProperty]);
|
|
114
|
+
if (!isExistingItem) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const id = incomingItem[this.keyProperty];
|
|
118
|
+
const existingItem = existingMap.get(id);
|
|
119
|
+
if (!deepEqual(incomingItem, existingItem)) {
|
|
120
|
+
this.updated.push(incomingItem);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return this.updated;
|
|
124
|
+
}
|
|
125
|
+
deletedItems() {
|
|
126
|
+
this.deleted = [];
|
|
127
|
+
for (const incomingItem of this.incomingArray) {
|
|
128
|
+
const isIdentifierPresent = incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
|
|
129
|
+
const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
|
|
130
|
+
if (isIdentifierPresent && onlyIdentifierPresent) {
|
|
131
|
+
this.deleted.push({ id: incomingItem[this.keyProperty] });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return this.deleted;
|
|
135
|
+
}
|
|
136
|
+
notFoundItems() {
|
|
137
|
+
this.notFound = [];
|
|
138
|
+
const existingMap = this.existingMap;
|
|
139
|
+
for (const incomingItem of this.existingArray) {
|
|
140
|
+
const isIdentifierPresent = incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
|
|
141
|
+
if (isIdentifierPresent && !existingMap.has(incomingItem[this.keyProperty])) {
|
|
142
|
+
this.notFound.push({ id: incomingItem[this.keyProperty] });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return this.notFound;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.ArrayChangeTracker = ArrayChangeTracker;
|
package/dist/src/utils/index.js
CHANGED
|
@@ -19,3 +19,4 @@ __exportStar(require("./helper.fn.util"), exports);
|
|
|
19
19
|
__exportStar(require("./models/date-code.model.util"), exports);
|
|
20
20
|
__exportStar(require("./string.util"), exports);
|
|
21
21
|
__exportStar(require("./entity.flow.util"), exports);
|
|
22
|
+
__exportStar(require("./array.util"), exports);
|