rambda 10.1.0 → 10.2.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/README.md +129 -0
- package/dist/rambda.cjs +61 -32
- package/dist/rambda.js +61 -33
- package/dist/rambda.umd.js +61 -32
- package/index.d.cts +117 -0
- package/index.d.ts +117 -0
- package/package.json +1 -1
- package/rambda.js +1 -0
- package/src/modifyPath.js +30 -0
package/README.md
CHANGED
|
@@ -6714,6 +6714,135 @@ test('when index is out of bounds', () => {
|
|
|
6714
6714
|
|
|
6715
6715
|
[](#modifyItemAtIndex)
|
|
6716
6716
|
|
|
6717
|
+
### modifyPath
|
|
6718
|
+
|
|
6719
|
+
```typescript
|
|
6720
|
+
|
|
6721
|
+
modifyPath<U, T>(path: [], fn: (value: U) => T): (obj: U) => T
|
|
6722
|
+
```
|
|
6723
|
+
|
|
6724
|
+
It changes a property of object on the base of provided path and transformer function.
|
|
6725
|
+
|
|
6726
|
+
```javascript
|
|
6727
|
+
const result = R.modifyPath('a.b.c', x=> x+1, {a:{b: {c:1}}})
|
|
6728
|
+
// => {a:{b: {c:2}}}
|
|
6729
|
+
```
|
|
6730
|
+
|
|
6731
|
+
<a title="redirect to Rambda Repl site" href="https://rambda.now.sh?const%20result%20%3D%20R.modifyPath('a.b.c'%2C%20x%3D%3E%20x%2B1%2C%20%7Ba%3A%7Bb%3A%20%7Bc%3A1%7D%7D%7D)%0A%2F%2F%20%3D%3E%20%7Ba%3A%7Bb%3A%20%7Bc%3A2%7D%7D%7D">Try this <strong>R.modifyPath</strong> example in Rambda REPL</a>
|
|
6732
|
+
|
|
6733
|
+
<details>
|
|
6734
|
+
|
|
6735
|
+
<summary>All TypeScript definitions</summary>
|
|
6736
|
+
|
|
6737
|
+
```typescript
|
|
6738
|
+
modifyPath<U, T>(path: [], fn: (value: U) => T): (obj: U) => T;
|
|
6739
|
+
modifyPath<
|
|
6740
|
+
K0 extends keyof U,
|
|
6741
|
+
U,
|
|
6742
|
+
T
|
|
6743
|
+
>(path: [K0], fn: (value: U[K0]) => T): (obj: U) => DeepModify<[K0], U, T>;
|
|
6744
|
+
modifyPath<
|
|
6745
|
+
K0 extends string & keyof U,
|
|
6746
|
+
U,
|
|
6747
|
+
T
|
|
6748
|
+
>(path: `${K0}`, fn: (value: U[K0]) => T): (obj: U) => DeepModify<[K0], U, T>;
|
|
6749
|
+
modifyPath<
|
|
6750
|
+
K0 extends keyof U,
|
|
6751
|
+
K1 extends keyof U[K0],
|
|
6752
|
+
U,
|
|
6753
|
+
T
|
|
6754
|
+
>(path: [K0, K1], fn: (value: U[K0][K1]) => T): (obj: U) => DeepModify<[K0, K1], U, T>;
|
|
6755
|
+
...
|
|
6756
|
+
...
|
|
6757
|
+
```
|
|
6758
|
+
|
|
6759
|
+
</details>
|
|
6760
|
+
|
|
6761
|
+
<details>
|
|
6762
|
+
|
|
6763
|
+
<summary><strong>R.modifyPath</strong> source</summary>
|
|
6764
|
+
|
|
6765
|
+
```javascript
|
|
6766
|
+
import { createPath } from './_internals/createPath.js'
|
|
6767
|
+
import { path as pathModule } from './path.js'
|
|
6768
|
+
|
|
6769
|
+
function assoc(prop, newValue) {
|
|
6770
|
+
return obj => Object.assign({}, obj, { [prop]: newValue })
|
|
6771
|
+
}
|
|
6772
|
+
|
|
6773
|
+
function modifyPathFn(pathInput, fn, obj) {
|
|
6774
|
+
const path = createPath(pathInput)
|
|
6775
|
+
if (path.length === 1) {
|
|
6776
|
+
return {
|
|
6777
|
+
...obj,
|
|
6778
|
+
[path[0]]: fn(obj[path[0]]),
|
|
6779
|
+
}
|
|
6780
|
+
}
|
|
6781
|
+
if (pathModule(path)(obj) === undefined) {
|
|
6782
|
+
return obj
|
|
6783
|
+
}
|
|
6784
|
+
|
|
6785
|
+
const val = modifyPathFn(Array.prototype.slice.call(path, 1), fn, obj[path[0]])
|
|
6786
|
+
if (val === obj[path[0]]) {
|
|
6787
|
+
return obj
|
|
6788
|
+
}
|
|
6789
|
+
|
|
6790
|
+
return assoc(path[0], val)(obj)
|
|
6791
|
+
}
|
|
6792
|
+
|
|
6793
|
+
export function modifyPath(pathInput, fn) {
|
|
6794
|
+
return obj => modifyPathFn(pathInput, fn, obj)
|
|
6795
|
+
}
|
|
6796
|
+
```
|
|
6797
|
+
|
|
6798
|
+
</details>
|
|
6799
|
+
|
|
6800
|
+
<details>
|
|
6801
|
+
|
|
6802
|
+
<summary><strong>Tests</strong></summary>
|
|
6803
|
+
|
|
6804
|
+
```javascript
|
|
6805
|
+
import { modifyPath } from './modifyPath.js'
|
|
6806
|
+
|
|
6807
|
+
test('happy', () => {
|
|
6808
|
+
const result = modifyPath('a.b.c', x => x + 1)({ a: { b: { c: 1 } } })
|
|
6809
|
+
expect(result).toEqual({ a: { b: { c: 2 } } })
|
|
6810
|
+
})
|
|
6811
|
+
```
|
|
6812
|
+
|
|
6813
|
+
</details>
|
|
6814
|
+
|
|
6815
|
+
<details>
|
|
6816
|
+
|
|
6817
|
+
<summary><strong>TypeScript</strong> test</summary>
|
|
6818
|
+
|
|
6819
|
+
```typescript
|
|
6820
|
+
import { modifyPath, pipe } from 'rambda'
|
|
6821
|
+
|
|
6822
|
+
const obj = { a: { b: { c: 1 } } }
|
|
6823
|
+
|
|
6824
|
+
describe('R.modifyPath', () => {
|
|
6825
|
+
it('array path', () => {
|
|
6826
|
+
const result = pipe(
|
|
6827
|
+
obj,
|
|
6828
|
+
modifyPath(['a', 'b', 'c'], (x: number) => String(x)),
|
|
6829
|
+
)
|
|
6830
|
+
result.a.b.c // $ExpectType string
|
|
6831
|
+
})
|
|
6832
|
+
it('string path', () => {
|
|
6833
|
+
const result = pipe(
|
|
6834
|
+
obj,
|
|
6835
|
+
modifyPath('a.b.c', (x: number) => String(x)),
|
|
6836
|
+
)
|
|
6837
|
+
result.a.b.c // $ExpectType string
|
|
6838
|
+
})
|
|
6839
|
+
})
|
|
6840
|
+
```
|
|
6841
|
+
|
|
6842
|
+
</details>
|
|
6843
|
+
|
|
6844
|
+
[](#modifyPath)
|
|
6845
|
+
|
|
6717
6846
|
### modifyProp
|
|
6718
6847
|
|
|
6719
6848
|
```typescript
|
package/dist/rambda.cjs
CHANGED
|
@@ -1010,6 +1010,66 @@ function modifyItemAtIndex(index, replaceFn) {
|
|
|
1010
1010
|
}
|
|
1011
1011
|
}
|
|
1012
1012
|
|
|
1013
|
+
function createPath(path, delimiter = '.') {
|
|
1014
|
+
return typeof path === 'string'
|
|
1015
|
+
? path.split(delimiter).map(x => (Number.isInteger(Number(x)) ? Number(x) : x))
|
|
1016
|
+
: path
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
function path(pathInput) {
|
|
1020
|
+
return (obj) => {
|
|
1021
|
+
if (!obj) {
|
|
1022
|
+
return undefined
|
|
1023
|
+
}
|
|
1024
|
+
let willReturn = obj;
|
|
1025
|
+
let counter = 0;
|
|
1026
|
+
|
|
1027
|
+
const pathArrValue = createPath(pathInput);
|
|
1028
|
+
|
|
1029
|
+
while (counter < pathArrValue.length) {
|
|
1030
|
+
if (willReturn === null || willReturn === undefined) {
|
|
1031
|
+
return undefined
|
|
1032
|
+
}
|
|
1033
|
+
if (willReturn[pathArrValue[counter]] === null) {
|
|
1034
|
+
return undefined
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
willReturn = willReturn[pathArrValue[counter]];
|
|
1038
|
+
counter++;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
return willReturn
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
function assoc(prop, newValue) {
|
|
1046
|
+
return obj => Object.assign({}, obj, { [prop]: newValue })
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
function modifyPathFn(pathInput, fn, obj) {
|
|
1050
|
+
const path$1 = createPath(pathInput);
|
|
1051
|
+
if (path$1.length === 1) {
|
|
1052
|
+
return {
|
|
1053
|
+
...obj,
|
|
1054
|
+
[path$1[0]]: fn(obj[path$1[0]]),
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
if (path(path$1)(obj) === undefined) {
|
|
1058
|
+
return obj
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
const val = modifyPathFn(Array.prototype.slice.call(path$1, 1), fn, obj[path$1[0]]);
|
|
1062
|
+
if (val === obj[path$1[0]]) {
|
|
1063
|
+
return obj
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
return assoc(path$1[0], val)(obj)
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
function modifyPath(pathInput, fn) {
|
|
1070
|
+
return obj => modifyPathFn(pathInput, fn, obj)
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1013
1073
|
function update(index, newValue) {
|
|
1014
1074
|
return list => {
|
|
1015
1075
|
const clone = cloneList(list);
|
|
@@ -1065,12 +1125,6 @@ function objectIncludes(condition) {
|
|
|
1065
1125
|
}
|
|
1066
1126
|
}
|
|
1067
1127
|
|
|
1068
|
-
function createPath(path, delimiter = '.') {
|
|
1069
|
-
return typeof path === 'string'
|
|
1070
|
-
? path.split(delimiter).map(x => (Number.isInteger(Number(x)) ? Number(x) : x))
|
|
1071
|
-
: path
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
1128
|
function _includes(x, list) {
|
|
1075
1129
|
let index = -1;
|
|
1076
1130
|
const { length } = list;
|
|
@@ -1137,32 +1191,6 @@ function partitionObject(predicate) {
|
|
|
1137
1191
|
}
|
|
1138
1192
|
}
|
|
1139
1193
|
|
|
1140
|
-
function path(pathInput) {
|
|
1141
|
-
return (obj) => {
|
|
1142
|
-
if (!obj) {
|
|
1143
|
-
return undefined
|
|
1144
|
-
}
|
|
1145
|
-
let willReturn = obj;
|
|
1146
|
-
let counter = 0;
|
|
1147
|
-
|
|
1148
|
-
const pathArrValue = createPath(pathInput);
|
|
1149
|
-
|
|
1150
|
-
while (counter < pathArrValue.length) {
|
|
1151
|
-
if (willReturn === null || willReturn === undefined) {
|
|
1152
|
-
return undefined
|
|
1153
|
-
}
|
|
1154
|
-
if (willReturn[pathArrValue[counter]] === null) {
|
|
1155
|
-
return undefined
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
willReturn = willReturn[pathArrValue[counter]];
|
|
1159
|
-
counter++;
|
|
1160
|
-
}
|
|
1161
|
-
|
|
1162
|
-
return willReturn
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
|
|
1166
1194
|
function pathSatisfies(fn, pathInput) {
|
|
1167
1195
|
return obj => Boolean(fn(path(pathInput)(obj)))
|
|
1168
1196
|
}
|
|
@@ -1859,6 +1887,7 @@ exports.merge = merge;
|
|
|
1859
1887
|
exports.mergeTypes = mergeTypes;
|
|
1860
1888
|
exports.minBy = minBy;
|
|
1861
1889
|
exports.modifyItemAtIndex = modifyItemAtIndex;
|
|
1890
|
+
exports.modifyPath = modifyPath;
|
|
1862
1891
|
exports.modifyProp = modifyProp;
|
|
1863
1892
|
exports.none = none;
|
|
1864
1893
|
exports.objOf = objOf;
|
package/dist/rambda.js
CHANGED
|
@@ -1008,6 +1008,66 @@ function modifyItemAtIndex(index, replaceFn) {
|
|
|
1008
1008
|
}
|
|
1009
1009
|
}
|
|
1010
1010
|
|
|
1011
|
+
function createPath(path, delimiter = '.') {
|
|
1012
|
+
return typeof path === 'string'
|
|
1013
|
+
? path.split(delimiter).map(x => (Number.isInteger(Number(x)) ? Number(x) : x))
|
|
1014
|
+
: path
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
function path(pathInput) {
|
|
1018
|
+
return (obj) => {
|
|
1019
|
+
if (!obj) {
|
|
1020
|
+
return undefined
|
|
1021
|
+
}
|
|
1022
|
+
let willReturn = obj;
|
|
1023
|
+
let counter = 0;
|
|
1024
|
+
|
|
1025
|
+
const pathArrValue = createPath(pathInput);
|
|
1026
|
+
|
|
1027
|
+
while (counter < pathArrValue.length) {
|
|
1028
|
+
if (willReturn === null || willReturn === undefined) {
|
|
1029
|
+
return undefined
|
|
1030
|
+
}
|
|
1031
|
+
if (willReturn[pathArrValue[counter]] === null) {
|
|
1032
|
+
return undefined
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
willReturn = willReturn[pathArrValue[counter]];
|
|
1036
|
+
counter++;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
return willReturn
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
function assoc(prop, newValue) {
|
|
1044
|
+
return obj => Object.assign({}, obj, { [prop]: newValue })
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
function modifyPathFn(pathInput, fn, obj) {
|
|
1048
|
+
const path$1 = createPath(pathInput);
|
|
1049
|
+
if (path$1.length === 1) {
|
|
1050
|
+
return {
|
|
1051
|
+
...obj,
|
|
1052
|
+
[path$1[0]]: fn(obj[path$1[0]]),
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
if (path(path$1)(obj) === undefined) {
|
|
1056
|
+
return obj
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
const val = modifyPathFn(Array.prototype.slice.call(path$1, 1), fn, obj[path$1[0]]);
|
|
1060
|
+
if (val === obj[path$1[0]]) {
|
|
1061
|
+
return obj
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
return assoc(path$1[0], val)(obj)
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
function modifyPath(pathInput, fn) {
|
|
1068
|
+
return obj => modifyPathFn(pathInput, fn, obj)
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1011
1071
|
function update(index, newValue) {
|
|
1012
1072
|
return list => {
|
|
1013
1073
|
const clone = cloneList(list);
|
|
@@ -1063,12 +1123,6 @@ function objectIncludes(condition) {
|
|
|
1063
1123
|
}
|
|
1064
1124
|
}
|
|
1065
1125
|
|
|
1066
|
-
function createPath(path, delimiter = '.') {
|
|
1067
|
-
return typeof path === 'string'
|
|
1068
|
-
? path.split(delimiter).map(x => (Number.isInteger(Number(x)) ? Number(x) : x))
|
|
1069
|
-
: path
|
|
1070
|
-
}
|
|
1071
|
-
|
|
1072
1126
|
function _includes(x, list) {
|
|
1073
1127
|
let index = -1;
|
|
1074
1128
|
const { length } = list;
|
|
@@ -1135,32 +1189,6 @@ function partitionObject(predicate) {
|
|
|
1135
1189
|
}
|
|
1136
1190
|
}
|
|
1137
1191
|
|
|
1138
|
-
function path(pathInput) {
|
|
1139
|
-
return (obj) => {
|
|
1140
|
-
if (!obj) {
|
|
1141
|
-
return undefined
|
|
1142
|
-
}
|
|
1143
|
-
let willReturn = obj;
|
|
1144
|
-
let counter = 0;
|
|
1145
|
-
|
|
1146
|
-
const pathArrValue = createPath(pathInput);
|
|
1147
|
-
|
|
1148
|
-
while (counter < pathArrValue.length) {
|
|
1149
|
-
if (willReturn === null || willReturn === undefined) {
|
|
1150
|
-
return undefined
|
|
1151
|
-
}
|
|
1152
|
-
if (willReturn[pathArrValue[counter]] === null) {
|
|
1153
|
-
return undefined
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
willReturn = willReturn[pathArrValue[counter]];
|
|
1157
|
-
counter++;
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
return willReturn
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
|
|
1164
1192
|
function pathSatisfies(fn, pathInput) {
|
|
1165
1193
|
return obj => Boolean(fn(path(pathInput)(obj)))
|
|
1166
1194
|
}
|
|
@@ -1786,4 +1814,4 @@ function zipWith(fn, x) {
|
|
|
1786
1814
|
)
|
|
1787
1815
|
}
|
|
1788
1816
|
|
|
1789
|
-
export { _arity, _includes, _indexOf, _lastIndexOf, addProp, addPropToObjects, all, allPass, any, anyPass, append, ascend, assertType, checkObjectWithSpec, compact, complement, concat, convertToType, count, countBy, createCompareFunction, createObjectFromKeys, defaultTo, descend, drop, dropLast, dropLastWhile, dropWhile, eqBy, eqProps, equals, equalsFn, evolve, excludes, filter, filterObject, find, findIndex, findLast, findLastIndex, findNth, flatMap, flatten, flattenObject, flattenObjectHelper, groupBy, groupByFallback, head, includes, indexOf, init, innerJoin, interpolate, intersection, intersperse, join, last, lastIndexOf, map, mapAsync, mapFn, mapKeys, mapObject, mapObjectAsync, mapParallelAsync, match, maxBy, merge, mergeTypes, minBy, modifyItemAtIndex, modifyProp, none, objOf, objectIncludes, omit, partition, partitionObject, path, pathSatisfies, permutations, pick, pipe, pipeAsync, pluck, prepend, prop, propEq, propOr, propSatisfies, range, reduce, reject, rejectObject, replace, shuffle, sort, sortBy, sortByDescending, sortByFn, sortByPath, sortByPathDescending, sortObject, sortWith, split, splitEvery, symmetricDifference, tail, take, takeLast, takeLastWhile, takeWhile, tap, test, transformFlatObject, tryCatch, type, union, uniq, uniqBy, uniqWith, unless, unwind, update, when, zip, zipWith };
|
|
1817
|
+
export { _arity, _includes, _indexOf, _lastIndexOf, addProp, addPropToObjects, all, allPass, any, anyPass, append, ascend, assertType, checkObjectWithSpec, compact, complement, concat, convertToType, count, countBy, createCompareFunction, createObjectFromKeys, defaultTo, descend, drop, dropLast, dropLastWhile, dropWhile, eqBy, eqProps, equals, equalsFn, evolve, excludes, filter, filterObject, find, findIndex, findLast, findLastIndex, findNth, flatMap, flatten, flattenObject, flattenObjectHelper, groupBy, groupByFallback, head, includes, indexOf, init, innerJoin, interpolate, intersection, intersperse, join, last, lastIndexOf, map, mapAsync, mapFn, mapKeys, mapObject, mapObjectAsync, mapParallelAsync, match, maxBy, merge, mergeTypes, minBy, modifyItemAtIndex, modifyPath, modifyProp, none, objOf, objectIncludes, omit, partition, partitionObject, path, pathSatisfies, permutations, pick, pipe, pipeAsync, pluck, prepend, prop, propEq, propOr, propSatisfies, range, reduce, reject, rejectObject, replace, shuffle, sort, sortBy, sortByDescending, sortByFn, sortByPath, sortByPathDescending, sortObject, sortWith, split, splitEvery, symmetricDifference, tail, take, takeLast, takeLastWhile, takeWhile, tap, test, transformFlatObject, tryCatch, type, union, uniq, uniqBy, uniqWith, unless, unwind, update, when, zip, zipWith };
|
package/dist/rambda.umd.js
CHANGED
|
@@ -1014,6 +1014,66 @@
|
|
|
1014
1014
|
}
|
|
1015
1015
|
}
|
|
1016
1016
|
|
|
1017
|
+
function createPath(path, delimiter = '.') {
|
|
1018
|
+
return typeof path === 'string'
|
|
1019
|
+
? path.split(delimiter).map(x => (Number.isInteger(Number(x)) ? Number(x) : x))
|
|
1020
|
+
: path
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function path(pathInput) {
|
|
1024
|
+
return (obj) => {
|
|
1025
|
+
if (!obj) {
|
|
1026
|
+
return undefined
|
|
1027
|
+
}
|
|
1028
|
+
let willReturn = obj;
|
|
1029
|
+
let counter = 0;
|
|
1030
|
+
|
|
1031
|
+
const pathArrValue = createPath(pathInput);
|
|
1032
|
+
|
|
1033
|
+
while (counter < pathArrValue.length) {
|
|
1034
|
+
if (willReturn === null || willReturn === undefined) {
|
|
1035
|
+
return undefined
|
|
1036
|
+
}
|
|
1037
|
+
if (willReturn[pathArrValue[counter]] === null) {
|
|
1038
|
+
return undefined
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
willReturn = willReturn[pathArrValue[counter]];
|
|
1042
|
+
counter++;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
return willReturn
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
function assoc(prop, newValue) {
|
|
1050
|
+
return obj => Object.assign({}, obj, { [prop]: newValue })
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
function modifyPathFn(pathInput, fn, obj) {
|
|
1054
|
+
const path$1 = createPath(pathInput);
|
|
1055
|
+
if (path$1.length === 1) {
|
|
1056
|
+
return {
|
|
1057
|
+
...obj,
|
|
1058
|
+
[path$1[0]]: fn(obj[path$1[0]]),
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
if (path(path$1)(obj) === undefined) {
|
|
1062
|
+
return obj
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
const val = modifyPathFn(Array.prototype.slice.call(path$1, 1), fn, obj[path$1[0]]);
|
|
1066
|
+
if (val === obj[path$1[0]]) {
|
|
1067
|
+
return obj
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
return assoc(path$1[0], val)(obj)
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
function modifyPath(pathInput, fn) {
|
|
1074
|
+
return obj => modifyPathFn(pathInput, fn, obj)
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1017
1077
|
function update(index, newValue) {
|
|
1018
1078
|
return list => {
|
|
1019
1079
|
const clone = cloneList(list);
|
|
@@ -1069,12 +1129,6 @@
|
|
|
1069
1129
|
}
|
|
1070
1130
|
}
|
|
1071
1131
|
|
|
1072
|
-
function createPath(path, delimiter = '.') {
|
|
1073
|
-
return typeof path === 'string'
|
|
1074
|
-
? path.split(delimiter).map(x => (Number.isInteger(Number(x)) ? Number(x) : x))
|
|
1075
|
-
: path
|
|
1076
|
-
}
|
|
1077
|
-
|
|
1078
1132
|
function _includes(x, list) {
|
|
1079
1133
|
let index = -1;
|
|
1080
1134
|
const { length } = list;
|
|
@@ -1141,32 +1195,6 @@
|
|
|
1141
1195
|
}
|
|
1142
1196
|
}
|
|
1143
1197
|
|
|
1144
|
-
function path(pathInput) {
|
|
1145
|
-
return (obj) => {
|
|
1146
|
-
if (!obj) {
|
|
1147
|
-
return undefined
|
|
1148
|
-
}
|
|
1149
|
-
let willReturn = obj;
|
|
1150
|
-
let counter = 0;
|
|
1151
|
-
|
|
1152
|
-
const pathArrValue = createPath(pathInput);
|
|
1153
|
-
|
|
1154
|
-
while (counter < pathArrValue.length) {
|
|
1155
|
-
if (willReturn === null || willReturn === undefined) {
|
|
1156
|
-
return undefined
|
|
1157
|
-
}
|
|
1158
|
-
if (willReturn[pathArrValue[counter]] === null) {
|
|
1159
|
-
return undefined
|
|
1160
|
-
}
|
|
1161
|
-
|
|
1162
|
-
willReturn = willReturn[pathArrValue[counter]];
|
|
1163
|
-
counter++;
|
|
1164
|
-
}
|
|
1165
|
-
|
|
1166
|
-
return willReturn
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
1198
|
function pathSatisfies(fn, pathInput) {
|
|
1171
1199
|
return obj => Boolean(fn(path(pathInput)(obj)))
|
|
1172
1200
|
}
|
|
@@ -1863,6 +1891,7 @@
|
|
|
1863
1891
|
exports.mergeTypes = mergeTypes;
|
|
1864
1892
|
exports.minBy = minBy;
|
|
1865
1893
|
exports.modifyItemAtIndex = modifyItemAtIndex;
|
|
1894
|
+
exports.modifyPath = modifyPath;
|
|
1866
1895
|
exports.modifyProp = modifyProp;
|
|
1867
1896
|
exports.none = none;
|
|
1868
1897
|
exports.objOf = objOf;
|
package/index.d.cts
CHANGED
|
@@ -597,6 +597,123 @@ export function minBy<T>(compareFn: (input: T) => Ord, x: T): (y: T) => T;
|
|
|
597
597
|
*/
|
|
598
598
|
export function modifyItemAtIndex<T>(index: number, replaceFn: (x: T) => T): (list: T[]) => T[];
|
|
599
599
|
|
|
600
|
+
/**
|
|
601
|
+
* It changes a property of object on the base of provided path and transformer function.
|
|
602
|
+
*/
|
|
603
|
+
export function modifyPath<U, T>(path: [], fn: (value: U) => T): (obj: U) => T;
|
|
604
|
+
export function modifyPath<
|
|
605
|
+
K0 extends keyof U,
|
|
606
|
+
U,
|
|
607
|
+
T
|
|
608
|
+
>(path: [K0], fn: (value: U[K0]) => T): (obj: U) => DeepModify<[K0], U, T>;
|
|
609
|
+
export function modifyPath<
|
|
610
|
+
K0 extends string & keyof U,
|
|
611
|
+
U,
|
|
612
|
+
T
|
|
613
|
+
>(path: `${K0}`, fn: (value: U[K0]) => T): (obj: U) => DeepModify<[K0], U, T>;
|
|
614
|
+
export function modifyPath<
|
|
615
|
+
K0 extends keyof U,
|
|
616
|
+
K1 extends keyof U[K0],
|
|
617
|
+
U,
|
|
618
|
+
T
|
|
619
|
+
>(path: [K0, K1], fn: (value: U[K0][K1]) => T): (obj: U) => DeepModify<[K0, K1], U, T>;
|
|
620
|
+
export function modifyPath<
|
|
621
|
+
K0 extends string & keyof U,
|
|
622
|
+
K1 extends string & keyof U[K0],
|
|
623
|
+
U,
|
|
624
|
+
T
|
|
625
|
+
>(path: `${K0}.${K1}`, fn: (value: U[K0][K1]) => T): (obj: U) => DeepModify<[K0, K1], U, T>;
|
|
626
|
+
export function modifyPath<
|
|
627
|
+
K0 extends keyof U,
|
|
628
|
+
K1 extends keyof U[K0],
|
|
629
|
+
K2 extends keyof U[K0][K1],
|
|
630
|
+
U,
|
|
631
|
+
T
|
|
632
|
+
>(path: [K0, K1, K2], fn: (value: U[K0][K1][K2]) => T): (obj: U) => DeepModify<[K0, K1, K2], U, T>;
|
|
633
|
+
export function modifyPath<
|
|
634
|
+
K0 extends string & keyof U,
|
|
635
|
+
K1 extends string & keyof U[K0],
|
|
636
|
+
K2 extends string & keyof U[K0][K1],
|
|
637
|
+
U,
|
|
638
|
+
T
|
|
639
|
+
>(path: `${K0}.${K1}.${K2}`, fn: (value: U[K0][K1][K2]) => T): (obj: U) => DeepModify<[K0, K1, K2], U, T>;
|
|
640
|
+
export function modifyPath<
|
|
641
|
+
K0 extends keyof U,
|
|
642
|
+
K1 extends keyof U[K0],
|
|
643
|
+
K2 extends keyof U[K0][K1],
|
|
644
|
+
K3 extends keyof U[K0][K1][K2],
|
|
645
|
+
U,
|
|
646
|
+
T
|
|
647
|
+
>(path: [K0, K1, K2, K3], fn: (value: U[K0][K1][K2][K3]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3], U, T>;
|
|
648
|
+
export function modifyPath<
|
|
649
|
+
K0 extends string & keyof U,
|
|
650
|
+
K1 extends string & keyof U[K0],
|
|
651
|
+
K2 extends string & keyof U[K0][K1],
|
|
652
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
653
|
+
U,
|
|
654
|
+
T
|
|
655
|
+
>(path: `${K0}.${K1}.${K2}.${K3}`, fn: (value: U[K0][K1][K2][K3]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3], U, T>;
|
|
656
|
+
export function modifyPath<
|
|
657
|
+
K0 extends keyof U,
|
|
658
|
+
K1 extends keyof U[K0],
|
|
659
|
+
K2 extends keyof U[K0][K1],
|
|
660
|
+
K3 extends keyof U[K0][K1][K2],
|
|
661
|
+
K4 extends keyof U[K0][K1][K2][K3],
|
|
662
|
+
U,
|
|
663
|
+
T
|
|
664
|
+
>(path: [K0, K1, K2, K3, K4], fn: (value: U[K0][K1][K2][K3][K4]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4], U, T>;
|
|
665
|
+
export function modifyPath<
|
|
666
|
+
K0 extends string & keyof U,
|
|
667
|
+
K1 extends string & keyof U[K0],
|
|
668
|
+
K2 extends string & keyof U[K0][K1],
|
|
669
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
670
|
+
K4 extends string & keyof U[K0][K1][K2][K3],
|
|
671
|
+
U,
|
|
672
|
+
T
|
|
673
|
+
>(path: `${K0}.${K1}.${K2}.${K3}.${K4}`, fn: (value: U[K0][K1][K2][K3][K4]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4], U, T>;
|
|
674
|
+
export function modifyPath<
|
|
675
|
+
K0 extends keyof U,
|
|
676
|
+
K1 extends keyof U[K0],
|
|
677
|
+
K2 extends keyof U[K0][K1],
|
|
678
|
+
K3 extends keyof U[K0][K1][K2],
|
|
679
|
+
K4 extends keyof U[K0][K1][K2][K3],
|
|
680
|
+
K5 extends keyof U[K0][K1][K2][K3][K4],
|
|
681
|
+
U,
|
|
682
|
+
T
|
|
683
|
+
>(path: [K0, K1, K2, K3, K4, K5], fn: (value: U[K0][K1][K2][K3][K4][K5]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5], U, T>;
|
|
684
|
+
export function modifyPath<
|
|
685
|
+
K0 extends string & keyof U,
|
|
686
|
+
K1 extends string & keyof U[K0],
|
|
687
|
+
K2 extends string & keyof U[K0][K1],
|
|
688
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
689
|
+
K4 extends string & keyof U[K0][K1][K2][K3],
|
|
690
|
+
K5 extends string & keyof U[K0][K1][K2][K3][K4],
|
|
691
|
+
U,
|
|
692
|
+
T
|
|
693
|
+
>(path: `${K0}.${K1}.${K2}.${K3}.${K4}.${K5}`, fn: (value: U[K0][K1][K2][K3][K4][K5]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5], U, T>;
|
|
694
|
+
export function modifyPath<
|
|
695
|
+
K0 extends keyof U,
|
|
696
|
+
K1 extends keyof U[K0],
|
|
697
|
+
K2 extends keyof U[K0][K1],
|
|
698
|
+
K3 extends keyof U[K0][K1][K2],
|
|
699
|
+
K4 extends keyof U[K0][K1][K2][K3],
|
|
700
|
+
K5 extends keyof U[K0][K1][K2][K3][K4],
|
|
701
|
+
K6 extends keyof U[K0][K1][K2][K3][K4][K5],
|
|
702
|
+
U,
|
|
703
|
+
T
|
|
704
|
+
>(path: [K0, K1, K2, K3, K4, K5, K6], fn: (value: U[K0][K1][K2][K3][K4][K5][K6]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5, K6], U, T>;
|
|
705
|
+
export function modifyPath<
|
|
706
|
+
K0 extends string & keyof U,
|
|
707
|
+
K1 extends string & keyof U[K0],
|
|
708
|
+
K2 extends string & keyof U[K0][K1],
|
|
709
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
710
|
+
K4 extends string & keyof U[K0][K1][K2][K3],
|
|
711
|
+
K5 extends string & keyof U[K0][K1][K2][K3][K4],
|
|
712
|
+
K6 extends string & keyof U[K0][K1][K2][K3][K4][K5],
|
|
713
|
+
U,
|
|
714
|
+
T
|
|
715
|
+
>(path: `${K0}.${K1}.${K2}.${K3}.${K4}.${K5}.${K6}`, fn: (value: U[K0][K1][K2][K3][K4][K5][K6]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5, K6], U, T>;
|
|
716
|
+
|
|
600
717
|
/**
|
|
601
718
|
* It changes a property with the result of transformer function.
|
|
602
719
|
*/
|
package/index.d.ts
CHANGED
|
@@ -597,6 +597,123 @@ export function minBy<T>(compareFn: (input: T) => Ord, x: T): (y: T) => T;
|
|
|
597
597
|
*/
|
|
598
598
|
export function modifyItemAtIndex<T>(index: number, replaceFn: (x: T) => T): (list: T[]) => T[];
|
|
599
599
|
|
|
600
|
+
/**
|
|
601
|
+
* It changes a property of object on the base of provided path and transformer function.
|
|
602
|
+
*/
|
|
603
|
+
export function modifyPath<U, T>(path: [], fn: (value: U) => T): (obj: U) => T;
|
|
604
|
+
export function modifyPath<
|
|
605
|
+
K0 extends keyof U,
|
|
606
|
+
U,
|
|
607
|
+
T
|
|
608
|
+
>(path: [K0], fn: (value: U[K0]) => T): (obj: U) => DeepModify<[K0], U, T>;
|
|
609
|
+
export function modifyPath<
|
|
610
|
+
K0 extends string & keyof U,
|
|
611
|
+
U,
|
|
612
|
+
T
|
|
613
|
+
>(path: `${K0}`, fn: (value: U[K0]) => T): (obj: U) => DeepModify<[K0], U, T>;
|
|
614
|
+
export function modifyPath<
|
|
615
|
+
K0 extends keyof U,
|
|
616
|
+
K1 extends keyof U[K0],
|
|
617
|
+
U,
|
|
618
|
+
T
|
|
619
|
+
>(path: [K0, K1], fn: (value: U[K0][K1]) => T): (obj: U) => DeepModify<[K0, K1], U, T>;
|
|
620
|
+
export function modifyPath<
|
|
621
|
+
K0 extends string & keyof U,
|
|
622
|
+
K1 extends string & keyof U[K0],
|
|
623
|
+
U,
|
|
624
|
+
T
|
|
625
|
+
>(path: `${K0}.${K1}`, fn: (value: U[K0][K1]) => T): (obj: U) => DeepModify<[K0, K1], U, T>;
|
|
626
|
+
export function modifyPath<
|
|
627
|
+
K0 extends keyof U,
|
|
628
|
+
K1 extends keyof U[K0],
|
|
629
|
+
K2 extends keyof U[K0][K1],
|
|
630
|
+
U,
|
|
631
|
+
T
|
|
632
|
+
>(path: [K0, K1, K2], fn: (value: U[K0][K1][K2]) => T): (obj: U) => DeepModify<[K0, K1, K2], U, T>;
|
|
633
|
+
export function modifyPath<
|
|
634
|
+
K0 extends string & keyof U,
|
|
635
|
+
K1 extends string & keyof U[K0],
|
|
636
|
+
K2 extends string & keyof U[K0][K1],
|
|
637
|
+
U,
|
|
638
|
+
T
|
|
639
|
+
>(path: `${K0}.${K1}.${K2}`, fn: (value: U[K0][K1][K2]) => T): (obj: U) => DeepModify<[K0, K1, K2], U, T>;
|
|
640
|
+
export function modifyPath<
|
|
641
|
+
K0 extends keyof U,
|
|
642
|
+
K1 extends keyof U[K0],
|
|
643
|
+
K2 extends keyof U[K0][K1],
|
|
644
|
+
K3 extends keyof U[K0][K1][K2],
|
|
645
|
+
U,
|
|
646
|
+
T
|
|
647
|
+
>(path: [K0, K1, K2, K3], fn: (value: U[K0][K1][K2][K3]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3], U, T>;
|
|
648
|
+
export function modifyPath<
|
|
649
|
+
K0 extends string & keyof U,
|
|
650
|
+
K1 extends string & keyof U[K0],
|
|
651
|
+
K2 extends string & keyof U[K0][K1],
|
|
652
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
653
|
+
U,
|
|
654
|
+
T
|
|
655
|
+
>(path: `${K0}.${K1}.${K2}.${K3}`, fn: (value: U[K0][K1][K2][K3]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3], U, T>;
|
|
656
|
+
export function modifyPath<
|
|
657
|
+
K0 extends keyof U,
|
|
658
|
+
K1 extends keyof U[K0],
|
|
659
|
+
K2 extends keyof U[K0][K1],
|
|
660
|
+
K3 extends keyof U[K0][K1][K2],
|
|
661
|
+
K4 extends keyof U[K0][K1][K2][K3],
|
|
662
|
+
U,
|
|
663
|
+
T
|
|
664
|
+
>(path: [K0, K1, K2, K3, K4], fn: (value: U[K0][K1][K2][K3][K4]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4], U, T>;
|
|
665
|
+
export function modifyPath<
|
|
666
|
+
K0 extends string & keyof U,
|
|
667
|
+
K1 extends string & keyof U[K0],
|
|
668
|
+
K2 extends string & keyof U[K0][K1],
|
|
669
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
670
|
+
K4 extends string & keyof U[K0][K1][K2][K3],
|
|
671
|
+
U,
|
|
672
|
+
T
|
|
673
|
+
>(path: `${K0}.${K1}.${K2}.${K3}.${K4}`, fn: (value: U[K0][K1][K2][K3][K4]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4], U, T>;
|
|
674
|
+
export function modifyPath<
|
|
675
|
+
K0 extends keyof U,
|
|
676
|
+
K1 extends keyof U[K0],
|
|
677
|
+
K2 extends keyof U[K0][K1],
|
|
678
|
+
K3 extends keyof U[K0][K1][K2],
|
|
679
|
+
K4 extends keyof U[K0][K1][K2][K3],
|
|
680
|
+
K5 extends keyof U[K0][K1][K2][K3][K4],
|
|
681
|
+
U,
|
|
682
|
+
T
|
|
683
|
+
>(path: [K0, K1, K2, K3, K4, K5], fn: (value: U[K0][K1][K2][K3][K4][K5]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5], U, T>;
|
|
684
|
+
export function modifyPath<
|
|
685
|
+
K0 extends string & keyof U,
|
|
686
|
+
K1 extends string & keyof U[K0],
|
|
687
|
+
K2 extends string & keyof U[K0][K1],
|
|
688
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
689
|
+
K4 extends string & keyof U[K0][K1][K2][K3],
|
|
690
|
+
K5 extends string & keyof U[K0][K1][K2][K3][K4],
|
|
691
|
+
U,
|
|
692
|
+
T
|
|
693
|
+
>(path: `${K0}.${K1}.${K2}.${K3}.${K4}.${K5}`, fn: (value: U[K0][K1][K2][K3][K4][K5]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5], U, T>;
|
|
694
|
+
export function modifyPath<
|
|
695
|
+
K0 extends keyof U,
|
|
696
|
+
K1 extends keyof U[K0],
|
|
697
|
+
K2 extends keyof U[K0][K1],
|
|
698
|
+
K3 extends keyof U[K0][K1][K2],
|
|
699
|
+
K4 extends keyof U[K0][K1][K2][K3],
|
|
700
|
+
K5 extends keyof U[K0][K1][K2][K3][K4],
|
|
701
|
+
K6 extends keyof U[K0][K1][K2][K3][K4][K5],
|
|
702
|
+
U,
|
|
703
|
+
T
|
|
704
|
+
>(path: [K0, K1, K2, K3, K4, K5, K6], fn: (value: U[K0][K1][K2][K3][K4][K5][K6]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5, K6], U, T>;
|
|
705
|
+
export function modifyPath<
|
|
706
|
+
K0 extends string & keyof U,
|
|
707
|
+
K1 extends string & keyof U[K0],
|
|
708
|
+
K2 extends string & keyof U[K0][K1],
|
|
709
|
+
K3 extends string & keyof U[K0][K1][K2],
|
|
710
|
+
K4 extends string & keyof U[K0][K1][K2][K3],
|
|
711
|
+
K5 extends string & keyof U[K0][K1][K2][K3][K4],
|
|
712
|
+
K6 extends string & keyof U[K0][K1][K2][K3][K4][K5],
|
|
713
|
+
U,
|
|
714
|
+
T
|
|
715
|
+
>(path: `${K0}.${K1}.${K2}.${K3}.${K4}.${K5}.${K6}`, fn: (value: U[K0][K1][K2][K3][K4][K5][K6]) => T): (obj: U) => DeepModify<[K0, K1, K2, K3, K4, K5, K6], U, T>;
|
|
716
|
+
|
|
600
717
|
/**
|
|
601
718
|
* It changes a property with the result of transformer function.
|
|
602
719
|
*/
|
package/package.json
CHANGED
package/rambda.js
CHANGED
|
@@ -61,6 +61,7 @@ export * from './src/merge.js'
|
|
|
61
61
|
export * from './src/mergeTypes.js'
|
|
62
62
|
export * from './src/minBy.js'
|
|
63
63
|
export * from './src/modifyItemAtIndex.js'
|
|
64
|
+
export * from './src/modifyPath.js'
|
|
64
65
|
export * from './src/modifyProp.js'
|
|
65
66
|
export * from './src/none.js'
|
|
66
67
|
export * from './src/objOf.js'
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createPath } from './_internals/createPath.js'
|
|
2
|
+
import { path as pathModule } from './path.js'
|
|
3
|
+
|
|
4
|
+
function assoc(prop, newValue) {
|
|
5
|
+
return obj => Object.assign({}, obj, { [prop]: newValue })
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function modifyPathFn(pathInput, fn, obj) {
|
|
9
|
+
const path = createPath(pathInput)
|
|
10
|
+
if (path.length === 1) {
|
|
11
|
+
return {
|
|
12
|
+
...obj,
|
|
13
|
+
[path[0]]: fn(obj[path[0]]),
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (pathModule(path)(obj) === undefined) {
|
|
17
|
+
return obj
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const val = modifyPathFn(Array.prototype.slice.call(path, 1), fn, obj[path[0]])
|
|
21
|
+
if (val === obj[path[0]]) {
|
|
22
|
+
return obj
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return assoc(path[0], val)(obj)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function modifyPath(pathInput, fn) {
|
|
29
|
+
return obj => modifyPathFn(pathInput, fn, obj)
|
|
30
|
+
}
|