Skip to content

Commit 1fbcf39

Browse files
committed
refactor(draft): export isDraftable()
1 parent 797cc8b commit 1fbcf39

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ In this basic example, the changes to the draft are 'mutative' within the draft
157157
> Enable autoFreeze, and return frozen state.
158158
159159
- mark - `(target) => ('mutable'|'immutable'|function)`
160-
> 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).
160+
> 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).
161161
162162
#### `create()` - Currying
163163

@@ -283,6 +283,21 @@ const state = create(baseState, (draft) => {
283283
expect(isDraft(draft.list)).toBeTruthy();
284284
});
285285
```
286+
### `isDraftable()`
287+
288+
Check if a value is draftable
289+
290+
```ts
291+
const baseState = {
292+
date: new Date(),
293+
list: [{ text: 'todo' }],
294+
};
295+
296+
expect(isDraftable(baseState.date)).toBeFalsy();
297+
expect(isDraftable(baseState.list)).toBeTruthy();
298+
```
299+
300+
> 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.
286301
287302
### `safeReturn()`
288303

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mutative",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "A JavaScript library for efficient creation of immutable state",
55
"main": "dist/index.js",
66
"module": "dist/mutative.esm.js",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export { current } from './current';
55
export { unsafe } from './unsafe';
66
export { safeReturn } from './safeReturn';
77
export { isDraft } from './utils/draft';
8+
export { isDraftable } from './utils/draft';
89

910
export { castDraft, castImmutable } from './utils/cast';
1011
export type { Immutable, Draft, Patches, Patch, Options } from './interface';

src/utils/draft.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DraftType, Options, ProxyDraft } from '../interface';
1+
import { DraftType, Mark, ProxyDraft } from '../interface';
22
import { dataTypes, PROXY_DRAFT } from '../constant';
33

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

25-
export function isDraftable(value: any, options?: Options<any, any>) {
25+
/**
26+
* Check if a value is draftable
27+
*/
28+
export function isDraftable(value: any, options?: { mark?: Mark<any, any> }) {
2629
if (!value || typeof value !== 'object') return false;
2730
let markResult: any;
2831
return (

test/index.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { create, original, current, apply } from '../src';
1+
import { create, original, current, apply, isDraftable, isDraft } from '../src';
22
import { PROXY_DRAFT } from '../src/constant';
33

44
test('check object type', () => {
@@ -2129,3 +2129,41 @@ test('check Primitive type with returning, patches, freeze and async', async ()
21292129
]);
21302130
}
21312131
});
2132+
2133+
test('base isDraft()', () => {
2134+
const baseState = {
2135+
date: new Date(),
2136+
list: [{ text: 'todo' }],
2137+
};
2138+
2139+
const state = create(baseState, (draft) => {
2140+
expect(isDraft(draft.date)).toBeFalsy();
2141+
expect(isDraft(draft.list)).toBeTruthy();
2142+
});
2143+
})
2144+
2145+
test('base isDraftable()', () => {
2146+
const baseState = {
2147+
date: new Date(),
2148+
list: [{ text: 'todo' }],
2149+
};
2150+
2151+
expect(isDraftable(baseState.date)).toBeFalsy();
2152+
expect(isDraftable(baseState.list)).toBeTruthy();
2153+
});
2154+
2155+
test('base isDraftable() with option', () => {
2156+
const baseState = {
2157+
date: new Date(),
2158+
list: [{ text: 'todo' }],
2159+
};
2160+
2161+
expect(
2162+
isDraftable(baseState.date, {
2163+
mark: (target, { immutable }) => {
2164+
if (target instanceof Date) return immutable;
2165+
},
2166+
})
2167+
).toBeTruthy();
2168+
expect(isDraftable(baseState.list)).toBeTruthy();
2169+
});

0 commit comments

Comments
 (0)