@finnair/path 3.0.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 +6 -29
- package/README.md +7 -3
- package/dist/Path.js +14 -6
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
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
|
-
# [
|
|
6
|
+
# [4.0.0](https://github.com/finnair/v-validation/compare/v3.2.0...v4.0.0) (2022-11-07)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @finnair/path
|
|
9
9
|
|
|
@@ -11,7 +11,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
# [3.
|
|
14
|
+
# [3.1.0](https://github.com/finnair/v-validation/compare/v3.0.0...v3.1.0) (2022-10-24)
|
|
15
15
|
|
|
16
16
|
**Note:** Version bump only for package @finnair/path
|
|
17
17
|
|
|
@@ -19,57 +19,34 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
# [3.0.0
|
|
23
|
-
|
|
24
|
-
**Note:** Version bump only for package @finnair/path
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
# [3.0.0](https://github.com/finnair/v-validation/compare/v2.0.0...v3.0.0) (2022-10-03)
|
|
27
23
|
|
|
24
|
+
### Features
|
|
28
25
|
|
|
26
|
+
- Use Object.freeze to ensure immutability.
|
|
29
27
|
|
|
30
28
|
# [2.0.0](https://github.com/finnair/v-validation/compare/v1.1.0...v2.0.0) (2022-09-08)
|
|
31
29
|
|
|
32
30
|
**Note:** Version bump only for package @finnair/path
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
32
|
# [1.1.0](https://github.com/finnair/v-validation/compare/v1.0.1...v1.1.0) (2022-08-29)
|
|
39
33
|
|
|
40
|
-
|
|
41
34
|
### Features
|
|
42
35
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
- Support for Luxon ([94b3806](https://github.com/finnair/v-validation/commit/94b38060e07feeb0abb8c81659d8bda537a4d9aa))
|
|
48
37
|
|
|
49
38
|
# [1.1.0-alpha.6](https://github.com/finnair/v-validation/compare/v1.0.1...v1.1.0-alpha.6) (2022-08-17)
|
|
50
39
|
|
|
51
40
|
**Note:** Version bump only for package @finnair/path
|
|
52
41
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
42
|
# [1.1.0-alpha.5](https://github.com/finnair/v-validation/compare/v1.0.1...v1.1.0-alpha.5) (2022-08-16)
|
|
58
43
|
|
|
59
44
|
**Note:** Version bump only for package @finnair/path
|
|
60
45
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
46
|
# [1.1.0-alpha.0](https://github.com/finnair/v-validation/compare/v1.0.1...v1.1.0-alpha.0) (2022-08-12)
|
|
66
47
|
|
|
67
48
|
**Note:** Version bump only for package @finnair/path
|
|
68
49
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
50
|
## [1.0.1](https://github.com/finnair/v-validation/compare/v0.9.1...v1.0.1) (2022-01-20)
|
|
74
51
|
|
|
75
52
|
### Features
|
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
|
|
59
|
+
let pathIndex = -1;
|
|
60
60
|
const _root = toObject(root, this.path);
|
|
61
61
|
let current = _root;
|
|
62
|
-
for (
|
|
63
|
-
const component = this.path[
|
|
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[
|
|
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[
|
|
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[
|
|
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
|
+
"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": "
|
|
25
|
+
"gitHead": "d6540b51c4f1c57563602f66bd3157637703529b"
|
|
26
26
|
}
|