Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jul 24, 2022
1 parent 0aeee5a commit 985e72e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/core/useStorage/index.md
Expand Up @@ -60,7 +60,7 @@ console.log(state.hello) // 'nihao', from storage
console.log(state.greeting) // 'hello', from merged default value
```

When setting it to true, it will perform a **shallow merge** for objects/arrays. You can pass a custom merge function or deep merge, for example:
When setting it to true, it will perform a **shallow merge** for objects. You can pass a function to perform custom merge (e.g. deep merge), for example:

```ts
const state = useStorage(
Expand Down
15 changes: 10 additions & 5 deletions packages/core/useStorage/index.test.ts
Expand Up @@ -336,21 +336,26 @@ describe('useStorage', () => {
// basic
storage.setItem(KEY, '0')
const basicRef = useStorage(KEY, 1, storage, { mergeDefaults: true })
expect(basicRef.value).toBe(1)
expect(basicRef.value).toBe(0)

// object
storage.setItem(KEY, JSON.stringify({ a: 1 }))
const objectRef = useStorage(KEY, { a: 2, b: 3 }, storage, { mergeDefaults: true })
expect(JSON.stringify(objectRef.value)).toBe(JSON.stringify({ a: 2, b: 3 }))
expect(objectRef.value).toEqual({ a: 1, b: 3 })

// array
storage.setItem(KEY, JSON.stringify([1]))
const arrayRef = useStorage(KEY, [2], storage, { mergeDefaults: true })
expect(JSON.stringify(arrayRef.value)).toBe(JSON.stringify([1, 2]))
expect(arrayRef.value).toEqual([1])

// custom function
storage.setItem(KEY, JSON.stringify([{ a: 1 }]))
const customRef = useStorage(KEY, [{ a: 3 }], storage, { mergeDefaults: (value, initial) => ([...initial, ...value]) })
expect(JSON.stringify(customRef.value)).toBe(JSON.stringify([{ a: 3 }, { a: 1 }]))
const customRef = useStorage(KEY, [{ a: 3 }], storage, { mergeDefaults: (value, initial) => [...initial, ...value] })
expect(customRef.value).toEqual([{ a: 3 }, { a: 1 }])

// custom function 2
storage.setItem(KEY, '1')
const customRef2 = useStorage(KEY, 2, storage, { mergeDefaults: (value, initial) => value + initial })
expect(customRef2.value).toEqual(3)
})
})
10 changes: 5 additions & 5 deletions packages/core/useStorage/index.ts
Expand Up @@ -78,8 +78,8 @@ export interface UseStorageOptions<T> extends ConfigurableEventFilter, Configura
/**
* Merge the default value with the value read from the storage.
*
* When setting to true, it will perform a shallow merge for objects/arrays.
* You can pass a custom merge function or deep merge.
* When setting it to true, it will perform a **shallow merge** for objects.
* You can pass a function to perform custom merge (e.g. deep merge), for example:
*
* @default false
*/
Expand Down Expand Up @@ -198,9 +198,9 @@ export function useStorage<T extends(string | number | boolean | object | null)>
const value = serializer.read(rawValue)
if (isFunction(mergeDefaults))
return mergeDefaults(value, rawInit)
else if (type === 'object')
return Array.isArray(value) ? [...rawInit as any, ...value] : { ...rawInit as any, ...value }
return rawInit
else if (type === 'object' && !Array.isArray(value))
return { ...rawInit as any, ...value }
return value
}
else if (typeof rawValue !== 'string') {
return rawValue
Expand Down

0 comments on commit 985e72e

Please sign in to comment.