law-common 10.28.2-beta.1 → 10.28.2-beta.11
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/src/api/enums/crud.enum.d.ts +7 -0
- package/dist/src/api/enums/crud.enum.js +11 -0
- package/dist/src/api/index.d.ts +3 -0
- package/dist/src/api/index.js +3 -0
- package/dist/src/api/interface/address-book.create.dto.interface.d.ts +2 -0
- package/dist/src/api/interface/address-book.update.dto.interface.d.ts +3 -0
- package/dist/src/api/interface/address-book.update.dto.interface.js +25 -0
- package/dist/src/api/interface/api.utils.interface.d.ts +4 -0
- package/dist/src/api/interface/api.utils.interface.js +2 -0
- package/dist/src/entities/model/base.entity.model.d.ts +1 -1
- package/dist/src/entities/model/entity.model.interface.d.ts +24 -0
- package/dist/src/entities/model/entity.model.interface.js +87 -1
- package/dist/src/utils/array.util.d.ts +25 -0
- package/dist/src/utils/array.util.js +168 -0
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/object.util.d.ts +10 -0
- package/dist/src/utils/object.util.js +134 -0
- package/package.json +1 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CRUDEnum = void 0;
|
|
4
|
+
var CRUDEnum;
|
|
5
|
+
(function (CRUDEnum) {
|
|
6
|
+
CRUDEnum["CREATE"] = "CREATE";
|
|
7
|
+
CRUDEnum["READ"] = "READ";
|
|
8
|
+
CRUDEnum["UPDATE"] = "UPDATE";
|
|
9
|
+
CRUDEnum["DELETE"] = "DELETE";
|
|
10
|
+
CRUDEnum["UNCHANGED"] = "UNCHANGED";
|
|
11
|
+
})(CRUDEnum || (exports.CRUDEnum = CRUDEnum = {}));
|
package/dist/src/api/index.d.ts
CHANGED
|
@@ -49,3 +49,6 @@ export * from "./interface/work.from.home.update.interface";
|
|
|
49
49
|
export * from "./interface/cron-job-manual-trigger.dto.interface";
|
|
50
50
|
export * from "./interface/cron-job.entity.response";
|
|
51
51
|
export * from "./interface/address-book.create.dto.interface";
|
|
52
|
+
export * from "./enums/crud.enum";
|
|
53
|
+
export * from "./interface/api.utils.interface";
|
|
54
|
+
export * from "./interface/address-book.update.dto.interface";
|
package/dist/src/api/index.js
CHANGED
|
@@ -69,3 +69,6 @@ __exportStar(require("./interface/cron-job.entity.response"), exports);
|
|
|
69
69
|
// export * from "./interface/billing-reimbursement-expense-history.api";
|
|
70
70
|
// export * from "./interface/project-user-mapping.entity.api";
|
|
71
71
|
__exportStar(require("./interface/address-book.create.dto.interface"), exports);
|
|
72
|
+
__exportStar(require("./enums/crud.enum"), exports);
|
|
73
|
+
__exportStar(require("./interface/api.utils.interface"), exports);
|
|
74
|
+
__exportStar(require("./interface/address-book.update.dto.interface"), exports);
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { IAddressBookEntity, IEntityCreateDto } from "../../entities";
|
|
2
|
+
import { CRUDEnum } from "../enums/crud.enum";
|
|
2
3
|
export type IAddressBookCreateExclude = "contactDetails" | "address" | "organizationId";
|
|
3
4
|
export interface IAddressBookContactDetail {
|
|
4
5
|
id?: number;
|
|
5
6
|
name: string;
|
|
6
7
|
email?: string;
|
|
7
8
|
phone?: string;
|
|
9
|
+
operation: CRUDEnum;
|
|
8
10
|
}
|
|
9
11
|
export interface IAddressBookAddress {
|
|
10
12
|
addressLine1?: string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
// export type IAddressBookUpdateDto = DeepPartialButRequired<
|
|
4
|
+
// Omit<IAddressBookCreateDto, 'contactDetails'>,
|
|
5
|
+
// never
|
|
6
|
+
// > & {
|
|
7
|
+
// contactDetails?: DeepPartialButRequired<IAddressBookContactDetail, 'id'>[]; // id required per item
|
|
8
|
+
// // address?: DeepPartial<IAddressBookAddress>;
|
|
9
|
+
// };
|
|
10
|
+
// const a: IAddressBookUpdateDto = {
|
|
11
|
+
// organizationName: "Test Name",
|
|
12
|
+
// contactDetails: [
|
|
13
|
+
// {
|
|
14
|
+
// id: 1,
|
|
15
|
+
// email: "test@example.com",
|
|
16
|
+
// phone: "123-456-7890"
|
|
17
|
+
// }
|
|
18
|
+
// ]
|
|
19
|
+
// };
|
|
20
|
+
// const b: IAddressBookUpdateDto = {
|
|
21
|
+
// organizationName: "Test Name",
|
|
22
|
+
// address: {
|
|
23
|
+
// city: "Sample City"
|
|
24
|
+
// }
|
|
25
|
+
// };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type DeepPartial<T> = T extends object ? {
|
|
2
|
+
[P in keyof T]?: T[P] extends Array<infer U> ? Array<DeepPartial<U>> : T[P] extends object ? DeepPartial<T[P]> : T[P];
|
|
3
|
+
} : T;
|
|
4
|
+
export type DeepPartialButRequired<T, K extends keyof T> = DeepPartial<T> & Required<Pick<T, K>>;
|
|
@@ -4,7 +4,7 @@ export declare enum RelationType {
|
|
|
4
4
|
MANY = "many"
|
|
5
5
|
}
|
|
6
6
|
export declare abstract class BaseEntityModel<T extends EntityEnum | VirtualEntityEnum> {
|
|
7
|
-
|
|
7
|
+
readonly entityName: T;
|
|
8
8
|
constructor(entityName: T);
|
|
9
9
|
abstract getRelationConfigs(): any[];
|
|
10
10
|
populateRelationsByIndex(entityIndexMap: EntityIndexMap): void;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EntityEnum, EntityIndexMap, EntityMap, EnumEntityType, EnumToModel, IApiEntity, IBaseEntityApiResponse, VirtualEntityEnum } from "../interface/entity.utils.interface";
|
|
2
|
+
import { BaseEntityModel } from "./base.entity.model";
|
|
2
3
|
export declare function mapToIndex(entityMap: EntityMap): EntityIndexMap;
|
|
3
4
|
export declare function getEntityIndexMap<T extends EntityEnum>(data: IBaseEntityApiResponse<EnumEntityType<T>>, baseEntity: T, reusedConfig?: {
|
|
4
5
|
existingEntityIndexMap: EntityIndexMap;
|
|
@@ -16,3 +17,26 @@ export declare function parseEntitiesWithoutModels<T extends EnumEntityType<Enti
|
|
|
16
17
|
}): {
|
|
17
18
|
[E in EntityEnum | VirtualEntityEnum]?: IApiEntity<EnumEntityType<E>>[];
|
|
18
19
|
};
|
|
20
|
+
export declare function removeEntityById(entityIndexMap: EntityIndexMap, entity: EntityEnum, id: number): void;
|
|
21
|
+
export declare class EntityModelRelationHelper {
|
|
22
|
+
entityMap: EntityMap;
|
|
23
|
+
entityModelMap: EntityMap;
|
|
24
|
+
entityModelIndexMap: EntityIndexMap;
|
|
25
|
+
constructor(entityMap: EntityMap);
|
|
26
|
+
private init;
|
|
27
|
+
private toEntityModelMap;
|
|
28
|
+
fromEntityToEntityModel<T extends EntityEnum | VirtualEntityEnum>(entity: EnumEntityType<T>, entityEnum: T): BaseEntityModel<T>;
|
|
29
|
+
private toEntityModelIndexMap;
|
|
30
|
+
populateRelationsByEntityEnums(entityEnums: EntityEnum[]): EntityModelRelationHelper;
|
|
31
|
+
populateRelationsByEntityEnum(entityEnum: EntityEnum): void;
|
|
32
|
+
populateRelationsByEntityModel(entityModel: BaseEntityModel<EntityEnum | VirtualEntityEnum>): void;
|
|
33
|
+
addEntityModel<T extends EntityEnum | VirtualEntityEnum>(entityModel: BaseEntityModel<T>, config?: {
|
|
34
|
+
populateRelations?: boolean;
|
|
35
|
+
}): void;
|
|
36
|
+
addEntity<T extends EntityEnum | VirtualEntityEnum>(entity: EnumEntityType<T>, entityEnum: T, config?: {
|
|
37
|
+
populateRelations?: boolean;
|
|
38
|
+
}): void;
|
|
39
|
+
updateEntityModel<T extends EntityEnum | VirtualEntityEnum>(entityModel: BaseEntityModel<T>, config?: {
|
|
40
|
+
populateRelations?: boolean;
|
|
41
|
+
}): void;
|
|
42
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.entityEnumToEntityModel = void 0;
|
|
3
|
+
exports.EntityModelRelationHelper = exports.entityEnumToEntityModel = void 0;
|
|
4
4
|
exports.mapToIndex = mapToIndex;
|
|
5
5
|
exports.getEntityIndexMap = getEntityIndexMap;
|
|
6
6
|
exports.populateRelationsFor = populateRelationsFor;
|
|
@@ -8,6 +8,7 @@ exports.entityMapToModels = entityMapToModels;
|
|
|
8
8
|
exports.parseEntities = parseEntities;
|
|
9
9
|
exports.entityMapToEntityIndexMap = entityMapToEntityIndexMap;
|
|
10
10
|
exports.parseEntitiesWithoutModels = parseEntitiesWithoutModels;
|
|
11
|
+
exports.removeEntityById = removeEntityById;
|
|
11
12
|
const entity_utils_interface_1 = require("../interface/entity.utils.interface");
|
|
12
13
|
const bank_entity_model_1 = require("./bank.entity.model");
|
|
13
14
|
const base_entity_model_1 = require("./base.entity.model");
|
|
@@ -125,3 +126,88 @@ function parseEntitiesWithoutModels(json, baseEntity, entityMap = {}) {
|
|
|
125
126
|
}
|
|
126
127
|
return entityMap;
|
|
127
128
|
}
|
|
129
|
+
function removeEntityById(entityIndexMap, entity, id) {
|
|
130
|
+
if (entityIndexMap[entity]) {
|
|
131
|
+
delete entityIndexMap[entity][id];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
class EntityModelRelationHelper {
|
|
135
|
+
constructor(entityMap) {
|
|
136
|
+
this.entityMap = entityMap;
|
|
137
|
+
this.entityModelMap = {};
|
|
138
|
+
this.entityModelIndexMap = {};
|
|
139
|
+
this.init();
|
|
140
|
+
}
|
|
141
|
+
init() {
|
|
142
|
+
this.toEntityModelMap();
|
|
143
|
+
this.toEntityModelIndexMap();
|
|
144
|
+
}
|
|
145
|
+
toEntityModelMap() {
|
|
146
|
+
for (const entityName in this.entityMap) {
|
|
147
|
+
if (!(entityName in exports.entityEnumToEntityModel)) {
|
|
148
|
+
throw new Error(`Unknown entity: ${entityName}`);
|
|
149
|
+
}
|
|
150
|
+
this.entityModelMap[entityName] = this.entityMap[entityName].map((entity) =>
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
exports.entityEnumToEntityModel[entityName](entity));
|
|
153
|
+
}
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
fromEntityToEntityModel(entity, entityEnum) {
|
|
157
|
+
if (!(entityEnum in exports.entityEnumToEntityModel)) {
|
|
158
|
+
throw new Error(`Unknown entity: ${entityEnum}`);
|
|
159
|
+
}
|
|
160
|
+
return exports.entityEnumToEntityModel[entityEnum](entity);
|
|
161
|
+
}
|
|
162
|
+
toEntityModelIndexMap() {
|
|
163
|
+
this.entityModelIndexMap = Object.keys(this.entityModelMap).reduce((acc, key) => {
|
|
164
|
+
// @ts-ignore
|
|
165
|
+
acc[key] = this.entityModelMap[key].reduce((innerAcc, entity) => {
|
|
166
|
+
innerAcc[entity.id] = entity;
|
|
167
|
+
return innerAcc;
|
|
168
|
+
}, {});
|
|
169
|
+
return acc;
|
|
170
|
+
}, {});
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
populateRelationsByEntityEnums(entityEnums) {
|
|
174
|
+
entityEnums.forEach((entityEnum) => {
|
|
175
|
+
this.populateRelationsByEntityEnum(entityEnum);
|
|
176
|
+
});
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
populateRelationsByEntityEnum(entityEnum) {
|
|
180
|
+
for (const key of Object.keys(this.entityModelIndexMap[entityEnum] || {})) {
|
|
181
|
+
const entityModel = this.entityModelIndexMap[entityEnum][key];
|
|
182
|
+
entityModel.populateRelationsByIndex(this.entityModelIndexMap);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
populateRelationsByEntityModel(entityModel) {
|
|
186
|
+
entityModel.populateRelationsByIndex(this.entityModelIndexMap);
|
|
187
|
+
}
|
|
188
|
+
addEntityModel(entityModel, config = { populateRelations: false }) {
|
|
189
|
+
const entityEnum = entityModel.entityName;
|
|
190
|
+
this.entityModelIndexMap[entityEnum] = this.entityModelIndexMap[entityEnum] || {};
|
|
191
|
+
// @ts-ignore
|
|
192
|
+
this.entityModelIndexMap[entityEnum][entityModel.id] = entityModel;
|
|
193
|
+
if (config.populateRelations) {
|
|
194
|
+
this.populateRelationsByEntityModel(entityModel);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
addEntity(entity, entityEnum, config = { populateRelations: false }) {
|
|
198
|
+
if (!(entityEnum in exports.entityEnumToEntityModel)) {
|
|
199
|
+
throw new Error(`Unknown entity: ${entityEnum}`);
|
|
200
|
+
}
|
|
201
|
+
this.addEntityModel(this.fromEntityToEntityModel(entity, entityEnum), config);
|
|
202
|
+
}
|
|
203
|
+
updateEntityModel(entityModel, config = { populateRelations: false }) {
|
|
204
|
+
const entityEnum = entityModel.entityName;
|
|
205
|
+
this.entityModelIndexMap[entityEnum] = this.entityModelIndexMap[entityEnum] || {};
|
|
206
|
+
// @ts-ignore
|
|
207
|
+
this.entityModelIndexMap[entityEnum][entityModel.id] = entityModel;
|
|
208
|
+
if (config.populateRelations) {
|
|
209
|
+
this.populateRelationsByEntityModel(entityModel);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.EntityModelRelationHelper = EntityModelRelationHelper;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ArrayComparisonCategory<T> = {
|
|
2
|
+
unchanged: T[];
|
|
3
|
+
added: T[];
|
|
4
|
+
updated: T[];
|
|
5
|
+
deleted: {
|
|
6
|
+
[x: string]: T[keyof T];
|
|
7
|
+
}[];
|
|
8
|
+
notFound: {
|
|
9
|
+
[x: string]: T[keyof T];
|
|
10
|
+
}[];
|
|
11
|
+
};
|
|
12
|
+
export declare class ArrayComparisonCategorizer<T> {
|
|
13
|
+
private incomingArray;
|
|
14
|
+
private existingArray;
|
|
15
|
+
private keyProperty;
|
|
16
|
+
private arrayComparisonCategory;
|
|
17
|
+
constructor(incomingArray: T[] | undefined, existingArray: T[] | undefined, keyProperty: keyof T);
|
|
18
|
+
private get existingMap();
|
|
19
|
+
private isExistingItem;
|
|
20
|
+
private isMarkForDeletion;
|
|
21
|
+
private isIdentifierPresent;
|
|
22
|
+
compare(): ArrayComparisonCategory<T>;
|
|
23
|
+
get category(): ArrayComparisonCategory<T> | null;
|
|
24
|
+
merge(): T[];
|
|
25
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayComparisonCategorizer = 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
|
+
class ArrayComparisonCategorizer {
|
|
27
|
+
constructor(incomingArray = [], existingArray = [], keyProperty) {
|
|
28
|
+
this.arrayComparisonCategory = null;
|
|
29
|
+
this.incomingArray = [...incomingArray];
|
|
30
|
+
this.existingArray = [...existingArray];
|
|
31
|
+
this.keyProperty = keyProperty;
|
|
32
|
+
}
|
|
33
|
+
get existingMap() {
|
|
34
|
+
return new Map(this.existingArray.filter((item) => item[this.keyProperty] !== undefined).map((item) => [item[this.keyProperty], item]));
|
|
35
|
+
}
|
|
36
|
+
isExistingItem(incomingItem, existingMap) {
|
|
37
|
+
return existingMap.has(incomingItem[this.keyProperty]);
|
|
38
|
+
}
|
|
39
|
+
isMarkForDeletion(incomingItem) {
|
|
40
|
+
const onlyIdentifierPresent = Object.keys(incomingItem).length === 1;
|
|
41
|
+
return this.isIdentifierPresent(incomingItem) && onlyIdentifierPresent;
|
|
42
|
+
}
|
|
43
|
+
isIdentifierPresent(incomingItem) {
|
|
44
|
+
return incomingItem[this.keyProperty] !== undefined && incomingItem[this.keyProperty] !== null;
|
|
45
|
+
}
|
|
46
|
+
compare() {
|
|
47
|
+
const existingMap = this.existingMap;
|
|
48
|
+
const unchanged = [];
|
|
49
|
+
const added = [];
|
|
50
|
+
const updated = [];
|
|
51
|
+
const deleted = [];
|
|
52
|
+
const notFound = [];
|
|
53
|
+
for (const incomingItem of this.incomingArray) {
|
|
54
|
+
const isIdentifierPresent = this.isIdentifierPresent(incomingItem);
|
|
55
|
+
const isMarkForDeletion = isIdentifierPresent && this.isMarkForDeletion(incomingItem);
|
|
56
|
+
const isExistingItem = isIdentifierPresent && this.isExistingItem(incomingItem, existingMap);
|
|
57
|
+
const existingItem = isExistingItem ? existingMap.get(incomingItem[this.keyProperty]) : null;
|
|
58
|
+
if (isMarkForDeletion) {
|
|
59
|
+
deleted.push({ [this.keyProperty]: incomingItem[this.keyProperty] });
|
|
60
|
+
}
|
|
61
|
+
else if (!isIdentifierPresent && !isExistingItem) {
|
|
62
|
+
added.push(incomingItem);
|
|
63
|
+
}
|
|
64
|
+
else if (isExistingItem && isIdentifierPresent) {
|
|
65
|
+
if (deepEqual(incomingItem, existingItem)) {
|
|
66
|
+
unchanged.push(incomingItem);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
updated.push(incomingItem);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
notFound.push({ [this.keyProperty]: incomingItem[this.keyProperty] });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const result = {
|
|
77
|
+
unchanged,
|
|
78
|
+
added,
|
|
79
|
+
updated,
|
|
80
|
+
deleted,
|
|
81
|
+
notFound
|
|
82
|
+
};
|
|
83
|
+
this.arrayComparisonCategory = result;
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
get category() {
|
|
87
|
+
return this.arrayComparisonCategory;
|
|
88
|
+
}
|
|
89
|
+
merge() {
|
|
90
|
+
if (!this.arrayComparisonCategory) {
|
|
91
|
+
throw new Error("ArrayComparisonCategory is not computed yet. Call compare() first.");
|
|
92
|
+
}
|
|
93
|
+
const changes = this.arrayComparisonCategory;
|
|
94
|
+
let newState = this.existingArray.filter(this.isIdentifierPresent.bind(this));
|
|
95
|
+
// Remove deleted items
|
|
96
|
+
const deletedIds = new Set(changes.deleted.map((d) => d[this.keyProperty]));
|
|
97
|
+
newState = newState.filter((item) => !deletedIds.has(item[this.keyProperty]));
|
|
98
|
+
// Update items
|
|
99
|
+
for (let i = 0; i < newState.length; i++) {
|
|
100
|
+
const item = newState[i];
|
|
101
|
+
const updatedItem = changes.updated.find((u) => u[this.keyProperty] === item[this.keyProperty]);
|
|
102
|
+
if (updatedItem) {
|
|
103
|
+
newState[i] = updatedItem;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Get Max Id
|
|
107
|
+
const existingWithId = this.existingArray.filter(this.isIdentifierPresent.bind(this));
|
|
108
|
+
const originalMaxId = existingWithId.length > 0
|
|
109
|
+
? Math.max(...existingWithId.map((item) => item[this.keyProperty]))
|
|
110
|
+
: 0;
|
|
111
|
+
// Add new items with new identifier
|
|
112
|
+
let currentMaxId = originalMaxId;
|
|
113
|
+
for (const item of changes.added) {
|
|
114
|
+
const id = ++currentMaxId;
|
|
115
|
+
newState.push(Object.assign(Object.assign({}, item), { [this.keyProperty]: id }));
|
|
116
|
+
}
|
|
117
|
+
return newState;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.ArrayComparisonCategorizer = ArrayComparisonCategorizer;
|
|
121
|
+
// function categorizeChanges<T extends Item>(
|
|
122
|
+
// incoming: T[],
|
|
123
|
+
// existing: T[]
|
|
124
|
+
// ): {
|
|
125
|
+
// unchanged: T[];
|
|
126
|
+
// added: T[];
|
|
127
|
+
// updated: T[];
|
|
128
|
+
// deleted: { id: number }[];
|
|
129
|
+
// notFound: { id: number }[];
|
|
130
|
+
// } {
|
|
131
|
+
// console.log("Categorizing changes between incoming and existing items");
|
|
132
|
+
// console.log("Incoming items:", incoming);
|
|
133
|
+
// console.log("Existing items:", existing);
|
|
134
|
+
// const existingMap = new Map<number, T>(existing.filter((item) => item.id !== undefined).map((item) => [item.id!, item]));
|
|
135
|
+
// const incomingIds = new Set<number>(incoming.filter((item) => item.id !== undefined).map((item) => item.id!));
|
|
136
|
+
// const deleted: { id: number }[] = [];
|
|
137
|
+
// const notFound: { id: number }[] = [];
|
|
138
|
+
// const unchanged: T[] = [];
|
|
139
|
+
// const updated: T[] = [];
|
|
140
|
+
// const added: T[] = [];
|
|
141
|
+
// for (const item of incoming) {
|
|
142
|
+
// if (item.id === undefined) {
|
|
143
|
+
// added.push(item);
|
|
144
|
+
// continue;
|
|
145
|
+
// }
|
|
146
|
+
// const id = item.id;
|
|
147
|
+
// if (Object.keys(item).length === 1) {
|
|
148
|
+
// deleted.push({ id });
|
|
149
|
+
// continue;
|
|
150
|
+
// }
|
|
151
|
+
// if (!existingMap.has(id)) {
|
|
152
|
+
// added.push(item);
|
|
153
|
+
// continue;
|
|
154
|
+
// }
|
|
155
|
+
// const existingItem = existingMap.get(id)!;
|
|
156
|
+
// if (deepEqual(item, existingItem)) {
|
|
157
|
+
// unchanged.push(item);
|
|
158
|
+
// } else {
|
|
159
|
+
// updated.push(item);
|
|
160
|
+
// }
|
|
161
|
+
// }
|
|
162
|
+
// for (const [id] of existingMap) {
|
|
163
|
+
// if (!incomingIds.has(id)) {
|
|
164
|
+
// notFound.push({ id });
|
|
165
|
+
// }
|
|
166
|
+
// }
|
|
167
|
+
// return { unchanged, added, updated, deleted, notFound };
|
|
168
|
+
// }
|
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);
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
class ObjectComparator {
|
|
3
|
+
constructor(incoming, existing) {
|
|
4
|
+
this.incoming = incoming;
|
|
5
|
+
this.existing = existing;
|
|
6
|
+
}
|
|
7
|
+
compare() {
|
|
8
|
+
return this.deepDiff(this.incoming, this.existing);
|
|
9
|
+
}
|
|
10
|
+
deepDiff(a, b) {
|
|
11
|
+
// Handle null/undefined cases
|
|
12
|
+
if (a == null && b == null) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
if (a == null || b == null) {
|
|
16
|
+
return a;
|
|
17
|
+
}
|
|
18
|
+
// Handle primitives (loose equality)
|
|
19
|
+
if (typeof a !== "object") {
|
|
20
|
+
return a == b ? undefined : a;
|
|
21
|
+
}
|
|
22
|
+
// a is object or array
|
|
23
|
+
if (Array.isArray(a)) {
|
|
24
|
+
// Handle as array
|
|
25
|
+
const diffArr = new Array(a.length);
|
|
26
|
+
let hasDiff = false;
|
|
27
|
+
for (let i = 0; i < a.length; i++) {
|
|
28
|
+
const subB = Array.isArray(b) && i < b.length ? b[i] : undefined;
|
|
29
|
+
const subDiff = this.deepDiff(a[i], subB);
|
|
30
|
+
if (subDiff !== undefined) {
|
|
31
|
+
diffArr[i] = subDiff;
|
|
32
|
+
hasDiff = true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return hasDiff ? diffArr : undefined;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// Handle as object
|
|
39
|
+
const diff = {};
|
|
40
|
+
let hasDiff = false;
|
|
41
|
+
for (const key of Object.keys(a)) {
|
|
42
|
+
const subDiff = this.deepDiff(a[key], b === null || b === void 0 ? void 0 : b[key]);
|
|
43
|
+
if (subDiff !== undefined) {
|
|
44
|
+
diff[key] = subDiff;
|
|
45
|
+
hasDiff = true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return hasDiff ? diff : undefined;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// // Comprehensive Usage Examples
|
|
53
|
+
// // Example 1: Simple object with primitive differences
|
|
54
|
+
// console.log("Example 1: Simple object");
|
|
55
|
+
// const incoming1 = { name: "Alice", age: 30, city: "New York" };
|
|
56
|
+
// const existing1 = { name: "Bob", age: 30, city: "Boston" };
|
|
57
|
+
// const comparator1 = new ObjectComparator(incoming1, existing1);
|
|
58
|
+
// console.log(comparator1.compare()); // Output: { name: 'Alice', city: 'New York' }
|
|
59
|
+
// // Example 2: Nested objects
|
|
60
|
+
// console.log("Example 2: Nested objects");
|
|
61
|
+
// const incoming2 = {
|
|
62
|
+
// user: { id: 1, profile: { email: "alice@example.com", phone: "123-456" } },
|
|
63
|
+
// settings: { theme: "dark", notifications: true },
|
|
64
|
+
// };
|
|
65
|
+
// const existing2 = {
|
|
66
|
+
// user: { id: 1, profile: { email: "bob@example.com" } },
|
|
67
|
+
// settings: { theme: "light" },
|
|
68
|
+
// };
|
|
69
|
+
// const comparator2 = new ObjectComparator(incoming2, existing2);
|
|
70
|
+
// console.log(comparator2.compare());
|
|
71
|
+
// // Output: {
|
|
72
|
+
// // user: { profile: { email: 'alice@example.com', phone: '123-456' } },
|
|
73
|
+
// // settings: { theme: 'dark', notifications: true }
|
|
74
|
+
// // }
|
|
75
|
+
// // Example 3: Array of primitives
|
|
76
|
+
// console.log("Example 3: Array of primitives");
|
|
77
|
+
// const incoming3 = { scores: [95, 87, 100] };
|
|
78
|
+
// const existing3 = { scores: [80, 87, 90] };
|
|
79
|
+
// const comparator3 = new ObjectComparator(incoming3, existing3);
|
|
80
|
+
// console.log(comparator3.compare()); // Output: { scores: [95, undefined, 100] }
|
|
81
|
+
// // Example 4: Array of objects
|
|
82
|
+
// console.log("Example 4: Array of objects");
|
|
83
|
+
// const incoming4 = {
|
|
84
|
+
// items: [
|
|
85
|
+
// { id: 1, name: "Item A", price: 10 },
|
|
86
|
+
// { id: 2, name: "Item B", price: 20 },
|
|
87
|
+
// { id: 3, name: "Item C", price: 30 },
|
|
88
|
+
// ],
|
|
89
|
+
// };
|
|
90
|
+
// const existing4 = {
|
|
91
|
+
// items: [
|
|
92
|
+
// { id: 1, name: "Item A", price: 15 },
|
|
93
|
+
// { id: 2, name: "Item X", price: 20 },
|
|
94
|
+
// ],
|
|
95
|
+
// };
|
|
96
|
+
// const comparator4 = new ObjectComparator(incoming4, existing4);
|
|
97
|
+
// console.log(comparator4.compare());
|
|
98
|
+
// // Output: {
|
|
99
|
+
// // items: [
|
|
100
|
+
// // { price: 10 },
|
|
101
|
+
// // { name: 'Item B' },
|
|
102
|
+
// // { id: 3, name: 'Item C', price: 30 }
|
|
103
|
+
// // ]
|
|
104
|
+
// // }
|
|
105
|
+
// // Example 5: Mixed structure with missing properties and nulls
|
|
106
|
+
// console.log("Example 5: Mixed with nulls and missing");
|
|
107
|
+
// const incoming5 = {
|
|
108
|
+
// data: {
|
|
109
|
+
// value: null,
|
|
110
|
+
// list: [1, 2, { nested: "hello" }],
|
|
111
|
+
// extra: "new",
|
|
112
|
+
// },
|
|
113
|
+
// };
|
|
114
|
+
// const existing5 = {
|
|
115
|
+
// data: {
|
|
116
|
+
// value: 42,
|
|
117
|
+
// list: [1, 3],
|
|
118
|
+
// },
|
|
119
|
+
// };
|
|
120
|
+
// const comparator5 = new ObjectComparator(incoming5, existing5);
|
|
121
|
+
// console.log(comparator5.compare());
|
|
122
|
+
// // Output: {
|
|
123
|
+
// // data: {
|
|
124
|
+
// // value: null,
|
|
125
|
+
// // list: [undefined, 2, { nested: 'hello' }],
|
|
126
|
+
// // extra: 'new'
|
|
127
|
+
// // }
|
|
128
|
+
// // }
|
|
129
|
+
// // Example 6: No differences
|
|
130
|
+
// console.log("Example 6: No differences");
|
|
131
|
+
// const incoming6 = { a: 1, b: { c: [true] } };
|
|
132
|
+
// const existing6 = { a: 1, b: { c: [true] } };
|
|
133
|
+
// const comparator6 = new ObjectComparator(incoming6, existing6);
|
|
134
|
+
// console.log(comparator6.compare()); // Output: {}
|