Skip to content

Commit

Permalink
refactor(draft): export isDraftable()
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Jan 27, 2023
1 parent 797cc8b commit 1fbcf39
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ In this basic example, the changes to the draft are 'mutative' within the draft
> Enable autoFreeze, and return frozen state.
- mark - `(target) => ('mutable'|'immutable'|function)`
> Set a mark to determine if the object is mutable or if an instance is an immutable, and it can also return a shallow copy function(AutoFreeze and Patches should both be disabled).
> Set a mark to determine if the value is mutable or if an instance is an immutable, and it can also return a shallow copy function(AutoFreeze and Patches should both be disabled).
#### `create()` - Currying

Expand Down Expand Up @@ -283,6 +283,21 @@ const state = create(baseState, (draft) => {
expect(isDraft(draft.list)).toBeTruthy();
});
```
### `isDraftable()`

Check if a value is draftable

```ts
const baseState = {
date: new Date(),
list: [{ text: 'todo' }],
};

expect(isDraftable(baseState.date)).toBeFalsy();
expect(isDraftable(baseState.list)).toBeTruthy();
```

> You can set a mark to determine if the value is draftable, and the mark function should be the same as passing in `create()` mark option.
### `safeReturn()`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mutative",
"version": "0.3.1",
"version": "0.3.2",
"description": "A JavaScript library for efficient creation of immutable state",
"main": "dist/index.js",
"module": "dist/mutative.esm.js",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { current } from './current';
export { unsafe } from './unsafe';
export { safeReturn } from './safeReturn';
export { isDraft } from './utils/draft';
export { isDraftable } from './utils/draft';

export { castDraft, castImmutable } from './utils/cast';
export type { Immutable, Draft, Patches, Patch, Options } from './interface';
7 changes: 5 additions & 2 deletions src/utils/draft.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DraftType, Options, ProxyDraft } from '../interface';
import { DraftType, Mark, ProxyDraft } from '../interface';
import { dataTypes, PROXY_DRAFT } from '../constant';

export function latest<T = any>(proxyDraft: ProxyDraft): T {
Expand All @@ -22,7 +22,10 @@ export function getValue<T extends object>(value: T): T {
return proxyDraft ? proxyDraft.copy ?? proxyDraft.original : value;
}

export function isDraftable(value: any, options?: Options<any, any>) {
/**
* Check if a value is draftable
*/
export function isDraftable(value: any, options?: { mark?: Mark<any, any> }) {
if (!value || typeof value !== 'object') return false;
let markResult: any;
return (
Expand Down
40 changes: 39 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { create, original, current, apply } from '../src';
import { create, original, current, apply, isDraftable, isDraft } from '../src';
import { PROXY_DRAFT } from '../src/constant';

test('check object type', () => {
Expand Down Expand Up @@ -2129,3 +2129,41 @@ test('check Primitive type with returning, patches, freeze and async', async ()
]);
}
});

test('base isDraft()', () => {
const baseState = {
date: new Date(),
list: [{ text: 'todo' }],
};

const state = create(baseState, (draft) => {
expect(isDraft(draft.date)).toBeFalsy();
expect(isDraft(draft.list)).toBeTruthy();
});
})

test('base isDraftable()', () => {
const baseState = {
date: new Date(),
list: [{ text: 'todo' }],
};

expect(isDraftable(baseState.date)).toBeFalsy();
expect(isDraftable(baseState.list)).toBeTruthy();
});

test('base isDraftable() with option', () => {
const baseState = {
date: new Date(),
list: [{ text: 'todo' }],
};

expect(
isDraftable(baseState.date, {
mark: (target, { immutable }) => {
if (target instanceof Date) return immutable;
},
})
).toBeTruthy();
expect(isDraftable(baseState.list)).toBeTruthy();
});

0 comments on commit 1fbcf39

Please sign in to comment.