Skip to content

Commit

Permalink
feat: remove deepmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
ouduidui committed Apr 27, 2022
1 parent 0950752 commit 342237e
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 126 deletions.
79 changes: 0 additions & 79 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,6 @@ describe('Persist', () => {
expect(store.test).toBe('bar')
})

it('deep merge to sync state', () => {
window.localStorage.clear()
window.localStorage.setItem('pinia_foo', JSON.stringify({ name: 'dewey', hobbies: ['code', 'movie'] }))
const testStore = defineStore('foo', {
state: () => ({ age: 24, hobbies: ['code', 'cube'] }),
})
const pinia = createPinia()
pinia.use(persist())

mount({ template: 'none' }, { global: { plugins: [pinia] } })
const store = testStore(pinia)
expect(store.$state).toStrictEqual({ name: 'dewey', age: 24, hobbies: ['code', 'cube', 'movie'] })
})

it('sync storage data when state change', (done) => {
window.localStorage.clear()
const testStore = defineStore('foo', {
Expand Down Expand Up @@ -216,69 +202,4 @@ describe('Persist', () => {
done()
})
})

it('no strict patch', (done) => {
window.localStorage.clear()
window.localStorage.setItem('pinia_foo', JSON.stringify({
test: [
{ id: 1, name: 'foo' },
],
}))

const testStore = defineStore('foo', {
state: () => ({
test: [
{ id: 1, name: 'foo' },
],
}),
actions: {
add(item: { id: number; name: string }) {
this.test.push(item)
},
remove() {
this.test = this.test.filter(item => item.id !== 1)
},
},
})
const pinia = createPinia()
pinia.use(persist())

mount({ template: 'none' }, { global: { plugins: [pinia] } })
const store = testStore(pinia)
expect(store.test).toStrictEqual([
{ id: 1, name: 'foo' },
])

Promise.resolve().then(() => {
expect(window.localStorage.getItem('pinia_foo')).toBe(JSON.stringify({
test: [
{ id: 1, name: 'foo' },
],
}))
store.add({ id: 2, name: 'bar' })
}).then(() => {
expect(window.localStorage.getItem('pinia_foo')).toBe(JSON.stringify({
test: [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
],
}))
store.remove()
}).then(() => {
expect(window.localStorage.getItem('pinia_foo')).toBe(JSON.stringify({
test: [
{ id: 2, name: 'bar' },
],
}))
store.add({ id: 2, name: 'bar' })
}).then(() => {
expect(window.localStorage.getItem('pinia_foo')).toBe(JSON.stringify({
test: [
{ id: 2, name: 'bar' },
{ id: 2, name: 'bar' },
],
}))
done()
})
})
})
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions src/helper.ts

This file was deleted.

25 changes: 1 addition & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { PiniaPluginContext } from 'pinia'
import { watch } from 'vue'
import { isObject, mergeArrayWithDedupe } from './helper'
import type { PersistOptions, Storage } from './types'

export function persist(options: PersistOptions = {}) {
Expand All @@ -18,7 +17,7 @@ export function persist(options: PersistOptions = {}) {
const storageResult = getItem(key, storage as Storage)
if (!storageResult || overwrite) setItem(key, store.$state, storage)
else
store.$patch(deepMerge(store.$state, storageResult))
store.$patch(storageResult)

watch(store.$state, () => {
setItem(key, store.$state, storage)
Expand All @@ -42,25 +41,3 @@ function getItem(key: string, storage: Storage): Record<string, any> | null {
function setItem(key: string, state: unknown, storage: Storage) {
return storage.setItem(key, JSON.stringify(state))
}

function deepMerge(
oldObj: Record<string | number | symbol, any>,
newObj: Record<string, any>,
) {
const target: any = {}
for (const key of Object.keys(newObj)) {
const oldVal = oldObj[key]
const newVal = newObj[key]

if (Array.isArray(oldVal) && Array.isArray(newVal))
target[key] = mergeArrayWithDedupe(oldVal, newVal)

else if (isObject(oldVal) && isObject(newVal))
target[key] = deepMerge(oldVal, newVal)

else
target[key] = newVal
}

return { ...oldObj, ...target }
}

0 comments on commit 342237e

Please sign in to comment.