mutability
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

mutability

Node CI npm version license

A JavaScript library for transactional mutable updates.

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,
    },
  };
  mutate(baseState, (draft) => {
    draft.a.c = 2;
  });
  expect(baseState).toEqual({ a: { c: 2 } });
});

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,
    },
  });
});

License

Mutability is MIT licensed.

Package Sidebar

Install

npm i mutability

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

98.2 kB

Total Files

21

Last publish

Collaborators

  • unadlib