From 9e5a0fdf1264595269bba13da94c5d4d67115217 Mon Sep 17 00:00:00 2001 From: unadlib Date: Sun, 19 May 2024 14:14:12 +0800 Subject: [PATCH] docs(api): add api doc --- docs/README.md | 77 ++++++++++++++++++++++++++++++++++++++++ docs/functions/mutate.md | 29 +++++++++++++++ docs/globals.md | 9 +++++ 3 files changed, 115 insertions(+) create mode 100644 docs/README.md create mode 100644 docs/functions/mutate.md create mode 100644 docs/globals.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..8024183 --- /dev/null +++ b/docs/README.md @@ -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). diff --git a/docs/functions/mutate.md b/docs/functions/mutate.md new file mode 100644 index 0000000..86c3bfb --- /dev/null +++ b/docs/functions/mutate.md @@ -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) diff --git a/docs/globals.md b/docs/globals.md new file mode 100644 index 0000000..54f5485 --- /dev/null +++ b/docs/globals.md @@ -0,0 +1,9 @@ +[**mutability**](README.md) • **Docs** + +*** + +# mutability + +## Functions + +- [mutate](functions/mutate.md)