@contrail/util 1.0.42 → 1.0.43
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.
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
1
|
+
import { TypeProperty } from "@contrail/types";
|
|
2
|
+
export declare function applyChangesIfEqual(target: any, priorSourceValues: any, newSourceValues: any, properties: any): void;
|
|
3
|
+
export declare function determineChangesIfEqual(target: any, priorSourceValues: any, newSourceValues: any, properties: any): any;
|
|
4
|
+
export declare function areItemPropertyValuesEqual(property: TypeProperty, itemOne: any, itemTwo: any): boolean;
|
|
@@ -1,23 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.determineChangesIfEqual = exports.applyChangesIfEqual = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
exports.areItemPropertyValuesEqual = exports.determineChangesIfEqual = exports.applyChangesIfEqual = void 0;
|
|
4
|
+
const types_1 = require("@contrail/types");
|
|
5
|
+
const compareDeep_1 = require("../compareDeep/compareDeep");
|
|
6
|
+
function applyChangesIfEqual(target, priorSourceValues, newSourceValues, properties) {
|
|
7
|
+
const changes = determineChangesIfEqual(target, priorSourceValues, newSourceValues, properties);
|
|
6
8
|
if (changes) {
|
|
7
9
|
Object.assign(target, changes);
|
|
8
10
|
}
|
|
9
11
|
}
|
|
10
12
|
exports.applyChangesIfEqual = applyChangesIfEqual;
|
|
11
|
-
function determineChangesIfEqual(target, priorSourceValues, newSourceValues,
|
|
13
|
+
function determineChangesIfEqual(target, priorSourceValues, newSourceValues, properties) {
|
|
12
14
|
const changes = {};
|
|
13
|
-
for (const
|
|
14
|
-
|
|
15
|
+
for (const property of properties) {
|
|
16
|
+
const propertyKey = getPropertyKey(property);
|
|
17
|
+
if (newSourceValues[propertyKey] === undefined) {
|
|
15
18
|
continue;
|
|
16
19
|
}
|
|
17
|
-
if (
|
|
18
|
-
changes[
|
|
20
|
+
if (areItemPropertyValuesEqual(property, priorSourceValues, target)) {
|
|
21
|
+
changes[propertyKey] = newSourceValues[propertyKey];
|
|
22
|
+
if ([types_1.PropertyType.ObjectReference, types_1.PropertyType.UserList, types_1.PropertyType.TypeReference].includes(property.propertyType)) {
|
|
23
|
+
changes[property.slug] = newSourceValues[property.slug];
|
|
24
|
+
}
|
|
19
25
|
}
|
|
20
26
|
}
|
|
21
27
|
return changes;
|
|
22
28
|
}
|
|
23
29
|
exports.determineChangesIfEqual = determineChangesIfEqual;
|
|
30
|
+
function areItemPropertyValuesEqual(property, itemOne, itemTwo) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
const propertyKey = getPropertyKey(property);
|
|
33
|
+
const propertyValueOne = itemOne[propertyKey];
|
|
34
|
+
const propertyValueTwo = itemTwo[propertyKey];
|
|
35
|
+
if (property.propertyType === types_1.PropertyType.MultiSelect) {
|
|
36
|
+
return areArraysEqualIgnoreOrder(propertyValueOne, propertyValueTwo);
|
|
37
|
+
}
|
|
38
|
+
if (property.propertyType === types_1.PropertyType.SizeRange) {
|
|
39
|
+
const isOfSameSizeRangeTemplate = ((_a = itemOne === null || itemOne === void 0 ? void 0 : itemOne.sizeRangeTemplate) === null || _a === void 0 ? void 0 : _a.id) === ((_b = itemTwo === null || itemTwo === void 0 ? void 0 : itemTwo.sizeRangeTemplate) === null || _b === void 0 ? void 0 : _b.id);
|
|
40
|
+
if (!isOfSameSizeRangeTemplate)
|
|
41
|
+
return false;
|
|
42
|
+
return areSizeRangesEqual(propertyValueOne, propertyValueTwo);
|
|
43
|
+
}
|
|
44
|
+
return propertyValueOne === propertyValueTwo;
|
|
45
|
+
}
|
|
46
|
+
exports.areItemPropertyValuesEqual = areItemPropertyValuesEqual;
|
|
47
|
+
function areSizeRangesEqual(sizeRangeOne, sizeRangeTwo) {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
if (!sizeRangeOne && !sizeRangeTwo) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
if (!sizeRangeOne || !sizeRangeTwo || ((_a = sizeRangeOne.sizes) === null || _a === void 0 ? void 0 : _a.length) !== ((_b = sizeRangeTwo.sizes) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const result = (0, compareDeep_1.getObjectDiffs)(sizeRangeOne, sizeRangeTwo, '');
|
|
56
|
+
return result.length === 0;
|
|
57
|
+
}
|
|
58
|
+
function areArraysEqualIgnoreOrder(arrayOne, arrayTwo) {
|
|
59
|
+
if (!arrayOne && !arrayTwo) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if ((arrayOne === null || arrayOne === void 0 ? void 0 : arrayOne.length) !== (arrayTwo === null || arrayTwo === void 0 ? void 0 : arrayTwo.length)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const sortedArray1 = arrayOne.slice().sort();
|
|
66
|
+
const sortedArray2 = arrayTwo.slice().sort();
|
|
67
|
+
const areArraysEqual = sortedArray1.every((element, index) => element === sortedArray2[index]);
|
|
68
|
+
return areArraysEqual;
|
|
69
|
+
}
|
|
70
|
+
function getPropertyKey(property) {
|
|
71
|
+
if ([types_1.PropertyType.ObjectReference, types_1.PropertyType.UserList, types_1.PropertyType.TypeReference].includes(property.propertyType)) {
|
|
72
|
+
return property.slug + 'Id';
|
|
73
|
+
}
|
|
74
|
+
return property.slug;
|
|
75
|
+
}
|
|
@@ -2,7 +2,7 @@ import { cloneDeep } from './cloneDeep/cloneDeep';
|
|
|
2
2
|
import { isObject } from './isObject/isObject';
|
|
3
3
|
import { mergeDeep } from './mergeDeep/mergeDeep';
|
|
4
4
|
import { getObjectDiffs } from './compareDeep/compareDeep';
|
|
5
|
-
import { applyChangesIfEqual, determineChangesIfEqual } from './applyChangesIfEqual/applyChangesIfEqual';
|
|
5
|
+
import { applyChangesIfEqual, determineChangesIfEqual, areItemPropertyValuesEqual } from './applyChangesIfEqual/applyChangesIfEqual';
|
|
6
6
|
export { ObjectDiff } from './compareDeep/compareDeep';
|
|
7
7
|
export declare class ObjectUtil {
|
|
8
8
|
static getByPath(obj: any, path: string, def?: any): any;
|
|
@@ -13,4 +13,5 @@ export declare class ObjectUtil {
|
|
|
13
13
|
static compareDeep: typeof getObjectDiffs;
|
|
14
14
|
static applyChangesIfEqual: typeof applyChangesIfEqual;
|
|
15
15
|
static determineChangesIfEqual: typeof determineChangesIfEqual;
|
|
16
|
+
static areItemPropertyValuesEqual: typeof areItemPropertyValuesEqual;
|
|
16
17
|
}
|
|
@@ -41,3 +41,4 @@ ObjectUtil.cloneDeep = cloneDeep_1.cloneDeep;
|
|
|
41
41
|
ObjectUtil.compareDeep = compareDeep_1.getObjectDiffs;
|
|
42
42
|
ObjectUtil.applyChangesIfEqual = applyChangesIfEqual_1.applyChangesIfEqual;
|
|
43
43
|
ObjectUtil.determineChangesIfEqual = applyChangesIfEqual_1.determineChangesIfEqual;
|
|
44
|
+
ObjectUtil.areItemPropertyValuesEqual = applyChangesIfEqual_1.areItemPropertyValuesEqual;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/util",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.43",
|
|
4
4
|
"description": "General javascript utilities",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -35,5 +35,8 @@
|
|
|
35
35
|
},
|
|
36
36
|
"coverageDirectory": "../coverage",
|
|
37
37
|
"testEnvironment": "node"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@contrail/types": "^3.0.45"
|
|
38
41
|
}
|
|
39
42
|
}
|