-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c354aaa
Showing
8 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.npmrc |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# @codewell/state-persistor | ||
Minimalistic state persistor for front end applications with localstorage. | ||
|
||
## Installation | ||
```bash | ||
npm install @codewell/state-persistor | ||
``` | ||
|
||
## Basic usage | ||
```JavaScript | ||
import { saveState, loadState, purgeState } from '@codewell/state-persistor'; | ||
|
||
const state = {}; // Some state | ||
const STORAGE_KEY = 'myCoolUniqueApplicationStorageKey'; // Unique key to store the state in localstorage | ||
|
||
// Persist the state | ||
saveState(state, STORAGE_KEY); | ||
|
||
// Get the persisted state | ||
const loadedState = loadState(STORAGE_KEY); | ||
|
||
// Clear any saved state | ||
purgeState(STORAGE_KEY); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@codewell/state-persistor", | ||
"version": "1.0.0", | ||
"description": "State persistor for front end applications", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/codewell/state-persistor.git" | ||
}, | ||
"keywords": [ | ||
"state", | ||
"localstorage", | ||
"persist" | ||
], | ||
"author": "Filip Johansson", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/codewell/state-persistor/issues" | ||
}, | ||
"homepage": "https://github.com/codewell/state-persistor#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const loadState = require('./loadState'); | ||
const purgeState = require('./purgeState'); | ||
const saveState = require('./saveState'); | ||
|
||
module.exports = { | ||
loadState, | ||
purgeState, | ||
saveState, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Load a state from local storage | ||
* @param {string} storageKey - What key in localstorage to load from | ||
* @returns {Object} - Returns the persisted state | ||
*/ | ||
const loadState = (storageKey) => { | ||
try { | ||
const serializedState = localStorage.getItem(storageKey); | ||
if (serializedState === null) { | ||
return undefined; | ||
} | ||
return JSON.parse(serializedState); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
module.exports = loadState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Remove everything from local storage | ||
* @param {string} storageKey - What key in localstorage to purge | ||
*/ | ||
const purgeState = (storageKey) => { | ||
try { | ||
localStorage.removeItem(storageKey); | ||
} catch (error) { | ||
// Ignore write errors | ||
} | ||
}; | ||
|
||
module.exports = purgeState; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Save a state to local storage | ||
* @param {Object} state - State to be persisted | ||
* @param {string} storageKey - What key in localstorage to save the state to | ||
*/ | ||
const saveState = (state, storageKey) => { | ||
try { | ||
const serializedState = JSON.stringify(state); | ||
localStorage.setItem(storageKey, serializedState); | ||
} catch (error) { | ||
// Ignore write errors | ||
} | ||
}; | ||
|
||
module.exports = saveState; |