1.1.2 • Published 10 months ago

mutability v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

mutability

Node CI npm version license

A JavaScript library for transactional mutable updates based on Mutative.

Motivation

When we want to perform transactional updates on a mutable object, if an error is caught during the update process, the mutable update will not be applied at all. Otherwise, the mutable update will be applied to the mutable object. Therefore, we need a tool to implement this functionality.

Installation

yarn add mutative mutability

or with npm

npm install mutative mutability

Usage

import { mutate } from 'mutability';

test('base - mutate', () => {
  const baseState = {
    a: {
      c: 1,
    },
  };
  const { patches, inversePatches } = mutate(baseState, (draft) => {
    draft.a.c = 2;
  });
  expect(baseState).toEqual({ a: { c: 2 } });
  expect({ patches, inversePatches }).toEqual({
    patches: [
      {
        op: 'replace',
        path: ['a', 'c'],
        value: 2,
      },
    ],
    inversePatches: [
      {
        op: 'replace',
        path: ['a', 'c'],
        value: 1,
      },
    ],
  });
  apply(baseState, inversePatches);
  expect(baseState).toEqual({ a: { c: 1 } });
});

test('base - mutate with error', () => {
  const baseState = {
    a: {
      c: 1,
    },
    b: {
      c: 1,
    },
  };
  try {
    mutate(baseState, (draft) => {
      draft.a.c = 2;
      throw new Error('error');
      draft.b.c = 2;
    });
  } catch (e) {
    //
  }
  expect(baseState).toEqual({
    a: {
      c: 1,
    },
    b: {
      c: 1,
    },
  });
});

APIs

mutate()

Mutate the mutable object, and return the patches and inverse patches.

apply()

Apply the mutable update with patches.

License

Mutability is MIT licensed.

1.1.1

10 months ago

1.1.2

10 months ago

1.0.2

12 months ago

1.1.0

11 months ago

1.0.1

1 year ago

1.0.3

12 months ago

1.0.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.3.2

1 year ago

0.2.3

1 year ago

0.3.1

1 year ago

0.2.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago