Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fippli committed Sep 13, 2019
0 parents commit c354aaa
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.npmrc
Empty file added .npmignore
Empty file.
24 changes: 24 additions & 0 deletions README.md
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);
```
24 changes: 24 additions & 0 deletions package.json
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"
}
9 changes: 9 additions & 0 deletions src/index.js
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,
};
18 changes: 18 additions & 0 deletions src/loadState.js
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;
13 changes: 13 additions & 0 deletions src/purgeState.js
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;
15 changes: 15 additions & 0 deletions src/saveState.js
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;

0 comments on commit c354aaa

Please sign in to comment.