@contrail/transform-data 1.3.0-alpha.2 → 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.
@@ -1,5 +1,9 @@
1
1
  {
2
2
  "permissions": {
3
- "allow": ["Bash(npx jest --no-coverage)", "Bash(git checkout:*)"]
3
+ "allow": [
4
+ "Bash(npx jest --no-coverage)",
5
+ "Bash(git checkout:*)",
6
+ "Bash(npx jest:*)"
7
+ ]
4
8
  }
5
9
  }
@@ -13,7 +13,7 @@ export declare class ObjectPathUtil {
13
13
  pathParts: string[];
14
14
  value: unknown;
15
15
  options: {
16
- shouldOverwrite: boolean;
16
+ shouldOverwriteExisting: boolean;
17
17
  };
18
18
  }): void;
19
19
  private static expandArraySegments;
@@ -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] = value;
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 = pathHasArrayIndex || !Object.prototype.hasOwnProperty.call(current, finalKey);
75
- if (shouldWriteProperty || options.shouldOverwrite) {
80
+ const shouldWriteProperty = options.shouldOverwriteExisting || !Object.prototype.hasOwnProperty.call(current, finalKey);
81
+ if (shouldWriteProperty) {
76
82
  current[finalKey] = value;
77
83
  }
78
84
  }
@@ -22,7 +22,7 @@ class RekeyTransformer {
22
22
  obj: row,
23
23
  pathParts: newKeyParts,
24
24
  value: oldValue,
25
- options: { shouldOverwrite: false },
25
+ options: { shouldOverwriteExisting: false },
26
26
  });
27
27
  }
28
28
  else if (rekeyKeepMissingValues) {
@@ -30,7 +30,7 @@ class RekeyTransformer {
30
30
  obj: row,
31
31
  pathParts: newKeyParts,
32
32
  value: undefined,
33
- options: { shouldOverwrite: false },
33
+ options: { shouldOverwriteExisting: false },
34
34
  });
35
35
  }
36
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/transform-data",
3
- "version": "1.3.0-alpha.2",
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": [],