@finnair/path 3.1.0 → 4.0.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [4.0.0](https://github.com/finnair/v-validation/compare/v3.2.0...v4.0.0) (2022-11-07)
7
+
8
+ **Note:** Version bump only for package @finnair/path
9
+
10
+
11
+
12
+
13
+
6
14
  # [3.1.0](https://github.com/finnair/v-validation/compare/v3.0.0...v3.1.0) (2022-10-24)
7
15
 
8
16
  **Note:** Version bump only for package @finnair/path
package/README.md CHANGED
@@ -64,6 +64,9 @@ changes.forEach((newValue, path) => {
64
64
  updateResource(latestValue);
65
65
  ```
66
66
 
67
+ NOTE: Updating an array with `Path.set` will truncate possible undefined values from the end of the array. This allows
68
+ removing trailing elements without leaving undefined elements in place.
69
+
67
70
  ### Include/Exclude Projection
68
71
 
69
72
  While GraphQL is all about projections, something similar can also be implemented in a REST API with include/exclude parameters. `Projection` and `parsePathMatcher` function provides means to process results safely based on such a user input. This is, of course, very simplified projection compared to what GraphQL has to offer, but it's also... well, simpler.
@@ -92,9 +95,7 @@ Path.of(); // Root object $ - also Path.ROOT
92
95
  Path.of('array', 1, 'property'); // $.array[1].property
93
96
 
94
97
  // Constructing with fluent syntax
95
- Path.of()
96
- .property('array')
97
- .index(0); // $.array[0]
98
+ Path.of().property('array').index(0); // $.array[0]
98
99
 
99
100
  // Concatenating Paths
100
101
  Path.of('parent').concat(Path.of('child', 'name')); // $.parent.child.name
@@ -119,6 +120,9 @@ Path.of('array', 1).unset({ array: [1, 2, 3] }); // { array: [1, undefined, 3] }
119
120
  // ...but unsetting a value doesn't create intermediate objects
120
121
  Path.of('array', 1).unset({}); // {}
121
122
 
123
+ // Trailing undefined elements will be removed from an array (i.e. array is resized)
124
+ Path.of('array', 2).set({ array: [1, undefined, 3] }, undefined); // { array: [1] } where array.length === 1
125
+
122
126
  // toJSON() returns JsonPath compatible serialization
123
127
  Path.of('array', 0, 'property with spaces').toJSON(); // $.array[0]["property with spaces"]
124
128
  ```
package/dist/Path.js CHANGED
@@ -56,22 +56,30 @@ class Path {
56
56
  if (this.path.length === 0) {
57
57
  return value;
58
58
  }
59
- let index = -1;
59
+ let pathIndex = -1;
60
60
  const _root = toObject(root, this.path);
61
61
  let current = _root;
62
- for (index = 0; index < this.path.length - 1 && current; index++) {
63
- const component = this.path[index];
62
+ for (pathIndex = 0; pathIndex < this.path.length - 1 && current; pathIndex++) {
63
+ const component = this.path[pathIndex];
64
64
  const child = toObject(current[component], this.path);
65
65
  current[component] = child;
66
66
  current = child;
67
67
  }
68
68
  if (value === undefined) {
69
69
  if (current !== undefined) {
70
- delete current[this.path[index]];
70
+ delete current[this.path[pathIndex]];
71
+ // Truncate undefined tail of an array
72
+ if (Array.isArray(current)) {
73
+ let i = current.length - 1;
74
+ while (i >= 0 && current[i] === undefined) {
75
+ i--;
76
+ }
77
+ current.length = i + 1;
78
+ }
71
79
  }
72
80
  }
73
81
  else {
74
- current[this.path[index]] = value;
82
+ current[this.path[pathIndex]] = value;
75
83
  }
76
84
  return _root;
77
85
  function toObject(current, path) {
@@ -79,7 +87,7 @@ class Path {
79
87
  return current;
80
88
  }
81
89
  else if (value !== undefined) {
82
- if (typeof path[index + 1] === 'number') {
90
+ if (typeof path[pathIndex + 1] === 'number') {
83
91
  return [];
84
92
  }
85
93
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@finnair/path",
3
- "version": "3.1.0",
3
+ "version": "4.0.0",
4
4
  "private": false,
5
5
  "description": "Simple object path as array of strings and numbers",
6
6
  "main": "dist/index.js",
@@ -22,5 +22,5 @@
22
22
  "scripts": {
23
23
  "build": "tsc -b ."
24
24
  },
25
- "gitHead": "5182a63fa61cc1f5c8991e1482c5bbffe14798c3"
25
+ "gitHead": "d6540b51c4f1c57563602f66bd3157637703529b"
26
26
  }