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

Commit a34163f

Browse files
Make distinction between empty array and no array. Closes #247
1 parent 812f1d4 commit a34163f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Creates a new instance of the plugin with the given options. The following optio
9191
can be provided to configure the plugin for your specific needs:
9292

9393
- `key <String>`: The key to store the persisted state under. Defaults to `vuex`.
94-
- `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 `[]`.
94+
- `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`.
9595
- `reducer <Function>`: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.
9696
- `subscriber <Function>`: A function called to setup mutation subscription. Defaults to `store => handler => store.subscribe(handler)`.
9797

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ export default function (
5656
}
5757

5858
function reducer(state, paths) {
59-
return paths.length === 0
60-
? state
61-
: paths.reduce(function (substate, path) {
59+
return Array.isArray(paths)
60+
? paths.reduce(function (substate, path) {
6261
return shvl.set(substate, path, shvl.get(state, path));
63-
}, {});
62+
}, {})
63+
: state;
6464
}
6565

6666
function subscriber(store) {
@@ -111,7 +111,7 @@ export default function (
111111
if ((options.filter || filter)(mutation)) {
112112
(options.setState || setState)(
113113
key,
114-
(options.reducer || reducer)(state, options.paths || []),
114+
(options.reducer || reducer)(state, options.paths),
115115
storage
116116
);
117117
}

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ it("persist the changed partial state back to serialized JSON under a nested pat
136136
);
137137
});
138138

139+
it("should not persist whole store if paths array is empty", () => {
140+
const storage = new Storage();
141+
const store = new Vuex.Store({
142+
state: { original: "state" },
143+
});
144+
145+
const plugin = createPersistedState({ storage, paths: [] });
146+
plugin(store);
147+
148+
store._subscribers[0]("mutation", { changed: "state" });
149+
150+
expect(storage.getItem("vuex")).toBe(JSON.stringify({}));
151+
});
152+
139153
it("should not persist null values", () => {
140154
const storage = new Storage();
141155
const store = new Vuex.Store({

0 commit comments

Comments
 (0)