Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
polemius committed May 1, 2019
0 parents commit ccacdec
Show file tree
Hide file tree
Showing 7 changed files with 5,751 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
npm-debug.log
yarn-error.log

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright 2019 Ivan Menshykov <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# storeon-localstorage

## Instalation

```
yarn add storeon-localstorage
```

## Usage

```js
import persistState from 'storeon-localstorage'

const store = createStore([
/* modules */
persistState(),
])
```

### persistState(path, config)

```
type path = Void | String | Array<String>
```

If no pass the `path` value then `persistState` store in local storage all state.

```
type config.key = String
```

Default value of `config.key` is `storeon`. This key used to store date in local storage.

## LICENSE

MIT

## Acknowledgments

This module based on [redux-localstorage](https://github.com/elgerlambert/redux-localstorage).
39 changes: 39 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

module.exports = function (config = {}) {
const serialize = function (data) { return JSON.stringify(data) }
const deserialize = function (data) { return JSON.parse(data) }
return function (store) {
store.on('@init', function () {
try {
const saveState = localStorage.getItem('storeon')
if (saveState === null) {
return {}
}
return deserialize(saveState)
} catch (err) {
return {}
}
})
store.on('@dispatch', function (state, data) {
const event = data.event
const key = config.key
if (key === undefined) {
return
}
const keyToStore = state[config.key]
if (keyToStore === undefined) {
return
}
if (event === '@init') {
return
}
try {
const saveState = serialize({ [key]: keyToStore })
localStorage.setItem('storeon', saveState)
} catch (err) {
console.error(err)
}
})
}
}

47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "storeon-localstorage",
"version": "0.1.0",
"description": "Module for storeon to persist data from state to local storage",
"main": "index.js",
"repository": "https://github.com/polemius/storeon-localstorage",
"author": "Ivan Menshykov <[email protected]>",
"license": "MIT",
"scripts": {
"lint": "eslint *.js",
"size": "size-limit"
},
"devDependencies": {
"@logux/eslint-config": "^28.2.0",
"eslint": "^5.16.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-es5": "^1.3.1",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-import-helpers": "^0.1.4",
"eslint-plugin-jest": "^22.5.1",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-prefer-let": "^1.0.1",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-security": "^1.4.0",
"eslint-plugin-standard": "^4.0.0",
"size-limit": "^1.3.0"
},
"size-limit": [
{
"path": "index.js",
"limit": "186 B"
}
],
"eslintConfig": {
"extends": "@logux/eslint-config/browser",
"rules": {
"node/no-unpublished-require": "off",
"es5/no-es6-static-methods": "off",
"es5/no-destructuring": "off",
"es5/no-default-parameters": "off",
"es5/no-block-scoping": "off",
"es5/no-computed-properties": "off",
"node/no-missing-require": "off",
"func-style": "off"
}
}
}
Loading

0 comments on commit ccacdec

Please sign in to comment.