Skip to content

deeplay-io/diff-maps

Repository files navigation

diff-maps npm version

Calculate diff between two Maps

Installation

npm install diff-maps

Usage

Calculate diff:

import {diffMaps} from 'diff-maps';

expect(
  diffMaps(
    new Map([
      ['1', 0],
      ['2', 0],
    ]),
    new Map([
      ['2', 1],
      ['3', 1],
    ]),
  ),
).toEqual([
  {type: 'remove', key: '1', prevValue: 0},
  {type: 'change', key: '2', value: 1, prevValue: 0},
  {type: 'add', key: '3', value: 1},
]);

Use custom equality function (default is Object.is):

import {diffMaps} from 'diff-maps';
import {isEqual} from 'lodash';

expect(
  diffMaps(
    new Map([
      ['1', {data: 0}],
      ['2', {data: 0}],
    ]),
    new Map([
      ['1', {data: 0}],
      ['2', {data: 1}],
    ]),
    isEqual,
  ),
).toEqual([
  {
    type: 'change',
    key: '2',
    value: {data: 1},
    prevValue: {data: 0},
  },
]);

Apply diff:

import {applyDiff} from 'diff-maps';

const state = new Map([
  ['1', 0],
  ['2', 0],
]);

// mutates the map
applyDiff(state, [
  {type: 'remove', key: '1', prevValue: 0},
  {type: 'change', key: '2', value: 1, prevValue: 0},
  {type: 'add', key: '3', value: 1},
]);

expect(state).toEqual(
  new Map([
    ['2', 1],
    ['3', 1],
  ]),
);

Use with Immutable.js:

import {Map} from 'immutable';

// calculate diff

expect(
  diffMaps(
    Map({
      1: 0,
      2: 0,
    }),
    Map({
      2: 1,
      3: 1,
    }),
  ),
).toEqual([
  {type: 'remove', key: '1', prevValue: 0},
  {type: 'change', key: '2', value: 1, prevValue: 0},
  {type: 'add', key: '3', value: 1},
]);

// apply diff

const state = Map({
  1: 0,
  2: 0,
});

const nextState = state.withMutations(mutableState => {
  applyDiff(mutableState, [
    {type: 'remove', key: '1', prevValue: 0},
    {type: 'change', key: '2', value: 1, prevValue: 0},
    {type: 'add', key: '3', value: 1},
  ]);
});

expect(new Map(nextState)).toEqual(
  new Map([
    ['2', 1],
    ['3', 1],
  ]),
);