Skip to content

Commit

Permalink
Add support for updating nested objects inside Maps (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
adelgado authored and kolodny committed Apr 17, 2018
1 parent e5e6252 commit 2981461
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ function newContext() {
nextObject = object;
}
} else {
var nextValueForKey = update(object[key], spec[key]);
var nextValueForKey =
type(object) === 'Map'
? update(object.get(key), spec[key])
: update(object[key], spec[key]);
if (!update.isEquals(nextValueForKey, nextObject[key]) || typeof nextValueForKey === 'undefined' && !hasOwnProperty.call(object, key)) {
if (nextObject === object) {
nextObject = copy(object);
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,23 @@ describe('update', function() {
);
});

it('supports nested objects inside Maps', function () {
var state = new Map([
['mapKey', { banana: 'yellow', apple: ['red'], blueberry: 'purple' }]
]);

var updatedState = update(state, {
['mapKey']: { apple: { $set: ['green', 'red'] } }
});

expect(updatedState).toEqual(
new Map([
[
'mapKey',
{ banana: 'yellow', apple: ['green', 'red'], blueberry: 'purple' }
]
])
);
})

});

0 comments on commit 2981461

Please sign in to comment.