Skip to content

Commit 62fe694

Browse files
committed
Initial commit
0 parents  commit 62fe694

File tree

8 files changed

+157
-0
lines changed

8 files changed

+157
-0
lines changed

.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"optional": ["es7.objectRestSpread"]
3+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.log
3+
lib

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src

Makefile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Vars
3+
#
4+
5+
BIN = ./node_modules/.bin
6+
.DEFAULT_GOAL := all
7+
8+
#
9+
# Tasks
10+
#
11+
12+
node_modules: package.json
13+
@npm install
14+
@touch node_modules
15+
16+
test: node_modules
17+
${BIN}/babel-tape-runner test/*.js
18+
19+
validate: node_modules
20+
@${BIN}/standard
21+
22+
all: validate test
23+
24+
#
25+
# Phony
26+
#
27+
28+
.PHONY: test validate

Readme.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# redux-effects-localstorage
3+
4+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
5+
6+
Redux effects driver for localStorage
7+
8+
## Installation
9+
10+
$ npm install redux-effects-localstorage
11+
12+
## Usage
13+
14+
Simple [redux-effects](https://github.com/redux-effects/redux-effects) driver for [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage). Add it to your redux middleware stack like this:
15+
16+
```javascript
17+
import localStorageDriver from 'redux-effects-localstorage'
18+
19+
applyMiddleware(localStorageDriver(window.localStorage))(store)
20+
```
21+
22+
And then dispatch it actions, like this:
23+
24+
```javascript
25+
store.dispatch({
26+
type: 'EFFECT_LOCALSTORAGE',
27+
payload: {
28+
type: 'set',
29+
key: 'todos.0',
30+
value: 'Clean the kitchen'
31+
}
32+
})
33+
```
34+
35+
Or, if you don't feel like writing all that out, use an action creator, like [declarative-localstorage](https://github.com/redux-effects/declarative-localstorage).
36+
37+
## License
38+
39+
The MIT License
40+
41+
Copyright © 2015, Weo.io <[email protected]>
42+
43+
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:
44+
45+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46+
47+
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.

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "redux-effects-localstorage",
3+
"description": "Redux effects driver for localStorage",
4+
"repository": "git://github.com/redux-effects/redux-effects-localstorage.git",
5+
"version": "0.1.0",
6+
"license": "MIT",
7+
"main": "lib/index.js",
8+
"scripts": {
9+
"prepublish": "rm -rf lib && babel src --out-dir lib",
10+
"postpublish": "rm -rf lib"
11+
},
12+
"dependencies": {},
13+
"devDependencies": {
14+
"babel": "^5.8.21",
15+
"babel-tape-runner": "^1.2.0",
16+
"standard": "^5.1.0",
17+
"tape": "^4.2.0"
18+
}
19+
}

src/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Action type
3+
*/
4+
5+
const EFFECT_LOCALSTORAGE = 'EFFECT_LOCALSTORAGE'
6+
7+
/**
8+
* redux-effects-localstorage
9+
*/
10+
11+
function storage (localStorage = window.localStorage) {
12+
return api => next => action
13+
return action.type === EFFECT_LOCALSTORAGE
14+
? execute(action)
15+
: next(action)
16+
17+
function execute ({type, key, value, n}) {
18+
switch (type) {
19+
case 'key':
20+
return Promise.resolve(localStorage.key(n))
21+
case 'getItem':
22+
return Promise.resolve(localStorage.getItem(key))
23+
case 'setItem':
24+
return Promise.resolve(localStorage.setItem(key, value))
25+
case 'removeItem':
26+
return Promise.resolve(localStorage.removeItem(key, value))
27+
case 'clear':
28+
return Promise.resolve(localStorage.clear())
29+
case 'length':
30+
return Promise.resolve(localStorage.length)
31+
default:
32+
throw new Error('redux-effects-localstorage unknown localStorage action type')
33+
}
34+
}
35+
}
36+
37+
38+
/**
39+
* Exports
40+
*/
41+
42+
export default localstorage

test/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Imports
3+
*/
4+
5+
import test from 'tape'
6+
import redux-effects-localstorage from '../src'
7+
8+
/**
9+
* Tests
10+
*/
11+
12+
test('should work', () => {
13+
14+
})

0 commit comments

Comments
 (0)