Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/immutable-deep-merge.ts
Views: 687
/*1The immutable.js mergeDeep in immutable 4.1 is badly broken, so we have to implement our own.2It used to be fine in 3.8.34Example in immutable 3.8.2:56```ts7a = require('immutable').fromJS({a:['x','y']})8b = require('immutable').fromJS({a:['x','y']})9> JSON.stringify(a.mergeDeep(b))10'{"a":["x","y"]}'1112// Same in immutable 4.1 has totally different (and very wrong) output:1314> JSON.stringify(a.mergeDeep(b))15'{"a":["x","y","x","y"]}'16```171819It's a documented change at https://immutable-js.com/docs/latest@main/mergeDeep2021"Note: Indexed and set-like collections are merged using concat/union and therefore do not recurse." ARGH!2223Of course we want the result of the above merge to be {"a":["x","y"]}, which is what24lodash does (since in Javascript a list is like a map from integers, so the correct semantics25are clear).2627Semantics of merge are discussed here: https://stackoverflow.com/questions/19965844/lodash-difference-between-extend-assign-and-merge28*/2930import { merge } from "lodash";31import { fromJS, Map } from "immutable";3233// This is obviously not a great approach in general, converting back and forth. However, we only34// use this for fairly small data in exactly one place, and we can do something the same but more35// efficient later.3637export default function mergeDeep(38a: Map<string|number|any, any>,39b: Map<string|number|any, any>40): Map<string|number|any, any> {41const c = merge(a.toJS(), b.toJS());42return fromJS(c);43}444546