@contrail/transform-data 1.3.0-alpha.3 → 1.3.0
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.
|
@@ -44,14 +44,17 @@ class ObjectPathUtil {
|
|
|
44
44
|
}
|
|
45
45
|
static setValueAtPath({ obj, pathParts, value, options, }) {
|
|
46
46
|
let current = obj;
|
|
47
|
-
let pathHasArrayIndex = false;
|
|
48
47
|
for (let i = 0; i < pathParts.length - 1; i++) {
|
|
49
48
|
const key = pathParts[i];
|
|
50
49
|
const isCurrentPartArrayIndex = ObjectPathUtil.isArrayIndexPart(key);
|
|
51
50
|
if (isCurrentPartArrayIndex) {
|
|
52
|
-
pathHasArrayIndex = true;
|
|
53
51
|
const idx = ObjectPathUtil.getArrayIndex(key);
|
|
54
52
|
ObjectPathUtil.ensureArrayFilledTo(current, idx);
|
|
53
|
+
const isNextPartArrayIndex = ObjectPathUtil.isArrayIndexPart(pathParts[i + 1]);
|
|
54
|
+
const isElementNotArray = isNextPartArrayIndex && !Array.isArray(current[idx]);
|
|
55
|
+
if (isElementNotArray) {
|
|
56
|
+
current[idx] = [];
|
|
57
|
+
}
|
|
55
58
|
current = current[idx];
|
|
56
59
|
continue;
|
|
57
60
|
}
|
|
@@ -68,11 +71,14 @@ class ObjectPathUtil {
|
|
|
68
71
|
if (isFinalKeyArrayIndex) {
|
|
69
72
|
const idx = ObjectPathUtil.getArrayIndex(finalKey);
|
|
70
73
|
ObjectPathUtil.ensureArrayFilledTo(current, idx);
|
|
71
|
-
current[idx]
|
|
74
|
+
const isArrayIndexEmpty = current[idx] == null || (typeof current[idx] === 'object' && Object.keys(current[idx]).length === 0);
|
|
75
|
+
if (options.shouldOverwriteExisting || isArrayIndexEmpty) {
|
|
76
|
+
current[idx] = value;
|
|
77
|
+
}
|
|
72
78
|
return;
|
|
73
79
|
}
|
|
74
|
-
const shouldWriteProperty =
|
|
75
|
-
if (shouldWriteProperty
|
|
80
|
+
const shouldWriteProperty = options.shouldOverwriteExisting || !Object.prototype.hasOwnProperty.call(current, finalKey);
|
|
81
|
+
if (shouldWriteProperty) {
|
|
76
82
|
current[finalKey] = value;
|
|
77
83
|
}
|
|
78
84
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrail/transform-data",
|
|
3
|
-
"version": "1.3.0
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Libraries for transforming data",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
10
10
|
"lint": "tslint --fix -p tsconfig.json",
|
|
11
11
|
"test": "jest",
|
|
12
|
+
"test-debug": "npx jest --runInBand",
|
|
12
13
|
"test:watch": "jest --watch"
|
|
13
14
|
},
|
|
14
15
|
"keywords": [],
|