Skip to content

Commit

Permalink
ramda#3340: support empty path in modifyPath (ramda#3376)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanGalilea authored Apr 25, 2023
1 parent de5803d commit 241f811
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/modifyPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ import _modify from './internal/_modify.js';
* R.modifyPath(['addresses', 0, 'zipCode'], R.reverse, person); //=> {name: 'James', addresses: [{ zipCode: '61209' }]}
*/
var modifyPath = _curry3(function modifyPath(path, fn, object) {
if ((!_isObject(object) && !_isArray(object)) || path.length === 0) {
if (!_isObject(object) && !_isArray(object)) {
return object;
}

if (path.length === 0) {
return fn(object);
}

var idx = path[0];
if (!_has(idx, object)) {
return object;
Expand Down
7 changes: 7 additions & 0 deletions test/modifyPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ describe('modifyPath', function() {
assert.strictEqual(object, created);
});

it('modifies the original object if path is empty array', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var expected = {a: 'Tomato', b: 'Potato', e: { f: 'g', h: [1, 2, 3] }};
var created = R.modifyPath([], R.assoc('b', 'Potato'), object);
eq(created, expected);
});

it('is not destructive', function() {
var object = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
var expected = {a: 'Tomato', b: { c: { d: [100, 101, 102] } }, e: { f: 'g', h: [1, 2, 3] }};
Expand Down

0 comments on commit 241f811

Please sign in to comment.