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

Made paths accept null #247

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function(options, storage, key) {
}

function reducer(state, paths) {
return paths.length === 0
return paths === null || paths === undefined
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about !Array.isArray(path) instead of checking both null and undefined?

? state
: paths.reduce(function(substate, path) {
return shvl.set(substate, path, shvl.get(state, path));
Expand Down Expand Up @@ -67,14 +67,16 @@ export default function(options, storage, key) {

if (typeof savedState === "object" && savedState !== null) {
store.replaceState(
options.overwrite ? savedState : merge(store.state, savedState, {
arrayMerge:
options.arrayMerger ||
function(store, saved) {
return saved;
},
clone: false
})
options.overwrite
? savedState
: merge(store.state, savedState, {
arrayMerge:
options.arrayMerger ||
function(store, saved) {
return saved;
},
clone: false
})
);
(options.rehydrated || function() {})(store);
}
Expand All @@ -83,7 +85,7 @@ export default function(options, storage, key) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,17 @@ it("fetches state from storage before the plugin is used", () => {
persisted: "before"
});
});

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({}));
});