Skip to content

Commit

Permalink
fix: merge array
Browse files Browse the repository at this point in the history
  • Loading branch information
ouduidui committed Apr 27, 2022
1 parent 4885ce6 commit e6b5528
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
65 changes: 65 additions & 0 deletions __tests__/index.spec.ts
Expand Up @@ -216,4 +216,69 @@ 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()
})
})
})
3 changes: 3 additions & 0 deletions dist/index.d.ts
Expand Up @@ -30,14 +30,17 @@ interface Storage {
interface PersistOptions {
/**
* it is the prefix of the key to store the persisted state under, such as the store id is "test", that store key is "pinia_test" by default.
* @default pinia
*/
prefix?: string;
/**
* it is the storage for state persist, you can choose localStorage, sessionStorage or your custom storage.
* @default window.localStorage
*/
storage?: Storage;
/**
* when app first loader or refresh, whether to overwrite the existing state with the output from state directly, instead of merging the two objects with deep merge.
* @default false
*/
overwrite?: boolean;
}
Expand Down
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.

21 changes: 20 additions & 1 deletion src/helper.ts
@@ -1,3 +1,22 @@
export const isObject = (val: unknown) => val && typeof val === 'object'

export const mergeArrayWithDedupe = (a: any[], b: any[]) => Array.from(new Set([...a, ...b]))
export const mergeArrayWithDedupe = (a: any[], b: any[]) => {
const result: any[] = [...a]

for (const item of b) {
if (!isObject(item)) {
if (!result.includes(item)) result.push(item)
}
else {
const itemStr = JSON.stringify(item)
if (!a.some((i) => {
if (!isObject(i)) return false
else
return itemStr === JSON.stringify(i)
}))
result.push(item)
}
}

return result
}

0 comments on commit e6b5528

Please sign in to comment.