@rimbu/deep 0.11.3 → 0.12.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/dist/main/deep.js +211 -0
- package/dist/main/deep.js.map +1 -0
- package/dist/main/index.js +7 -9
- package/dist/main/index.js.map +1 -1
- package/dist/main/internal.js +6 -4
- package/dist/main/internal.js.map +1 -1
- package/dist/main/match.js +160 -199
- package/dist/main/match.js.map +1 -1
- package/dist/main/patch.js +101 -125
- package/dist/main/patch.js.map +1 -1
- package/dist/main/path.js +109 -62
- package/dist/main/path.js.map +1 -1
- package/dist/main/protected.js +0 -19
- package/dist/main/protected.js.map +1 -1
- package/dist/main/selector.js +40 -0
- package/dist/main/selector.js.map +1 -0
- package/dist/main/tuple.js.map +1 -1
- package/dist/module/deep.js +192 -0
- package/dist/module/deep.js.map +1 -0
- package/dist/module/index.js +9 -1
- package/dist/module/index.js.map +1 -1
- package/dist/module/internal.js +4 -4
- package/dist/module/internal.js.map +1 -1
- package/dist/module/match.js +159 -179
- package/dist/module/match.js.map +1 -1
- package/dist/module/patch.js +91 -112
- package/dist/module/patch.js.map +1 -1
- package/dist/module/path.js +99 -44
- package/dist/module/path.js.map +1 -1
- package/dist/module/protected.js +1 -17
- package/dist/module/protected.js.map +1 -1
- package/dist/module/selector.js +36 -0
- package/dist/module/selector.js.map +1 -0
- package/dist/module/tuple.js.map +1 -1
- package/dist/types/deep.d.ts +284 -0
- package/dist/types/index.d.ts +10 -1
- package/dist/types/internal.d.ts +7 -4
- package/dist/types/match.d.ts +74 -80
- package/dist/types/patch.d.ts +57 -50
- package/dist/types/path.d.ts +177 -34
- package/dist/types/protected.d.ts +1 -16
- package/dist/types/selector.d.ts +47 -0
- package/dist/types/tuple.d.ts +10 -0
- package/package.json +3 -3
- package/src/deep.ts +362 -0
- package/src/index.ts +14 -10
- package/src/internal.ts +7 -4
- package/src/match.ts +396 -212
- package/src/patch.ts +173 -176
- package/src/path.ts +400 -74
- package/src/protected.ts +14 -25
- package/src/selector.ts +90 -0
- package/src/tuple.ts +12 -0
package/dist/main/patch.js
CHANGED
|
@@ -1,155 +1,131 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.patch =
|
|
3
|
+
exports.patch = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var base_1 = require("@rimbu/base");
|
|
6
|
-
var Patch;
|
|
7
|
-
(function (Patch) {
|
|
8
|
-
/**
|
|
9
|
-
* Returns a function that patches a given `value` with the given `patchItems`.
|
|
10
|
-
* @typeparam T - the patch value type
|
|
11
|
-
* @typeparam Q - the input value type
|
|
12
|
-
* @param patchItems - a number of `Patch` objects that patch a given value of type T.
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const items = [{ a: 1, b: 'a' }, { a: 2, b: 'b' }]
|
|
16
|
-
* items.map(Patch.create({ a: v => v + 1 }))
|
|
17
|
-
* // => [{ a: 2, b: 'a' }, { a: 3, b: 'b' }]
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
function create() {
|
|
21
|
-
var patchItems = [];
|
|
22
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
23
|
-
patchItems[_i] = arguments[_i];
|
|
24
|
-
}
|
|
25
|
-
return function (value) { return patch.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(patchItems), false)); };
|
|
26
|
-
}
|
|
27
|
-
Patch.create = create;
|
|
28
|
-
})(Patch = exports.Patch || (exports.Patch = {}));
|
|
29
|
-
var NestedObj = /** @class */ (function () {
|
|
30
|
-
function NestedObj(patchDataItems) {
|
|
31
|
-
this.patchDataItems = patchDataItems;
|
|
32
|
-
}
|
|
33
|
-
return NestedObj;
|
|
34
|
-
}());
|
|
35
|
-
/**
|
|
36
|
-
* Returns a nested patch object based on the given `patchDataItems` that work on a subpart
|
|
37
|
-
* of a larger object to be patched.
|
|
38
|
-
* @typeparam T - the input value type
|
|
39
|
-
* @typeparam R - the root object type
|
|
40
|
-
* @typeparam Q - the patch type
|
|
41
|
-
* @param patchDataItems - a number of `Patch` objects to be applied to the subpart of the object
|
|
42
|
-
* @example
|
|
43
|
-
* ```ts
|
|
44
|
-
* patch({ a: 1, b: { c: true, d: 'a' } }, { b: patchNested({ d: 'b' }) })
|
|
45
|
-
* // => { a: 1, b: { c: true, d: 'b' } }
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
function patchNested() {
|
|
49
|
-
var patchDataItems = [];
|
|
50
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
51
|
-
patchDataItems[_i] = arguments[_i];
|
|
52
|
-
}
|
|
53
|
-
return new NestedObj(patchDataItems);
|
|
54
|
-
}
|
|
55
|
-
exports.patchNested = patchNested;
|
|
56
6
|
/**
|
|
57
7
|
* Returns an immutably updated version of the given `value` where the given `patchItems` have been
|
|
58
8
|
* applied to the result.
|
|
9
|
+
* The Rimbu patch notation is as follows:
|
|
10
|
+
* - if the target is a simple value or array, the patch can be the same type or a function returning the same type
|
|
11
|
+
* - if the target is a tuple (array of fixed length), the patch be the same type or an object containing numeric keys with patches indicating the tuple index to patch
|
|
12
|
+
* - if the target is an object, the patch can be the same type, or an array containing partial keys with their patches for the object
|
|
13
|
+
* @typeparam T - the type of the value to patch
|
|
14
|
+
* @typeparam TE - a utility type
|
|
15
|
+
* @typeparam TT - a utility type
|
|
59
16
|
* @param value - the input value to patch
|
|
60
|
-
* @param
|
|
17
|
+
* @param patchItem - the `Patch` value to apply to the input value
|
|
61
18
|
* @example
|
|
62
19
|
* ```ts
|
|
63
20
|
* const input = { a: 1, b: { c: true, d: 'a' } }
|
|
64
|
-
* patch(input, { a: 2 }) // => { a: 2, b: { c: true, d: 'a' } }
|
|
65
|
-
* patch(input
|
|
21
|
+
* patch(input, [{ a: 2 }]) // => { a: 2, b: { c: true, d: 'a' } }
|
|
22
|
+
* patch(input, [{ b: [{ c: (v) => !v }] }] )
|
|
66
23
|
* // => { a: 1, b: { c: false, d: 'a' } }
|
|
67
|
-
* patch(input:
|
|
24
|
+
* patch(input: [{ a: (v) => v + 1, b: [{ d: 'q' }] }] )
|
|
68
25
|
* // => { a: 2, b: { c: true, d: 'q' } }
|
|
69
26
|
* ```
|
|
70
27
|
*/
|
|
71
|
-
function patch(value) {
|
|
72
|
-
|
|
73
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
74
|
-
patchItems[_i - 1] = arguments[_i];
|
|
75
|
-
}
|
|
76
|
-
var newValue = (0, base_1.isPlainObj)(value) ? tslib_1.__assign({}, value) : value;
|
|
77
|
-
var changedRef = { changed: false };
|
|
78
|
-
var result = processPatch(newValue, newValue, patchItems, changedRef);
|
|
79
|
-
if (changedRef.changed)
|
|
80
|
-
return result;
|
|
81
|
-
return value;
|
|
28
|
+
function patch(value, patchItem) {
|
|
29
|
+
return patchEntry(value, value, value, patchItem);
|
|
82
30
|
}
|
|
83
31
|
exports.patch = patch;
|
|
84
|
-
function
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
while (++i < len) {
|
|
88
|
-
var patchItem = patchDataItems[i];
|
|
89
|
-
if (patchItem instanceof Function) {
|
|
90
|
-
var item = patchItem(patchNested);
|
|
91
|
-
if (item instanceof NestedObj) {
|
|
92
|
-
processPatch(value, root, item.patchDataItems, changedRef);
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
processPatchObj(value, root, item, changedRef);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
processPatchObj(value, root, patchItem, changedRef);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
function processPatchObj(value, root, patchData, changedRef) {
|
|
105
|
-
if (undefined === value || null === value) {
|
|
32
|
+
function patchEntry(value, parent, root, patchItem) {
|
|
33
|
+
if (Object.is(value, patchItem)) {
|
|
34
|
+
// patching a value with itself never changes the value
|
|
106
35
|
return value;
|
|
107
36
|
}
|
|
108
|
-
if (
|
|
109
|
-
|
|
37
|
+
if (typeof value === 'function') {
|
|
38
|
+
// function input, directly return patch
|
|
39
|
+
return patchItem;
|
|
110
40
|
}
|
|
111
|
-
if (
|
|
112
|
-
|
|
41
|
+
if (typeof patchItem === 'function') {
|
|
42
|
+
// function patch always needs to be resolved first
|
|
43
|
+
var item = patchItem(value, parent, root);
|
|
44
|
+
return patchEntry(value, parent, root, item);
|
|
113
45
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
46
|
+
if ((0, base_1.isPlainObj)(value)) {
|
|
47
|
+
// value is plain object
|
|
48
|
+
return patchPlainObj(value, root, patchItem);
|
|
49
|
+
}
|
|
50
|
+
if (Array.isArray(value)) {
|
|
51
|
+
// value is tuple or array
|
|
52
|
+
return patchArr(value, root, patchItem);
|
|
53
|
+
}
|
|
54
|
+
// value is primitive type or complex object
|
|
55
|
+
return patchItem;
|
|
56
|
+
}
|
|
57
|
+
function patchPlainObj(value, root, patchItem) {
|
|
58
|
+
var e_1, _a;
|
|
59
|
+
if (!Array.isArray(patchItem)) {
|
|
60
|
+
// the patch is a complete replacement of the current value
|
|
61
|
+
return patchItem;
|
|
62
|
+
}
|
|
63
|
+
// patch is an array of partial updates
|
|
64
|
+
// copy the input value
|
|
65
|
+
var result = tslib_1.__assign({}, value);
|
|
66
|
+
var anyChange = false;
|
|
67
|
+
try {
|
|
68
|
+
// loop over patches in array
|
|
69
|
+
for (var patchItem_1 = tslib_1.__values(patchItem), patchItem_1_1 = patchItem_1.next(); !patchItem_1_1.done; patchItem_1_1 = patchItem_1.next()) {
|
|
70
|
+
var entry = patchItem_1_1.value;
|
|
71
|
+
// update root if needed
|
|
72
|
+
var currentRoot = value === root ? tslib_1.__assign({}, result) : root;
|
|
73
|
+
// loop over all the patch keys
|
|
74
|
+
for (var key in entry) {
|
|
75
|
+
// patch the value at the given key with the patch at that key
|
|
76
|
+
var currentValue = result[key];
|
|
77
|
+
var newValue = patchEntry(currentValue, value, currentRoot, entry[key]);
|
|
78
|
+
if (!Object.is(currentValue, newValue)) {
|
|
79
|
+
// if value changed, set it in result and mark change
|
|
80
|
+
anyChange = true;
|
|
81
|
+
result[key] = newValue;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
134
84
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
85
|
+
}
|
|
86
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
87
|
+
finally {
|
|
88
|
+
try {
|
|
89
|
+
if (patchItem_1_1 && !patchItem_1_1.done && (_a = patchItem_1.return)) _a.call(patchItem_1);
|
|
138
90
|
}
|
|
91
|
+
finally { if (e_1) throw e_1.error; }
|
|
139
92
|
}
|
|
93
|
+
if (anyChange) {
|
|
94
|
+
// something changed, return new value
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
// nothing changed, return old value
|
|
140
98
|
return value;
|
|
141
99
|
}
|
|
142
|
-
function
|
|
143
|
-
if (
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
100
|
+
function patchArr(value, root, patchItem) {
|
|
101
|
+
if (Array.isArray(patchItem)) {
|
|
102
|
+
// value is a normal array
|
|
103
|
+
// patch is a complete replacement of current array
|
|
104
|
+
return patchItem;
|
|
105
|
+
}
|
|
106
|
+
// value is a tuple
|
|
107
|
+
// patch is an object containing numeric keys with function values
|
|
108
|
+
// that update the tuple at the given indices
|
|
109
|
+
// copy the tuple
|
|
110
|
+
var result = tslib_1.__spreadArray([], tslib_1.__read(value), false);
|
|
111
|
+
var anyChange = false;
|
|
112
|
+
// loop over all index keys in object
|
|
113
|
+
for (var index in patchItem) {
|
|
114
|
+
var numIndex = index;
|
|
115
|
+
// patch the tuple at the given index
|
|
116
|
+
var currentValue = result[numIndex];
|
|
117
|
+
var newValue = patchEntry(currentValue, value, root, patchItem[index]);
|
|
118
|
+
if (!Object.is(newValue, currentValue)) {
|
|
119
|
+
// if value changed, set it in result and mark change
|
|
120
|
+
anyChange = true;
|
|
121
|
+
result[numIndex] = newValue;
|
|
150
122
|
}
|
|
151
|
-
return value;
|
|
152
123
|
}
|
|
153
|
-
|
|
124
|
+
if (anyChange) {
|
|
125
|
+
// something changed, return new value
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
// nothing changed, return old value
|
|
129
|
+
return value;
|
|
154
130
|
}
|
|
155
131
|
//# sourceMappingURL=patch.js.map
|
package/dist/main/patch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../../src/patch.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"patch.js","sourceRoot":"","sources":["../../src/patch.ts"],"names":[],"mappings":";;;;AAAA,oCAAyE;AAwFzE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,KAAK,CACnB,KAAQ,EACR,SAAwB;IAExB,OAAO,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,SAAqB,CAAC,CAAC;AAChE,CAAC;AALD,sBAKC;AAED,SAAS,UAAU,CACjB,KAAQ,EACR,MAAS,EACT,IAAO,EACP,SAAkC;IAElC,IAAI,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,EAAE;QAC/B,uDAAuD;QACvD,OAAO,KAAK,CAAC;KACd;IAED,IAAI,OAAO,KAAK,KAAK,UAAU,EAAE;QAC/B,wCAAwC;QACxC,OAAO,SAAc,CAAC;KACvB;IAED,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE;QACnC,mDAAmD;QACnD,IAA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1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Path = void 0;
|
|
3
|
+
exports.patchAt = exports.getAt = exports.Path = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var internal_1 = require("./internal");
|
|
6
6
|
var Path;
|
|
7
7
|
(function (Path) {
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @typeparam T - the object type to select in
|
|
11
|
-
* @typeparam P - a Path in object type T
|
|
12
|
-
* @param source - the object to select in
|
|
13
|
-
* @param path - the path into the object
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* console.log(Path.get({ a: { b: { c: 5 } } }), 'a.b')
|
|
17
|
-
* // => { c: 5 }
|
|
18
|
-
* console.log(Path.get({ a: { b: { c: 5 } } }), 'a.b.c')
|
|
19
|
-
* // => 5
|
|
20
|
-
* ```
|
|
9
|
+
* Regular expression used to split a path string into tokens.
|
|
21
10
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
11
|
+
Path.stringSplitRegex = /\?\.|\.|\[|\]/g;
|
|
12
|
+
/**
|
|
13
|
+
* Return the given `path` string split into an array of subpaths.
|
|
14
|
+
* @param path - the input string path
|
|
15
|
+
*/
|
|
16
|
+
function stringSplit(path) {
|
|
17
|
+
return path.split(Path.stringSplitRegex);
|
|
18
|
+
}
|
|
19
|
+
Path.stringSplit = stringSplit;
|
|
20
|
+
})(Path = exports.Path || (exports.Path = {}));
|
|
21
|
+
/**
|
|
22
|
+
* Returns the value resulting from selecting the given `path` in the given `source` object.
|
|
23
|
+
* It supports optional chaining for nullable values or values that may be undefined, and also
|
|
24
|
+
* for accessing objects inside an array.
|
|
25
|
+
* There is currently no support for forcing non-null (the `!` operator).
|
|
26
|
+
* @typeparam T - the object type to select in
|
|
27
|
+
* @typeparam P - a Path in object type T
|
|
28
|
+
* @param source - the object to select in
|
|
29
|
+
* @param path - the path into the object
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const value = { a: { b: { c: [{ d: 5 }, { d: 6 }] } } }
|
|
33
|
+
* Deep.getAt(value, 'a.b');
|
|
34
|
+
* // => { c: 5 }
|
|
35
|
+
* Deep.getAt(value, 'a.b.c');
|
|
36
|
+
* // => [{ d: 5 }, { d: 5 }]
|
|
37
|
+
* Deep.getAt(value, 'a.b.c[1]');
|
|
38
|
+
* // => { d: 6 }
|
|
39
|
+
* Deep.getAt(value, 'a.b.c[1]?.d');
|
|
40
|
+
* // => 6
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function getAt(source, path) {
|
|
44
|
+
var e_1, _a;
|
|
45
|
+
if (path === '') {
|
|
46
|
+
// empty path always directly returns source value
|
|
47
|
+
return source;
|
|
48
|
+
}
|
|
49
|
+
var items = Path.stringSplit(path);
|
|
50
|
+
// start with `source` as result value
|
|
51
|
+
var result = source;
|
|
52
|
+
try {
|
|
53
|
+
for (var items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
54
|
+
var item = items_1_1.value;
|
|
55
|
+
if (undefined === item || item === '' || item === '[') {
|
|
56
|
+
// ignore irrelevant items
|
|
57
|
+
continue;
|
|
30
58
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
59
|
+
if (undefined === result || null === result) {
|
|
60
|
+
// optional chaining assumed and no value available, skip rest of path and return undefined
|
|
61
|
+
return undefined;
|
|
36
62
|
}
|
|
37
|
-
|
|
63
|
+
// set current result to subpath value
|
|
64
|
+
result = result[item];
|
|
38
65
|
}
|
|
39
|
-
return result;
|
|
40
66
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
* Sets the value at the given path in the source to the given value.
|
|
44
|
-
* @param source - the object to update
|
|
45
|
-
* @param path - the path in the object to update
|
|
46
|
-
* @param value - the new value to set at the given position
|
|
47
|
-
* @example
|
|
48
|
-
* ```ts
|
|
49
|
-
* console.log(Path.update({ a: { b: { c: 5 } } }, 'a.b.c', v => v + 5)
|
|
50
|
-
* // => { a: { b: { c: 6 } } }
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
function update(source, path, value) {
|
|
54
|
-
var e_2, _a;
|
|
55
|
-
var items = path.split('.');
|
|
56
|
-
var last = items.pop();
|
|
57
|
-
var root = {};
|
|
58
|
-
var current = root;
|
|
67
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
68
|
+
finally {
|
|
59
69
|
try {
|
|
60
|
-
|
|
61
|
-
var item = items_2_1.value;
|
|
62
|
-
var next = {};
|
|
63
|
-
current[item] = (0, internal_1.patchNested)(next);
|
|
64
|
-
current = next;
|
|
65
|
-
}
|
|
70
|
+
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
66
71
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
finally { if (e_1) throw e_1.error; }
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
exports.getAt = getAt;
|
|
77
|
+
/**
|
|
78
|
+
* Patches the value at the given path in the source to the given value.
|
|
79
|
+
* Because the path to update must exist in the `source` object, optional
|
|
80
|
+
* chaining and array indexing is not allowed.
|
|
81
|
+
* @param source - the object to update
|
|
82
|
+
* @param path - the path in the object to update
|
|
83
|
+
* @param patchItem - the patch for the value at the given path
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* const value = { a: { b: { c: 5 } } };
|
|
87
|
+
* Deep.patchAt(value, 'a.b.c', v => v + 5);
|
|
88
|
+
* // => { a: { b: { c: 6 } } }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
function patchAt(source, path, patchItem) {
|
|
92
|
+
if (path === '') {
|
|
93
|
+
return internal_1.Deep.patch(source, patchItem);
|
|
94
|
+
}
|
|
95
|
+
var items = Path.stringSplit(path);
|
|
96
|
+
// creates a patch object based on the current path
|
|
97
|
+
function createPatchPart(index, target) {
|
|
98
|
+
var _a;
|
|
99
|
+
if (index === items.length) {
|
|
100
|
+
// processed all items, return the input `patchItem`
|
|
101
|
+
return patchItem;
|
|
73
102
|
}
|
|
74
|
-
|
|
75
|
-
|
|
103
|
+
var item = items[index];
|
|
104
|
+
if (undefined === item || item === '') {
|
|
105
|
+
// empty items can be ignored
|
|
106
|
+
return createPatchPart(index + 1, target);
|
|
107
|
+
}
|
|
108
|
+
if (item === '[') {
|
|
109
|
+
// next item is array index, set arrayMode to true
|
|
110
|
+
return createPatchPart(index + 1, target);
|
|
111
|
+
}
|
|
112
|
+
// create object with subPart as property key, and the restuls of processing next parts as value
|
|
113
|
+
var result = (_a = {},
|
|
114
|
+
_a[item] = createPatchPart(index + 1, target[item]),
|
|
115
|
+
_a);
|
|
116
|
+
if (Array.isArray(target)) {
|
|
117
|
+
// target in source object is array/tuple, so the patch should be object
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
// target in source is not an array, so it patch should be an array
|
|
121
|
+
return [result];
|
|
76
122
|
}
|
|
77
|
-
|
|
78
|
-
}
|
|
123
|
+
return internal_1.Deep.patch(source, createPatchPart(0, source));
|
|
124
|
+
}
|
|
125
|
+
exports.patchAt = patchAt;
|
|
79
126
|
//# sourceMappingURL=path.js.map
|
package/dist/main/path.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/path.ts"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"path.js","sourceRoot":"","sources":["../../src/path.ts"],"names":[],"mappings":";;;;AACA,uCAAyC;AAGzC,IAAiB,IAAI,CAsTpB;AAtTD,WAAiB,IAAI;IAqSn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mDAAmD;IACnD,SAAS,eAAe,CAAC,KAAa,EAAE,MAAW;;QACjD,IAAI,KAAK,KAAK,KAAK,CAAC,MAAM,EAAE;YAC1B,oDAAoD;YACpD,OAAO,SAAS,CAAC;SAClB;QAED,IAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QAE1B,IAAI,SAAS,KAAK,IAAI,IAAI,IAAI,KAAK,EAAE,EAAE;YACrC,6BAA6B;YAC7B,OAAO,eAAe,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;SAC3C;QAED,IAAI,IAAI,KAAK,GAAG,EAAE;YAChB,kDAAkD;YAClD,OAAO,eAAe,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC;SAC3C;QAED,gGAAgG;QAChG,IAAM,MAAM;YACV,GAAC,IAAI,IAAG,eAAe,CAAC,KAAK,GAAG,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;eACjD,CAAC;QAEF,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACzB,wEAAwE;YACxE,OAAO,MAAM,CAAC;SACf;QAED,mEAAmE;QACnE,OAAO,CAAC,MAAM,CAAC,CAAC;IAClB,CAAC;IAED,OAAO,eAAI,CAAC,KAAK,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;AACxD,CAAC;AA7CD,0BA6CC"}
|
package/dist/main/protected.js
CHANGED
|
@@ -1,22 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Protected = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Returns the same value wrapped in the `Protected` type.
|
|
6
|
-
* @param value - the value to wrap
|
|
7
|
-
* @note does not perform any runtime protection, it is only a utility to easily add the `Protected`
|
|
8
|
-
* type to a value
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* const obj = Protected({ a: 1, b: { c: true, d: [1] } })
|
|
12
|
-
* obj.a = 2 // compiler error: a is readonly
|
|
13
|
-
* obj.b.c = false // compiler error: c is readonly
|
|
14
|
-
* obj.b.d.push(2) // compiler error: d is a readonly array
|
|
15
|
-
* (obj as any).b.d.push(2) // will actually mutate the object
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
function Protected(value) {
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
exports.Protected = Protected;
|
|
22
3
|
//# sourceMappingURL=protected.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protected.js","sourceRoot":"","sources":["../../src/protected.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"protected.js","sourceRoot":"","sources":["../../src/protected.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.select = void 0;
|
|
4
|
+
var internal_1 = require("./internal");
|
|
5
|
+
/**
|
|
6
|
+
* Returns the result of applying the given `selector` shape to the given `source` value.
|
|
7
|
+
* @typeparam T - the patch value type
|
|
8
|
+
* @typeparam SL - the selector shape type
|
|
9
|
+
* @param source - the source value to select from
|
|
10
|
+
* @param selector - a shape indicating the selection from the source values
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const item = { a: { b: 1, c: 'a' } };
|
|
14
|
+
* Deep.select(item, { q: 'a.c', y: ['a.b', 'a.c'], z: (v) => v.a.b + 1 });
|
|
15
|
+
* // => { q: 'a', y: [1, 'a'], z: 2 }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function select(source, selector) {
|
|
19
|
+
if (typeof selector === 'function') {
|
|
20
|
+
// selector is function, resolve selector function
|
|
21
|
+
return selector(source);
|
|
22
|
+
}
|
|
23
|
+
else if (typeof selector === 'string') {
|
|
24
|
+
// selector is string path, get the value at the given path
|
|
25
|
+
return internal_1.Deep.getAt(source, selector);
|
|
26
|
+
}
|
|
27
|
+
else if (Array.isArray(selector)) {
|
|
28
|
+
// selector is tuple, get each tuple item value
|
|
29
|
+
return selector.map(function (s) { return select(source, s); });
|
|
30
|
+
}
|
|
31
|
+
// selector is object
|
|
32
|
+
var result = {};
|
|
33
|
+
for (var key in selector) {
|
|
34
|
+
// set each selected object key to the selector value
|
|
35
|
+
result[key] = select(source, selector[key]);
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
exports.select = select;
|
|
40
|
+
//# sourceMappingURL=selector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.js","sourceRoot":"","sources":["../../src/selector.ts"],"names":[],"mappings":";;;AACA,uCAAwD;AAkDxD;;;;;;;;;;;;GAYG;AACH,SAAgB,MAAM,CACpB,MAAS,EACT,QAA4B;IAE5B,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE;QAClC,kDAAkD;QAClD,OAAQ,QAAgB,CAAC,MAAsB,CAAC,CAAC;KAClD;SAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QACvC,2DAA2D;QAC3D,OAAO,eAAI,CAAC,KAAK,CAAC,MAAM,EAAE,QAAuB,CAAQ,CAAC;KAC3D;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;QAClC,+CAA+C;QAC/C,OAAO,QAAQ,CAAC,GAAG,CAAC,UAAC,CAAC,IAAK,OAAA,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,EAAjB,CAAiB,CAAQ,CAAC;KACtD;IAED,qBAAqB;IAErB,IAAM,MAAM,GAAQ,EAAE,CAAC;IAEvB,KAAK,IAAM,GAAG,IAAI,QAAe,EAAE;QACjC,qDAAqD;QACrD,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAG,QAAgB,CAAC,GAAG,CAAC,CAAC,CAAC;KACtD;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAzBD,wBAyBC"}
|
package/dist/main/tuple.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tuple.js","sourceRoot":"","sources":["../../src/tuple.ts"],"names":[],"mappings":";;;;AAAA,oCAAkC;AAQlC,IAAiB,KAAK,
|
|
1
|
+
{"version":3,"file":"tuple.js","sourceRoot":"","sources":["../../src/tuple.ts"],"names":[],"mappings":";;;;AAAA,oCAAkC;AAQlC,IAAiB,KAAK,CA4LrB;AA5LD,WAAiB,KAAK;IAuBpB;;;;;;;;OAQG;IACH,SAAgB,EAAE;QAAiC,gBAAY;aAAZ,UAAY,EAAZ,qBAAY,EAAZ,IAAY;YAAZ,2BAAY;;QAC7D,OAAO,MAAa,CAAC;IACvB,CAAC;IAFe,QAAE,KAEjB,CAAA;IAED;;;;;;;;;;OAUG;IACH,SAAgB,QAAQ,CACtB,KAAQ,EACR,KAAQ;QAER,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;IALe,cAAQ,WAKvB,CAAA;IAED;;;;;;;;;OASG;IACH,SAAgB,KAAK,CAAyB,KAAQ;QACpD,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAFe,WAAK,QAEpB,CAAA;IAED;;;;;;;;;OASG;IACH,SAAgB,MAAM,CAAyB,KAAQ;QACrD,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAFe,YAAM,SAErB,CAAA;IAED;;;;;;;;;OASG;IACH,SAAgB,IAAI,CAClB,KAAyB;QAEzB,OAAO,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAQ,CAAC;IACxC,CAAC;IAJe,UAAI,OAInB,CAAA;IAED;;;;;;;;;;;;OAYG;IACH,SAAgB,QAAQ,CACtB,KAAQ,EACR,KAAQ,EACR,OAAqB;QAErB,OAAO,UAAG,CAAC,MAAM,CAAC,KAAK,EAAE,KAAe,EAAE,OAAO,CAAM,CAA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