-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[**mutability**](README.md) • **Docs** | ||
|
||
*** | ||
|
||
# mutability | ||
|
||
## Functions | ||
|
||
- [mutate](functions/mutate.md) |