Skip to content

Commit

Permalink
feat(mutate): support returning patches
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Aug 21, 2024
1 parent 269cc7c commit b54b01b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ test('base - mutate with error', () => {
});
```

## APIs

### `mutate()`
Mutate the mutable object

### `apply()`
Apply the mutable update with patches

## License

Mutability is [MIT licensed](https://github.com/mutativejs/mutability/blob/main/LICENSE).
3 changes: 3 additions & 0 deletions src/apply.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { DraftType, Operation, Patches } from './interface';
import { deepClone, get, getType, unescapePath } from './utils';

/**
* Apply patches to the state.
*/
export function apply(state: any, patches: Patches) {
let i;
for (i = patches.length - 1; i >= 0; i -= 1) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { create } from 'mutative';
import { apply } from './apply';

export { apply } from './apply';

/**
* Transactional updates to the base state with the recipe.
*/
Expand All @@ -9,4 +11,5 @@ export const mutate = <T>(baseState: T, recipe: (state: T) => void) => {
enablePatches: true,
});
apply(baseState, patches);
return patches;
};

0 comments on commit b54b01b

Please sign in to comment.