@contrail/util 1.0.42 → 1.0.44

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
- export declare function applyChangesIfEqual(target: any, priorSourceValues: any, newSourceValues: any, propertyKeys: any): void;
2
- export declare function determineChangesIfEqual(target: any, priorSourceValues: any, newSourceValues: any, propertyKeys: any): any;
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,77 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.determineChangesIfEqual = exports.applyChangesIfEqual = void 0;
4
- function applyChangesIfEqual(target, priorSourceValues, newSourceValues, propertyKeys) {
5
- const changes = determineChangesIfEqual(target, priorSourceValues, newSourceValues, propertyKeys);
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, propertyKeys) {
13
+ function determineChangesIfEqual(target, priorSourceValues, newSourceValues, properties) {
12
14
  const changes = {};
13
- for (const propKey of propertyKeys) {
14
- if (newSourceValues[propKey] === undefined) {
15
+ for (const property of properties) {
16
+ const propertyKey = getPropertyKey(property);
17
+ if (newSourceValues[propertyKey] === undefined) {
15
18
  continue;
16
19
  }
17
- if (!target.hasOwnProperty(propKey) || (priorSourceValues[propKey] === target[propKey])) {
18
- changes[propKey] = newSourceValues[propKey];
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
+ const propertyValueOneAsArray = typeof propertyValueOne === 'string' ? propertyValueOne.split(',') : propertyValueOne;
37
+ const propertyValueTwoAsArray = typeof propertyValueTwo === 'string' ? propertyValueTwo.split(',') : propertyValueTwo;
38
+ return areArraysEqualIgnoreOrder(propertyValueOneAsArray, propertyValueTwoAsArray);
39
+ }
40
+ if (property.propertyType === types_1.PropertyType.SizeRange) {
41
+ 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);
42
+ if (!isOfSameSizeRangeTemplate)
43
+ return false;
44
+ return areSizeRangesEqual(propertyValueOne, propertyValueTwo);
45
+ }
46
+ return propertyValueOne === propertyValueTwo;
47
+ }
48
+ exports.areItemPropertyValuesEqual = areItemPropertyValuesEqual;
49
+ function areSizeRangesEqual(sizeRangeOne, sizeRangeTwo) {
50
+ var _a, _b;
51
+ if (!sizeRangeOne && !sizeRangeTwo) {
52
+ return true;
53
+ }
54
+ 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)) {
55
+ return false;
56
+ }
57
+ const result = (0, compareDeep_1.getObjectDiffs)(sizeRangeOne, sizeRangeTwo, '');
58
+ return result.length === 0;
59
+ }
60
+ function areArraysEqualIgnoreOrder(arrayOne, arrayTwo) {
61
+ if (!arrayOne && !arrayTwo) {
62
+ return true;
63
+ }
64
+ if ((arrayOne === null || arrayOne === void 0 ? void 0 : arrayOne.length) !== (arrayTwo === null || arrayTwo === void 0 ? void 0 : arrayTwo.length)) {
65
+ return false;
66
+ }
67
+ const sortedArray1 = arrayOne.slice().sort();
68
+ const sortedArray2 = arrayTwo.slice().sort();
69
+ const areArraysEqual = sortedArray1.every((element, index) => element === sortedArray2[index]);
70
+ return areArraysEqual;
71
+ }
72
+ function getPropertyKey(property) {
73
+ if ([types_1.PropertyType.ObjectReference, types_1.PropertyType.UserList, types_1.PropertyType.TypeReference].includes(property.propertyType)) {
74
+ return property.slug + 'Id';
75
+ }
76
+ return property.slug;
77
+ }
@@ -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.42",
3
+ "version": "1.0.44",
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
  }