immutable-js-oss/immutable-js

mergeIn inconsistency in merging maps with maps and lists

Closed this issue · 1 comments

From @GolubevS on Sat, 09 Nov 2019 09:24:41 GMT

What happened

mergeIn inconsistency in merging maps with maps and lists (keys in keyPath do not exist)

How to reproduce

Merging map with map:

import { Map } from 'immutable';

const map = Map({ a: { b: { c: 10 } } });
const newMap = map.mergeIn(['a', 'e'], {c: 20, d: 30});

console.log(newMap.toJS());

Result as expected:

{ a: { b: { c: 10 }, e: {c: 20, d: 30}}}

Merging map with list:

import { Map } from 'immutable';

const map = Map({ a: { b: [1, 2] } });
const newMap = map.mergeIn(['a', 'e'], [2, 3]);

console.log(newMap.toJS());

Expected:

{ a: { b: [1, 2], e: [2, 3] } }

Result:

Uncaught TypeError: Expected [K, V] tuple: 2

Update

Issue more general: list cannot be merged with list if key not exists:

import { List } from 'immutable';

const myList = List.of(1, 2, [3, [4, 5]]);
const newList = myList.mergeIn([2, 2], [5, 6]);

console.log(newList.toJS());

Expected:

[1, 2, [3, [4, 5], [5, 6]]]

Result:

Uncaught TypeError: Expected [K, V] tuple: 5

Copied from original issue: immutable-js#1749

Working as intended per comment in original repo.