Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __tests__/updateIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ describe('updateIn', () => {
});
});

it('deep set with numeric key 1', () => {
const m = fromJS({ a: { b: 1 } });
expect(m.updateIn(['a', 'c', 0], () => 20).toJS()).toEqual({
a: { b: 1, c: [20] },
});
});

it('deep set with numeric key 2', () => {
const m = fromJS({});
expect(m.updateIn(['a', 0], () => 20).toJS()).toEqual({
a: [20],
});
});

it('deep push', () => {
const m = fromJS({ a: { b: [1, 2, 3] } });
expect(m.updateIn(['a', 'b'], list => list.push(4)).toJS()).toEqual({
Expand Down
21 changes: 15 additions & 6 deletions src/functional/updateIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import isDataStructure from '../utils/isDataStructure';
import quoteString from '../utils/quoteString';
import { NOT_SET } from '../TrieUtils';
import { emptyMap } from '../Map';
import { emptyList } from '../List';
import { get } from './get';
import { remove } from './remove';
import { set } from './set';
Expand Down Expand Up @@ -66,10 +67,18 @@ function updateInDeeply(
return nextUpdated === nextExisting
? existing
: nextUpdated === NOT_SET
? remove(existing, key)
: set(
wasNotSet ? (inImmutable ? emptyMap() : {}) : existing,
key,
nextUpdated
);
? remove(existing, key)
: set(
wasNotSet
? inImmutable
? typeof key === 'number'
? emptyList()
: emptyMap()
: typeof key === 'number'
? []
: {}
: existing,
key,
nextUpdated
);
}