Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow all deepmerge.Options to be set #258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ Here are the properties, and what they mean -
| modules | string[] | List of modules you want to persist. (Do not write your own reducer if you want to use this) |
| asyncStorage | boolean | Denotes if the store uses Promises (like localforage) or not (you must set this to true when using something like localforage) <br>_**Default: false**_ |
| supportCircular | boolean | Denotes if the state has any circular references to itself (state.x === state) <br>_**Default: false**_ |
| mergeOption | String or deepmerge Options | By default, when merging previous state with the new state, arrays are replaced. <br>You can pass 'concatArrays' to concatenate them instead.<br>Additionally, you can pass through whatever deepmerge.Options you want. <br>_**Default: 'replaceArrays'**_ |

### Usage Notes

Expand Down Expand Up @@ -258,6 +259,35 @@ new VuexPersistence({
})
```

#### Non-Plain objects in the store

If you have non-plain objects in your store, you need to provide a way to identify them via the `mergeOption`.

Otherwise, they will be turned into plain objects.

```js
import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersistence from 'vuex-persist'
import { replaceArrays } from 'vuex-persist'
import { isPlainObject } from 'is-plain-object';

Vue.use(Vuex)

var store = new Vuex.Store({
state: {
user: new User('arny'),
navigation: { path: '/home' }
},
plugins: [new VuexPersistence({
mergeOption: {
isMergeableObject: isPlainObject,
arrayMerge: replaceArrays
}
}).plugin]
})
```

## Examples

### Simple
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AsyncStorage } from './AsyncStorage'
import { MockStorage } from './MockStorage'
import { PersistOptions } from './PersistOptions'
import SimplePromiseQueue from './SimplePromiseQueue'
import { merge, MergeOptionType } from './utils'
import { merge, MergeOptionType, replaceArrays, concatArrays } from './utils'

let FlattedJSON = JSON

Expand Down Expand Up @@ -279,7 +279,7 @@ export class VuexPersistence<S> implements PersistOptions<S> {
}

export {
MockStorage, AsyncStorage, PersistOptions
MockStorage, AsyncStorage, PersistOptions, MergeOptionType, replaceArrays, concatArrays
}

export default VuexPersistence
22 changes: 12 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import deepmerge from 'deepmerge'

export type MergeOptionType = 'replaceArrays' | 'concatArrays'
export type MergeOptionType = 'replaceArrays' | 'concatArrays' | deepmerge.Options

const options: {[k in MergeOptionType]: deepmerge.Options} = {
export const replaceArrays = (destinationArray: any[], sourceArray: any[], options:deepmerge.Options) => sourceArray
export const concatArrays = (target: any[], source: any[], options:deepmerge.Options) => target.concat(...source)

const stringOptions = {
replaceArrays: {
arrayMerge: (destinationArray, sourceArray, options) => sourceArray
arrayMerge: replaceArrays
},
concatArrays: {
arrayMerge: (target, source, options) => target.concat(...source)
arrayMerge: concatArrays
}
}

const defaultMergeOptions: deepmerge.Options = {
// replacing arrays

}

export function merge<I, F>(into: Partial<I>, from: Partial<F>, mergeOption: MergeOptionType): I & F & {} {
return deepmerge(into, from, options[mergeOption])
if (typeof mergeOption == 'string') {
return deepmerge(into, from, stringOptions[mergeOption])
} else {
return deepmerge(into, from, mergeOption)
}
}