@contrail/util 1.0.12 → 1.0.16

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.
@@ -4,3 +4,4 @@ export interface ObjectDiff {
4
4
  newValue: any;
5
5
  }
6
6
  export declare function getObjectDiffs(before: any, after: any, prefix: string): ObjectDiff[];
7
+ export declare function getObjectDiff(before: any, after: any, name: any): ObjectDiff;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getObjectDiffs = void 0;
3
+ exports.getObjectDiff = exports.getObjectDiffs = void 0;
4
4
  function getObjectDiffs(before, after, prefix) {
5
5
  const diffs = [];
6
6
  if (!before || !after) {
@@ -18,30 +18,40 @@ function getObjectDiffs(before, after, prefix) {
18
18
  diffs.push(getObjectDiff(before[deletedProperty], null, prefix + deletedProperty));
19
19
  });
20
20
  commonProperties.forEach(commonProperty => {
21
- if (typeof before[commonProperty] !== typeof after[commonProperty]) {
22
- diffs.push(getObjectDiff(before[commonProperty], after[commonProperty], prefix + commonProperty));
21
+ const beforeVal = before[commonProperty];
22
+ const afterVal = after[commonProperty];
23
+ if (beforeVal === afterVal) {
24
+ return;
23
25
  }
24
- else if (isObject(before[commonProperty])) {
25
- if (isDate(before[commonProperty])) {
26
- if (!datesEqual(before[commonProperty], after[commonProperty])) {
27
- diffs.push(getObjectDiff(before[commonProperty], after[commonProperty], prefix + commonProperty));
26
+ if (beforeVal === null || afterVal === null) {
27
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
28
+ return;
29
+ }
30
+ if (typeof beforeVal !== typeof afterVal) {
31
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
32
+ return;
33
+ }
34
+ if (isObject(beforeVal)) {
35
+ if (isDate(beforeVal)) {
36
+ if (!datesEqual(beforeVal, afterVal)) {
37
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
28
38
  }
29
39
  }
30
- else if (Array.isArray(before[commonProperty])) {
31
- const beforeArray = [...before[commonProperty]].sort(sortFunc);
32
- const afterArray = [...after[commonProperty]].sort(sortFunc);
40
+ else if (Array.isArray(beforeVal)) {
41
+ const beforeArray = [...beforeVal].sort(sortFunc);
42
+ const afterArray = [...afterVal].sort(sortFunc);
33
43
  if (JSON.stringify(beforeArray) !== JSON.stringify(afterArray)) {
34
- diffs.push(getObjectDiff(before[commonProperty], after[commonProperty], prefix + commonProperty));
44
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
35
45
  }
36
46
  }
37
47
  else {
38
- const nestedObjectDiffs = getObjectDiffs(before[commonProperty], after[commonProperty], prefix + commonProperty + '.');
48
+ const nestedObjectDiffs = getObjectDiffs(beforeVal, afterVal, prefix + commonProperty + '.');
39
49
  diffs.push(...nestedObjectDiffs);
40
50
  }
41
51
  }
42
52
  else {
43
- if (before[commonProperty] !== after[commonProperty]) {
44
- diffs.push(getObjectDiff(before[commonProperty], after[commonProperty], prefix + commonProperty));
53
+ if (beforeVal !== afterVal) {
54
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
45
55
  }
46
56
  }
47
57
  });
@@ -56,6 +66,7 @@ function getObjectDiff(before, after, name) {
56
66
  };
57
67
  return diff;
58
68
  }
69
+ exports.getObjectDiff = getObjectDiff;
59
70
  function sortFunc(x, y) {
60
71
  const pre = ['string', 'number', 'bool'];
61
72
  if (typeof x !== typeof y)
@@ -14,6 +14,14 @@ export declare const item1Assortment2: {
14
14
  dateProperty: Date;
15
15
  arrayProperty: string[];
16
16
  };
17
+ export declare const item1Assortment3: {
18
+ id: string;
19
+ stringProperty: string;
20
+ intProperty: number;
21
+ boolProperty: boolean;
22
+ dateProperty: Date;
23
+ arrayProperty: any;
24
+ };
17
25
  export declare const item2Assortment1: {
18
26
  id: string;
19
27
  stringProperty: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.item3Assortment2NestedChanges = exports.item3Assortment1 = exports.item2Assortment2DateChange = exports.item2Assortment2ArrayChange = exports.item2Assortment2ScalarChange = exports.item2Assortment1 = exports.item1Assortment2 = exports.item1Assortment1 = void 0;
3
+ exports.item3Assortment2NestedChanges = exports.item3Assortment1 = exports.item2Assortment2DateChange = exports.item2Assortment2ArrayChange = exports.item2Assortment2ScalarChange = exports.item2Assortment1 = exports.item1Assortment3 = exports.item1Assortment2 = exports.item1Assortment1 = void 0;
4
4
  exports.item1Assortment1 = {
5
5
  id: 'id1',
6
6
  stringProperty: 'stringProperty1',
@@ -17,6 +17,14 @@ exports.item1Assortment2 = {
17
17
  dateProperty: new Date('12/22/1994'),
18
18
  arrayProperty: ['A', 'B', 'C'],
19
19
  };
20
+ exports.item1Assortment3 = {
21
+ id: 'id1',
22
+ stringProperty: 'stringProperty1',
23
+ intProperty: 1,
24
+ boolProperty: false,
25
+ dateProperty: new Date('12/22/1994'),
26
+ arrayProperty: null,
27
+ };
20
28
  exports.item2Assortment1 = {
21
29
  id: 'id2',
22
30
  stringProperty: 'stringProperty2',
@@ -1,3 +1,6 @@
1
1
  export declare class StringUtil {
2
2
  static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
3
+ static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
4
+ static isString(value: any): boolean;
5
+ static isArray(value: any): boolean;
3
6
  }
@@ -9,5 +9,43 @@ class StringUtil {
9
9
  }
10
10
  return slugName;
11
11
  }
12
+ static parseVariables(variableString, data, quoteStrings = false) {
13
+ const regExp = /\{([^}]+)\}/;
14
+ let done = false;
15
+ let currentVariableString = variableString;
16
+ while (!done) {
17
+ const match = regExp.exec(currentVariableString);
18
+ if (!match) {
19
+ done = true;
20
+ break;
21
+ }
22
+ const variableLevels = match[1].split('.');
23
+ let replacementValue = data;
24
+ for (let i = 0; i < variableLevels.length; i++) {
25
+ replacementValue = replacementValue[variableLevels[i]];
26
+ }
27
+ if (quoteStrings && StringUtil.isString(replacementValue)) {
28
+ replacementValue = JSON.stringify(replacementValue);
29
+ }
30
+ if (StringUtil.isArray(replacementValue)) {
31
+ if (quoteStrings) {
32
+ const newArray = [];
33
+ for (const val of replacementValue) {
34
+ newArray.push(JSON.stringify(val));
35
+ }
36
+ replacementValue = newArray;
37
+ }
38
+ replacementValue = '[' + replacementValue + ']';
39
+ }
40
+ currentVariableString = currentVariableString.replace(match[0], replacementValue);
41
+ }
42
+ return currentVariableString;
43
+ }
44
+ static isString(value) {
45
+ return typeof value === 'string' || value instanceof String;
46
+ }
47
+ static isArray(value) {
48
+ return Array.isArray(value);
49
+ }
12
50
  }
13
51
  exports.StringUtil = StringUtil;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.0.12",
3
+ "version": "1.0.16",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",