Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Make distinction between empty array and no array. Closes #247
Browse files Browse the repository at this point in the history
  • Loading branch information
robinvdvleuten committed Mar 25, 2020
1 parent 812f1d4 commit a34163f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -91,7 +91,7 @@ Creates a new instance of the plugin with the given options. The following optio
can be provided to configure the plugin for your specific needs:

- `key <String>`: The key to store the persisted state under. Defaults to `vuex`.
- `paths <Array>`: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults to `[]`.
- `paths <Array>`: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. If an empty array is given, no state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults to `undefined`.
- `reducer <Function>`: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.
- `subscriber <Function>`: A function called to setup mutation subscription. Defaults to `store => handler => store.subscribe(handler)`.

Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Expand Up @@ -56,11 +56,11 @@ export default function (
}

function reducer(state, paths) {
return paths.length === 0
? state
: paths.reduce(function (substate, path) {
return Array.isArray(paths)
? paths.reduce(function (substate, path) {
return shvl.set(substate, path, shvl.get(state, path));
}, {});
}, {})
: state;
}

function subscriber(store) {
Expand Down Expand Up @@ -111,7 +111,7 @@ export default function (
if ((options.filter || filter)(mutation)) {
(options.setState || setState)(
key,
(options.reducer || reducer)(state, options.paths || []),
(options.reducer || reducer)(state, options.paths),
storage
);
}
Expand Down
14 changes: 14 additions & 0 deletions test.js
Expand Up @@ -136,6 +136,20 @@ it("persist the changed partial state back to serialized JSON under a nested pat
);
});

it("should not persist whole store if paths array is empty", () => {
const storage = new Storage();
const store = new Vuex.Store({
state: { original: "state" },
});

const plugin = createPersistedState({ storage, paths: [] });
plugin(store);

store._subscribers[0]("mutation", { changed: "state" });

expect(storage.getItem("vuex")).toBe(JSON.stringify({}));
});

it("should not persist null values", () => {
const storage = new Storage();
const store = new Vuex.Store({
Expand Down

0 comments on commit a34163f

Please sign in to comment.