@contrail/util 1.0.18 → 1.0.21

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/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from './object-util/object-util';
2
- export * from './string-util/string-util';
1
+ export * from './object-util/object-util';
2
+ export * from './string-util/string-util';
package/lib/index.js CHANGED
@@ -1,14 +1,18 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./object-util/object-util"), exports);
14
- __exportStar(require("./string-util/string-util"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./object-util/object-util"), exports);
18
+ __exportStar(require("./string-util/string-util"), exports);
@@ -0,0 +1,2 @@
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;
@@ -0,0 +1,23 @@
1
+ "use strict";
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);
6
+ if (changes) {
7
+ Object.assign(target, changes);
8
+ }
9
+ }
10
+ exports.applyChangesIfEqual = applyChangesIfEqual;
11
+ function determineChangesIfEqual(target, priorSourceValues, newSourceValues, propertyKeys) {
12
+ const changes = {};
13
+ for (const propKey of propertyKeys) {
14
+ if (newSourceValues[propKey] === undefined) {
15
+ continue;
16
+ }
17
+ if (!target[propKey] || (priorSourceValues[propKey] === target[propKey])) {
18
+ changes[propKey] = newSourceValues[propKey];
19
+ }
20
+ }
21
+ return changes;
22
+ }
23
+ exports.determineChangesIfEqual = determineChangesIfEqual;
@@ -1 +1 @@
1
- export declare function cloneDeep(obj: any): any;
1
+ export declare function cloneDeep(obj: any): any;
@@ -1,10 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cloneDeep = void 0;
4
- function cloneDeep(obj) {
5
- if (!obj) {
6
- return null;
7
- }
8
- return JSON.parse(JSON.stringify(obj));
9
- }
10
- exports.cloneDeep = cloneDeep;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cloneDeep = void 0;
4
+ function cloneDeep(obj) {
5
+ if (!obj) {
6
+ return null;
7
+ }
8
+ return JSON.parse(JSON.stringify(obj));
9
+ }
10
+ exports.cloneDeep = cloneDeep;
@@ -1,7 +1,7 @@
1
- export interface ObjectDiff {
2
- propertyName: string;
3
- oldValue: any;
4
- newValue: any;
5
- }
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
+ export interface ObjectDiff {
2
+ propertyName: string;
3
+ oldValue: any;
4
+ newValue: any;
5
+ }
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,87 +1,87 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getObjectDiff = exports.getObjectDiffs = void 0;
4
- function getObjectDiffs(before, after, prefix) {
5
- const diffs = [];
6
- if (!before || !after) {
7
- return diffs;
8
- }
9
- const beforeProperties = Object.getOwnPropertyNames(before);
10
- const afterProperties = Object.getOwnPropertyNames(after);
11
- const addedProperties = afterProperties.filter(value => !beforeProperties.includes(value));
12
- const deletedProperties = beforeProperties.filter(value => !afterProperties.includes(value));
13
- const commonProperties = beforeProperties.filter(value => afterProperties.includes(value));
14
- addedProperties.forEach(addedProperty => {
15
- diffs.push(getObjectDiff(null, after[addedProperty], prefix + addedProperty));
16
- });
17
- deletedProperties.forEach(deletedProperty => {
18
- diffs.push(getObjectDiff(before[deletedProperty], null, prefix + deletedProperty));
19
- });
20
- commonProperties.forEach(commonProperty => {
21
- const beforeVal = before[commonProperty];
22
- const afterVal = after[commonProperty];
23
- if (beforeVal === afterVal) {
24
- return;
25
- }
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));
38
- }
39
- }
40
- else if (Array.isArray(beforeVal)) {
41
- const beforeArray = [...beforeVal].sort(sortFunc);
42
- const afterArray = [...afterVal].sort(sortFunc);
43
- if (JSON.stringify(beforeArray) !== JSON.stringify(afterArray)) {
44
- diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
45
- }
46
- }
47
- else {
48
- const nestedObjectDiffs = getObjectDiffs(beforeVal, afterVal, prefix + commonProperty + '.');
49
- diffs.push(...nestedObjectDiffs);
50
- }
51
- }
52
- else {
53
- if (beforeVal !== afterVal) {
54
- diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
55
- }
56
- }
57
- });
58
- return diffs;
59
- }
60
- exports.getObjectDiffs = getObjectDiffs;
61
- function getObjectDiff(before, after, name) {
62
- const diff = {
63
- propertyName: name,
64
- oldValue: before,
65
- newValue: after,
66
- };
67
- return diff;
68
- }
69
- exports.getObjectDiff = getObjectDiff;
70
- function sortFunc(x, y) {
71
- const pre = ['string', 'number', 'bool'];
72
- if (typeof x !== typeof y)
73
- return pre.indexOf(typeof y) - pre.indexOf(typeof x);
74
- if (x === y)
75
- return 0;
76
- else
77
- return (x > y) ? 1 : -1;
78
- }
79
- function isObject(object) {
80
- return object != null && typeof object === 'object';
81
- }
82
- function isDate(object) {
83
- return object != null && object instanceof Date;
84
- }
85
- function datesEqual(date1, date2) {
86
- return date1.getTime() === date2.getTime();
87
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getObjectDiff = exports.getObjectDiffs = void 0;
4
+ function getObjectDiffs(before, after, prefix) {
5
+ const diffs = [];
6
+ if (!before || !after) {
7
+ return diffs;
8
+ }
9
+ const beforeProperties = Object.getOwnPropertyNames(before);
10
+ const afterProperties = Object.getOwnPropertyNames(after);
11
+ const addedProperties = afterProperties.filter(value => !beforeProperties.includes(value));
12
+ const deletedProperties = beforeProperties.filter(value => !afterProperties.includes(value));
13
+ const commonProperties = beforeProperties.filter(value => afterProperties.includes(value));
14
+ addedProperties.forEach(addedProperty => {
15
+ diffs.push(getObjectDiff(null, after[addedProperty], prefix + addedProperty));
16
+ });
17
+ deletedProperties.forEach(deletedProperty => {
18
+ diffs.push(getObjectDiff(before[deletedProperty], null, prefix + deletedProperty));
19
+ });
20
+ commonProperties.forEach(commonProperty => {
21
+ const beforeVal = before[commonProperty];
22
+ const afterVal = after[commonProperty];
23
+ if (beforeVal === afterVal) {
24
+ return;
25
+ }
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));
38
+ }
39
+ }
40
+ else if (Array.isArray(beforeVal)) {
41
+ const beforeArray = [...beforeVal].sort(sortFunc);
42
+ const afterArray = [...afterVal].sort(sortFunc);
43
+ if (JSON.stringify(beforeArray) !== JSON.stringify(afterArray)) {
44
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
45
+ }
46
+ }
47
+ else {
48
+ const nestedObjectDiffs = getObjectDiffs(beforeVal, afterVal, prefix + commonProperty + '.');
49
+ diffs.push(...nestedObjectDiffs);
50
+ }
51
+ }
52
+ else {
53
+ if (beforeVal !== afterVal) {
54
+ diffs.push(getObjectDiff(beforeVal, afterVal, prefix + commonProperty));
55
+ }
56
+ }
57
+ });
58
+ return diffs;
59
+ }
60
+ exports.getObjectDiffs = getObjectDiffs;
61
+ function getObjectDiff(before, after, name) {
62
+ const diff = {
63
+ propertyName: name,
64
+ oldValue: before,
65
+ newValue: after,
66
+ };
67
+ return diff;
68
+ }
69
+ exports.getObjectDiff = getObjectDiff;
70
+ function sortFunc(x, y) {
71
+ const pre = ['string', 'number', 'bool'];
72
+ if (typeof x !== typeof y)
73
+ return pre.indexOf(typeof y) - pre.indexOf(typeof x);
74
+ if (x === y)
75
+ return 0;
76
+ else
77
+ return (x > y) ? 1 : -1;
78
+ }
79
+ function isObject(object) {
80
+ return object != null && typeof object === 'object';
81
+ }
82
+ function isDate(object) {
83
+ return object != null && object instanceof Date;
84
+ }
85
+ function datesEqual(date1, date2) {
86
+ return date1.getTime() === date2.getTime();
87
+ }
@@ -1,88 +1,88 @@
1
- export declare const item1Assortment1: {
2
- id: string;
3
- stringProperty: string;
4
- intProperty: number;
5
- boolProperty: boolean;
6
- dateProperty: Date;
7
- arrayProperty: string[];
8
- };
9
- export declare const item1Assortment2: {
10
- id: string;
11
- stringProperty: string;
12
- intProperty: number;
13
- boolProperty: boolean;
14
- dateProperty: Date;
15
- arrayProperty: string[];
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
- };
25
- export declare const item2Assortment1: {
26
- id: string;
27
- stringProperty: string;
28
- intProperty: number;
29
- boolProperty: boolean;
30
- dateProperty: Date;
31
- arrayProperty: string[];
32
- };
33
- export declare const item2Assortment2ScalarChange: {
34
- id: string;
35
- stringProperty: string;
36
- intProperty: number;
37
- boolProperty: boolean;
38
- dateProperty: Date;
39
- arrayProperty: string[];
40
- };
41
- export declare const item2Assortment2ArrayChange: {
42
- id: string;
43
- stringProperty: string;
44
- intProperty: number;
45
- boolProperty: boolean;
46
- dateProperty: Date;
47
- arrayProperty: string[];
48
- };
49
- export declare const item2Assortment2DateChange: {
50
- id: string;
51
- stringProperty: string;
52
- intProperty: number;
53
- boolProperty: boolean;
54
- dateProperty: Date;
55
- arrayProperty: string[];
56
- };
57
- export declare const item3Assortment1: {
58
- id: string;
59
- stringProperty: string;
60
- intProperty: number;
61
- boolProperty: boolean;
62
- dateProperty: Date;
63
- arrayProperty: string[];
64
- nestedProperties: {
65
- nestedBoolProperty: boolean;
66
- nestedDateProperty: Date;
67
- nestedArrayProperty: string[];
68
- nestedObject: {
69
- nestedBoolProperty: boolean;
70
- };
71
- };
72
- };
73
- export declare const item3Assortment2NestedChanges: {
74
- id: string;
75
- stringProperty: string;
76
- intProperty: number;
77
- boolProperty: boolean;
78
- dateProperty: Date;
79
- arrayProperty: string[];
80
- nestedProperties: {
81
- nestedBoolProperty: boolean;
82
- nestedDateProperty: Date;
83
- nestedArrayProperty: string[];
84
- nestedObject: {
85
- nestedBoolProperty: boolean;
86
- };
87
- };
88
- };
1
+ export declare const item1Assortment1: {
2
+ id: string;
3
+ stringProperty: string;
4
+ intProperty: number;
5
+ boolProperty: boolean;
6
+ dateProperty: Date;
7
+ arrayProperty: string[];
8
+ };
9
+ export declare const item1Assortment2: {
10
+ id: string;
11
+ stringProperty: string;
12
+ intProperty: number;
13
+ boolProperty: boolean;
14
+ dateProperty: Date;
15
+ arrayProperty: string[];
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
+ };
25
+ export declare const item2Assortment1: {
26
+ id: string;
27
+ stringProperty: string;
28
+ intProperty: number;
29
+ boolProperty: boolean;
30
+ dateProperty: Date;
31
+ arrayProperty: string[];
32
+ };
33
+ export declare const item2Assortment2ScalarChange: {
34
+ id: string;
35
+ stringProperty: string;
36
+ intProperty: number;
37
+ boolProperty: boolean;
38
+ dateProperty: Date;
39
+ arrayProperty: string[];
40
+ };
41
+ export declare const item2Assortment2ArrayChange: {
42
+ id: string;
43
+ stringProperty: string;
44
+ intProperty: number;
45
+ boolProperty: boolean;
46
+ dateProperty: Date;
47
+ arrayProperty: string[];
48
+ };
49
+ export declare const item2Assortment2DateChange: {
50
+ id: string;
51
+ stringProperty: string;
52
+ intProperty: number;
53
+ boolProperty: boolean;
54
+ dateProperty: Date;
55
+ arrayProperty: string[];
56
+ };
57
+ export declare const item3Assortment1: {
58
+ id: string;
59
+ stringProperty: string;
60
+ intProperty: number;
61
+ boolProperty: boolean;
62
+ dateProperty: Date;
63
+ arrayProperty: string[];
64
+ nestedProperties: {
65
+ nestedBoolProperty: boolean;
66
+ nestedDateProperty: Date;
67
+ nestedArrayProperty: string[];
68
+ nestedObject: {
69
+ nestedBoolProperty: boolean;
70
+ };
71
+ };
72
+ };
73
+ export declare const item3Assortment2NestedChanges: {
74
+ id: string;
75
+ stringProperty: string;
76
+ intProperty: number;
77
+ boolProperty: boolean;
78
+ dateProperty: Date;
79
+ arrayProperty: string[];
80
+ nestedProperties: {
81
+ nestedBoolProperty: boolean;
82
+ nestedDateProperty: Date;
83
+ nestedArrayProperty: string[];
84
+ nestedObject: {
85
+ nestedBoolProperty: boolean;
86
+ };
87
+ };
88
+ };
@@ -1,91 +1,91 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.item3Assortment2NestedChanges = exports.item3Assortment1 = exports.item2Assortment2DateChange = exports.item2Assortment2ArrayChange = exports.item2Assortment2ScalarChange = exports.item2Assortment1 = exports.item1Assortment3 = exports.item1Assortment2 = exports.item1Assortment1 = void 0;
4
- exports.item1Assortment1 = {
5
- id: 'id1',
6
- stringProperty: 'stringProperty1',
7
- intProperty: 1,
8
- boolProperty: false,
9
- dateProperty: new Date('12/22/1994'),
10
- arrayProperty: ['A', 'B', 'C'],
11
- };
12
- exports.item1Assortment2 = {
13
- id: 'id1',
14
- stringProperty: 'stringProperty1',
15
- intProperty: 1,
16
- boolProperty: false,
17
- dateProperty: new Date('12/22/1994'),
18
- arrayProperty: ['A', 'B', 'C'],
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
- };
28
- exports.item2Assortment1 = {
29
- id: 'id2',
30
- stringProperty: 'stringProperty2',
31
- intProperty: 2,
32
- boolProperty: false,
33
- dateProperty: new Date('12/23/1994'),
34
- arrayProperty: ['A', 'B'],
35
- };
36
- exports.item2Assortment2ScalarChange = {
37
- id: 'id2',
38
- stringProperty: 'stringProperty3Update',
39
- intProperty: 4,
40
- boolProperty: true,
41
- dateProperty: new Date('12/23/1994'),
42
- arrayProperty: ['A', 'B'],
43
- };
44
- exports.item2Assortment2ArrayChange = {
45
- id: 'id2',
46
- stringProperty: 'stringProperty2',
47
- intProperty: 2,
48
- boolProperty: false,
49
- dateProperty: new Date('12/23/1994'),
50
- arrayProperty: ['A', 'B', 'D'],
51
- };
52
- exports.item2Assortment2DateChange = {
53
- id: 'id2',
54
- stringProperty: 'stringProperty2',
55
- intProperty: 2,
56
- boolProperty: false,
57
- dateProperty: new Date('12/23/1995'),
58
- arrayProperty: ['A', 'B'],
59
- };
60
- exports.item3Assortment1 = {
61
- id: 'id3',
62
- stringProperty: 'stringProperty2',
63
- intProperty: 2,
64
- boolProperty: false,
65
- dateProperty: new Date('12/23/1994'),
66
- arrayProperty: ['A', 'B'],
67
- nestedProperties: {
68
- nestedBoolProperty: false,
69
- nestedDateProperty: new Date('12/22/1994'),
70
- nestedArrayProperty: ['C', 'D'],
71
- nestedObject: {
72
- nestedBoolProperty: false,
73
- },
74
- },
75
- };
76
- exports.item3Assortment2NestedChanges = {
77
- id: 'id3',
78
- stringProperty: 'stringProperty2',
79
- intProperty: 2,
80
- boolProperty: true,
81
- dateProperty: new Date('12/23/1994'),
82
- arrayProperty: ['A', 'B'],
83
- nestedProperties: {
84
- nestedBoolProperty: false,
85
- nestedDateProperty: new Date('12/23/1995'),
86
- nestedArrayProperty: ['C', 'D', 'A'],
87
- nestedObject: {
88
- nestedBoolProperty: true,
89
- },
90
- },
91
- };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.item3Assortment2NestedChanges = exports.item3Assortment1 = exports.item2Assortment2DateChange = exports.item2Assortment2ArrayChange = exports.item2Assortment2ScalarChange = exports.item2Assortment1 = exports.item1Assortment3 = exports.item1Assortment2 = exports.item1Assortment1 = void 0;
4
+ exports.item1Assortment1 = {
5
+ id: 'id1',
6
+ stringProperty: 'stringProperty1',
7
+ intProperty: 1,
8
+ boolProperty: false,
9
+ dateProperty: new Date('12/22/1994'),
10
+ arrayProperty: ['A', 'B', 'C'],
11
+ };
12
+ exports.item1Assortment2 = {
13
+ id: 'id1',
14
+ stringProperty: 'stringProperty1',
15
+ intProperty: 1,
16
+ boolProperty: false,
17
+ dateProperty: new Date('12/22/1994'),
18
+ arrayProperty: ['A', 'B', 'C'],
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
+ };
28
+ exports.item2Assortment1 = {
29
+ id: 'id2',
30
+ stringProperty: 'stringProperty2',
31
+ intProperty: 2,
32
+ boolProperty: false,
33
+ dateProperty: new Date('12/23/1994'),
34
+ arrayProperty: ['A', 'B'],
35
+ };
36
+ exports.item2Assortment2ScalarChange = {
37
+ id: 'id2',
38
+ stringProperty: 'stringProperty3Update',
39
+ intProperty: 4,
40
+ boolProperty: true,
41
+ dateProperty: new Date('12/23/1994'),
42
+ arrayProperty: ['A', 'B'],
43
+ };
44
+ exports.item2Assortment2ArrayChange = {
45
+ id: 'id2',
46
+ stringProperty: 'stringProperty2',
47
+ intProperty: 2,
48
+ boolProperty: false,
49
+ dateProperty: new Date('12/23/1994'),
50
+ arrayProperty: ['A', 'B', 'D'],
51
+ };
52
+ exports.item2Assortment2DateChange = {
53
+ id: 'id2',
54
+ stringProperty: 'stringProperty2',
55
+ intProperty: 2,
56
+ boolProperty: false,
57
+ dateProperty: new Date('12/23/1995'),
58
+ arrayProperty: ['A', 'B'],
59
+ };
60
+ exports.item3Assortment1 = {
61
+ id: 'id3',
62
+ stringProperty: 'stringProperty2',
63
+ intProperty: 2,
64
+ boolProperty: false,
65
+ dateProperty: new Date('12/23/1994'),
66
+ arrayProperty: ['A', 'B'],
67
+ nestedProperties: {
68
+ nestedBoolProperty: false,
69
+ nestedDateProperty: new Date('12/22/1994'),
70
+ nestedArrayProperty: ['C', 'D'],
71
+ nestedObject: {
72
+ nestedBoolProperty: false,
73
+ },
74
+ },
75
+ };
76
+ exports.item3Assortment2NestedChanges = {
77
+ id: 'id3',
78
+ stringProperty: 'stringProperty2',
79
+ intProperty: 2,
80
+ boolProperty: true,
81
+ dateProperty: new Date('12/23/1994'),
82
+ arrayProperty: ['A', 'B'],
83
+ nestedProperties: {
84
+ nestedBoolProperty: false,
85
+ nestedDateProperty: new Date('12/23/1995'),
86
+ nestedArrayProperty: ['C', 'D', 'A'],
87
+ nestedObject: {
88
+ nestedBoolProperty: true,
89
+ },
90
+ },
91
+ };
@@ -1 +1 @@
1
- export declare function isObject(item: any): boolean;
1
+ export declare function isObject(item: any): boolean;
@@ -1,7 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isObject = void 0;
4
- function isObject(item) {
5
- return (item && typeof item === 'object' && !Array.isArray(item));
6
- }
7
- exports.isObject = isObject;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isObject = void 0;
4
+ function isObject(item) {
5
+ return (item && typeof item === 'object' && !Array.isArray(item));
6
+ }
7
+ exports.isObject = isObject;
@@ -1 +1 @@
1
- export declare function mergeDeep(target: any, ...sources: any[]): any;
1
+ export declare function mergeDeep(target: any, ...sources: any[]): any;
@@ -1,26 +1,26 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeDeep = void 0;
4
- const isObject_1 = require("../isObject/isObject");
5
- function mergeDeep(target, ...sources) {
6
- if (!sources.length)
7
- return target;
8
- const source = sources.shift();
9
- if (isObject_1.isObject(target) && isObject_1.isObject(source)) {
10
- for (const key in source) {
11
- if (isObject_1.isObject(source[key]) && !isDate(source[key])) {
12
- if (!target[key])
13
- Object.assign(target, { [key]: {} });
14
- mergeDeep(target[key], source[key]);
15
- }
16
- else {
17
- Object.assign(target, { [key]: source[key] });
18
- }
19
- }
20
- }
21
- return mergeDeep(target, ...sources);
22
- }
23
- exports.mergeDeep = mergeDeep;
24
- function isDate(object) {
25
- return object != null && object instanceof Date;
26
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeDeep = void 0;
4
+ const isObject_1 = require("../isObject/isObject");
5
+ function mergeDeep(target, ...sources) {
6
+ if (!sources.length)
7
+ return target;
8
+ const source = sources.shift();
9
+ if ((0, isObject_1.isObject)(target) && (0, isObject_1.isObject)(source)) {
10
+ for (const key in source) {
11
+ if ((0, isObject_1.isObject)(source[key]) && !isDate(source[key])) {
12
+ if (!target[key])
13
+ Object.assign(target, { [key]: {} });
14
+ mergeDeep(target[key], source[key]);
15
+ }
16
+ else {
17
+ Object.assign(target, { [key]: source[key] });
18
+ }
19
+ }
20
+ }
21
+ return mergeDeep(target, ...sources);
22
+ }
23
+ exports.mergeDeep = mergeDeep;
24
+ function isDate(object) {
25
+ return object != null && object instanceof Date;
26
+ }
@@ -1,13 +1,15 @@
1
- import { cloneDeep } from './cloneDeep/cloneDeep';
2
- import { isObject } from './isObject/isObject';
3
- import { mergeDeep } from './mergeDeep/mergeDeep';
4
- import { getObjectDiffs } from './compareDeep/compareDeep';
5
- export { ObjectDiff } from './compareDeep/compareDeep';
6
- export declare class ObjectUtil {
7
- static getByPath(obj: any, path: string, def?: any): any;
8
- static setByPath(obj: any, path: string, value: any): void;
9
- static isObject: typeof isObject;
10
- static mergeDeep: typeof mergeDeep;
11
- static cloneDeep: typeof cloneDeep;
12
- static compareDeep: typeof getObjectDiffs;
13
- }
1
+ import { cloneDeep } from './cloneDeep/cloneDeep';
2
+ import { isObject } from './isObject/isObject';
3
+ import { mergeDeep } from './mergeDeep/mergeDeep';
4
+ import { getObjectDiffs } from './compareDeep/compareDeep';
5
+ import { applyChangesIfEqual } from './applyChangesIfEqual/applyChangesIfEqual';
6
+ export { ObjectDiff } from './compareDeep/compareDeep';
7
+ export declare class ObjectUtil {
8
+ static getByPath(obj: any, path: string, def?: any): any;
9
+ static setByPath(obj: any, path: string, value: any): void;
10
+ static isObject: typeof isObject;
11
+ static mergeDeep: typeof mergeDeep;
12
+ static cloneDeep: typeof cloneDeep;
13
+ static compareDeep: typeof getObjectDiffs;
14
+ static applyChangesIfEqual: typeof applyChangesIfEqual;
15
+ }
@@ -1,40 +1,42 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ObjectUtil = void 0;
4
- const cloneDeep_1 = require("./cloneDeep/cloneDeep");
5
- const isObject_1 = require("./isObject/isObject");
6
- const mergeDeep_1 = require("./mergeDeep/mergeDeep");
7
- const compareDeep_1 = require("./compareDeep/compareDeep");
8
- class ObjectUtil {
9
- static getByPath(obj, path, def = null) {
10
- const pathUnits = path
11
- .replace(/\[/g, '.')
12
- .replace(/]/g, '')
13
- .split('.');
14
- pathUnits.forEach((level) => {
15
- if (obj) {
16
- obj = obj[level];
17
- }
18
- });
19
- if (obj === undefined) {
20
- return def;
21
- }
22
- return obj;
23
- }
24
- static setByPath(obj, path, value) {
25
- const a = path.split('.');
26
- let o = obj;
27
- while (a.length - 1) {
28
- const n = a.shift();
29
- if (!(n in o))
30
- o[n] = {};
31
- o = o[n];
32
- }
33
- o[a[0]] = value;
34
- }
35
- }
36
- exports.ObjectUtil = ObjectUtil;
37
- ObjectUtil.isObject = isObject_1.isObject;
38
- ObjectUtil.mergeDeep = mergeDeep_1.mergeDeep;
39
- ObjectUtil.cloneDeep = cloneDeep_1.cloneDeep;
40
- ObjectUtil.compareDeep = compareDeep_1.getObjectDiffs;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ObjectUtil = void 0;
4
+ const cloneDeep_1 = require("./cloneDeep/cloneDeep");
5
+ const isObject_1 = require("./isObject/isObject");
6
+ const mergeDeep_1 = require("./mergeDeep/mergeDeep");
7
+ const compareDeep_1 = require("./compareDeep/compareDeep");
8
+ const applyChangesIfEqual_1 = require("./applyChangesIfEqual/applyChangesIfEqual");
9
+ class ObjectUtil {
10
+ static getByPath(obj, path, def = null) {
11
+ const pathUnits = path
12
+ .replace(/\[/g, '.')
13
+ .replace(/]/g, '')
14
+ .split('.');
15
+ pathUnits.forEach((level) => {
16
+ if (obj) {
17
+ obj = obj[level];
18
+ }
19
+ });
20
+ if (obj === undefined) {
21
+ return def;
22
+ }
23
+ return obj;
24
+ }
25
+ static setByPath(obj, path, value) {
26
+ const a = path.split('.');
27
+ let o = obj;
28
+ while (a.length - 1) {
29
+ const n = a.shift();
30
+ if (!(n in o))
31
+ o[n] = {};
32
+ o = o[n];
33
+ }
34
+ o[a[0]] = value;
35
+ }
36
+ }
37
+ exports.ObjectUtil = ObjectUtil;
38
+ ObjectUtil.isObject = isObject_1.isObject;
39
+ ObjectUtil.mergeDeep = mergeDeep_1.mergeDeep;
40
+ ObjectUtil.cloneDeep = cloneDeep_1.cloneDeep;
41
+ ObjectUtil.compareDeep = compareDeep_1.getObjectDiffs;
42
+ ObjectUtil.applyChangesIfEqual = applyChangesIfEqual_1.applyChangesIfEqual;
@@ -1,8 +1,8 @@
1
- export declare class StringUtil {
2
- static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
3
- static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
4
- static convertToCamelCase(str: any): any;
5
- static convertToTitleCase(str: any): any;
6
- static isString(value: any): boolean;
7
- static isArray(value: any): boolean;
8
- }
1
+ export declare class StringUtil {
2
+ static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
3
+ static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
4
+ static convertToCamelCase(str: any): any;
5
+ static convertToTitleCase(str: any): any;
6
+ static isString(value: any): boolean;
7
+ static isArray(value: any): boolean;
8
+ }
@@ -1,61 +1,61 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StringUtil = void 0;
4
- class StringUtil {
5
- static convertToHyphenCase(entityName, transformToLowerCase = true) {
6
- const slugName = entityName === null || entityName === void 0 ? void 0 : entityName.split(/(?=[A-Z])/).join('-');
7
- if (slugName && transformToLowerCase) {
8
- return slugName.toLowerCase();
9
- }
10
- return slugName;
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 convertToCamelCase(str) {
45
- return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
46
- return index === 0 ? word.toLowerCase() : word.toUpperCase();
47
- }).replace(/\s+/g, '');
48
- }
49
- static convertToTitleCase(str) {
50
- const camelCase = this.convertToCamelCase(str);
51
- const withSpaces = camelCase.replace(/([A-Z])/g, " $1");
52
- return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
53
- }
54
- static isString(value) {
55
- return typeof value === 'string' || value instanceof String;
56
- }
57
- static isArray(value) {
58
- return Array.isArray(value);
59
- }
60
- }
61
- exports.StringUtil = StringUtil;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringUtil = void 0;
4
+ class StringUtil {
5
+ static convertToHyphenCase(entityName, transformToLowerCase = true) {
6
+ const slugName = entityName === null || entityName === void 0 ? void 0 : entityName.split(/(?=[A-Z])/).join('-');
7
+ if (slugName && transformToLowerCase) {
8
+ return slugName.toLowerCase();
9
+ }
10
+ return slugName;
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 convertToCamelCase(str) {
45
+ return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
46
+ return index === 0 ? word.toLowerCase() : word.toUpperCase();
47
+ }).replace(/\s+/g, '');
48
+ }
49
+ static convertToTitleCase(str) {
50
+ const camelCase = this.convertToCamelCase(str);
51
+ const withSpaces = camelCase.replace(/([A-Z])/g, " $1");
52
+ return withSpaces.charAt(0).toUpperCase() + withSpaces.slice(1);
53
+ }
54
+ static isString(value) {
55
+ return typeof value === 'string' || value instanceof String;
56
+ }
57
+ static isArray(value) {
58
+ return Array.isArray(value);
59
+ }
60
+ }
61
+ exports.StringUtil = StringUtil;
package/package.json CHANGED
@@ -1,41 +1,41 @@
1
- {
2
- "name": "@contrail/util",
3
- "version": "1.0.18",
4
- "description": "General javascript utilities",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "scripts": {
8
- "build": "tsc",
9
- "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
- "lint": "tslint -p tsconfig.json",
11
- "test:watch": "jest --watch",
12
- "test": "jest"
13
- },
14
- "keywords": [],
15
- "author": "",
16
- "license": "ISC",
17
- "devDependencies": {
18
- "@types/jest": "^23.3.14",
19
- "jest": "^23.6.0",
20
- "prettier": "^1.19.1",
21
- "ts-jest": "^23.10.5",
22
- "tslint": "^5.11.0",
23
- "tslint-config-prettier": "^1.18.0",
24
- "typescript": "^3.0.1"
25
- },
26
- "jest": {
27
- "moduleFileExtensions": [
28
- "js",
29
- "json",
30
- "ts"
31
- ],
32
- "rootDir": "src",
33
- "testRegex": ".spec.ts$",
34
- "transform": {
35
- "^.+\\.(t|j)s$": "ts-jest"
36
- },
37
- "coverageDirectory": "../coverage",
38
- "testEnvironment": "node"
39
- },
40
- "dependencies": {}
41
- }
1
+ {
2
+ "name": "@contrail/util",
3
+ "version": "1.0.21",
4
+ "description": "General javascript utilities",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
10
+ "lint": "tslint -p tsconfig.json",
11
+ "test:watch": "jest --watch",
12
+ "test": "jest"
13
+ },
14
+ "keywords": [],
15
+ "author": "",
16
+ "license": "ISC",
17
+ "devDependencies": {
18
+ "@types/jest": "^23.3.14",
19
+ "jest": "^23.6.0",
20
+ "prettier": "^1.19.1",
21
+ "ts-jest": "^23.10.5",
22
+ "tslint": "^5.11.0",
23
+ "tslint-config-prettier": "^1.18.0",
24
+ "typescript": "^4.0.0"
25
+ },
26
+ "jest": {
27
+ "moduleFileExtensions": [
28
+ "js",
29
+ "json",
30
+ "ts"
31
+ ],
32
+ "rootDir": "src",
33
+ "testRegex": ".spec.ts$",
34
+ "transform": {
35
+ "^.+\\.(t|j)s$": "ts-jest"
36
+ },
37
+ "coverageDirectory": "../coverage",
38
+ "testEnvironment": "node"
39
+ },
40
+ "dependencies": {}
41
+ }