Skip to content

Commit

Permalink
docs(readme): update
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Jun 16, 2024
1 parent 6f77e2f commit 038b60f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,19 @@ expect(prevState).toEqual(baseState);

Get the current value from a draft.

```ts
const baseState = {
foo: 'bar',
list: [{ text: 'todo' }],
};
- For any draft where a child node has been modified, the state obtained by executing current() each time will be a new reference object.
- For a draft where no child nodes have been modified, executing current() will always return the original state.

const state = create(baseState, (draft) => {
draft.foo = 'foobar';
draft.list.push({ text: 'learning' });
expect(current(draft.list)).toEqual([{ text: 'todo' }, { text: 'learning' }]);
> It is recommended to minimize the number of times current() is executed when performing read-only operations, ideally executing it only once.
```ts
const state = create({ a: { b: { c: 1 } }, d: { f: 1 } }, (draft) => {
draft.a.b.c = 2;
expect(current(draft.a)).toEqual({ b: { c: 2 } });
// The node `a` has been modified.
expect(current(draft.a) === current(draft.a)).toBeFalsy();
// The node `d` has not been modified.
expect(current(draft.d) === current(draft.d)).toBeTruthy();
});
```

Expand Down
21 changes: 20 additions & 1 deletion test/current.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
import { create, current, isDraft } from '../src';

describe('current', () => {
test('base', () => {
const state = create({ a: { b: { c: 1 } }, d: { f: 1 } }, (draft) => {
draft.a.b.c = 2;
expect(current(draft.a)).toEqual({ b: { c: 2 } });
// The node `a` has been modified.
expect(current(draft.a) === current(draft.a)).toBeFalsy();
// The node `d` has not been modified.
expect(current(draft.d) === current(draft.d)).toBeTruthy();
});
});
test('should return the current value', () => {
interface Item {
foo: string;
Expand Down Expand Up @@ -141,6 +151,9 @@ test('nested draft', () => {
d: number;
};
}>;
j: {
k: number;
};
};
const baseState: State = {
c: {
Expand All @@ -167,6 +180,9 @@ test('nested draft', () => {
},
},
]),
j: {
k: 1,
},
};
create(baseState, (draft) => {
draft.c.a = 2;
Expand All @@ -191,7 +207,6 @@ test('nested draft', () => {
},
},
};

const d = current(draft.d);
const map = current(draft.map);
const set = current(draft.set);
Expand All @@ -202,5 +217,9 @@ test('nested draft', () => {
const f = set.values().next().value.d.d.f.f.f;
expect(f).toEqual({ a: 2 });
expect(isDraft(f)).toBeFalsy();
// the node `d` has been changed
expect(current(draft.d) === current(draft.d)).toBeFalsy();
// the node `j` has not been changed
expect(current(draft.j) === current(draft.j)).toBeTruthy();
});
});

0 comments on commit 038b60f

Please sign in to comment.