src getter fix, local storage fix

This commit is contained in:
Sam Potts
2017-11-05 11:45:02 +11:00
parent 8aaa932050
commit 1c693df00b
10 changed files with 71 additions and 53 deletions

View File

@ -5,18 +5,37 @@
import support from './support';
import utils from './utils';
// Get contents of local storage
function get() {
const store = window.localStorage.getItem(this.config.storage.key);
if (utils.is.empty(store)) {
return {};
}
return JSON.parse(store);
}
// Save a value back to local storage
function set(value) {
function set(object) {
// Bail if we don't have localStorage support or it's disabled
if (!support.storage || !this.config.storage.enabled) {
return;
}
// Can only store objectst
if (!utils.is.object(object)) {
return;
}
// Get current storage
const storage = get.call(this);
// Update the working copy of the values
utils.extend(this.storage, value);
utils.extend(storage, object);
// Update storage
window.localStorage.setItem(this.config.storage.key, JSON.stringify(this.storage));
window.localStorage.setItem(this.config.storage.key, JSON.stringify(storage));
}
// Setup localStorage
@ -53,4 +72,4 @@ function setup() {
return storage;
}
export default { setup, set };
export default { setup, set, get };