storeon-localstorage
Advanced tools
Comparing version 0.1.0 to 0.2.0
47
index.js
@@ -1,13 +0,23 @@ | ||
module.exports = function (config = {}) { | ||
/** | ||
* Storeon module to persist state to local storage | ||
* | ||
* @param {String|String[]} path The keys of state object | ||
* that will be store in local storage | ||
* @param {Object} config The config object | ||
* @param {String} [config.key='storeon'] The default key | ||
* to use in local storage | ||
*/ | ||
const persistState = function (path, config = { key: 'storeon' }) { | ||
const serialize = function (data) { return JSON.stringify(data) } | ||
const deserialize = function (data) { return JSON.parse(data) } | ||
const key = config.key | ||
return function (store) { | ||
store.on('@init', function () { | ||
try { | ||
const saveState = localStorage.getItem('storeon') | ||
if (saveState === null) { | ||
const savedState = localStorage.getItem(key) | ||
if (savedState === null) { | ||
return {} | ||
} | ||
return deserialize(saveState) | ||
return deserialize(savedState) | ||
} catch (err) { | ||
@@ -18,17 +28,21 @@ return {} | ||
store.on('@dispatch', function (state, data) { | ||
const event = data.event | ||
const key = config.key | ||
if (key === undefined) { | ||
const event = data[0] | ||
if (event === '@init') { | ||
return | ||
} | ||
const keyToStore = state[config.key] | ||
if (keyToStore === undefined) { | ||
return | ||
let stateToStore = { } | ||
if (typeof path === 'string') { | ||
stateToStore = { [path]: state[path] } | ||
} else if (Array.isArray(path)) { | ||
path.forEach(function (p) { | ||
stateToStore[p] = state[p] | ||
}) | ||
} else { | ||
stateToStore = state | ||
} | ||
if (event === '@init') { | ||
return | ||
} | ||
try { | ||
const saveState = serialize({ [key]: keyToStore }) | ||
localStorage.setItem('storeon', saveState) | ||
const saveState = serialize(stateToStore) | ||
localStorage.setItem(key, saveState) | ||
} catch (err) { | ||
@@ -41,1 +55,2 @@ console.error(err) | ||
module.exports = persistState |
{ | ||
"name": "storeon-localstorage", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Module for storeon to persist data from state to local storage", | ||
@@ -11,6 +11,9 @@ "main": "index.js", | ||
"lint": "eslint *.js", | ||
"size": "size-limit" | ||
"size": "size-limit", | ||
"spell": "yarn docs && yaspeller *.md", | ||
"docs": "documentation build *.js -f md -o api.md" | ||
}, | ||
"devDependencies": { | ||
"@logux/eslint-config": "^28.2.0", | ||
"documentation": "^10.1.0", | ||
"eslint": "^5.16.0", | ||
@@ -27,5 +30,6 @@ "eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"husky": "^2.1.0", | ||
"lint-staged": "^8.1.5", | ||
"size-limit": "^1.3.0", | ||
"husky": "^2.1.0" | ||
"yaspeller": "^5.0.1" | ||
}, | ||
@@ -35,3 +39,3 @@ "size-limit": [ | ||
"path": "index.js", | ||
"limit": "186 B" | ||
"limit": "208 B" | ||
} | ||
@@ -53,3 +57,4 @@ ], | ||
"lint-staged": { | ||
"*.js": "eslint" | ||
"*.js": "eslint", | ||
"*.md": "yaspeller" | ||
}, | ||
@@ -60,3 +65,13 @@ "husky": { | ||
} | ||
}, | ||
"yaspeller": { | ||
"lang": "en", | ||
"ignoreCapitalization": true, | ||
"dictionary": [ | ||
"storeon", | ||
"persistState", | ||
"localstorage", | ||
"redux" | ||
] | ||
} | ||
} |
# storeon-localstorage | ||
## Instalation | ||
## Installation | ||
@@ -5,0 +5,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5946
5
50
17