Skip to content

Commit

Permalink
docs(api): add api doc
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed May 19, 2024
1 parent 1da68cc commit 9e5a0fd
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
77 changes: 77 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
**mutability**[**Docs**](globals.md)

***

# mutability

![Node CI](https://github.com/mutativejs/mutability/workflows/Node%20CI/badge.svg)
[![npm version](https://badge.fury.io/js/mutability.svg)](http://badge.fury.io/js/mutability)
![license](https://img.shields.io/npm/l/mutability)

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

```sh
yarn add mutative mutability
```

or with npm

```sh
npm install mutative mutability
```

## Usage

```ts
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](https://github.com/mutativejs/mutability/blob/main/LICENSE).
29 changes: 29 additions & 0 deletions docs/functions/mutate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[**mutability**](../README.md)**Docs**

***

[mutability](../globals.md) / mutate

# Function: mutate()

> **mutate**\<`T`\>(`baseState`, `recipe`): `void`
Transactional updates to the base state with the recipe.

## Type parameters

**T**

## Parameters

**baseState**: `T`

**recipe**

## Returns

`void`

## Source

[index.ts:7](https://github.com/mutativejs/mutability/blob/7716adccba23dd12347de1acf2bcc25ee5a2c9a6/src/index.ts#L7)
9 changes: 9 additions & 0 deletions docs/globals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[**mutability**](README.md)**Docs**

***

# mutability

## Functions

- [mutate](functions/mutate.md)

0 comments on commit 9e5a0fd

Please sign in to comment.