Skip to content
This repository was archived by the owner on Apr 9, 2019. It is now read-only.

Commit 9c24553

Browse files
author
crossjs
committed
build 1.0.0
1 parent 6437069 commit 9c24553

File tree

9 files changed

+352
-209
lines changed

9 files changed

+352
-209
lines changed

README.md

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,32 @@
11
# VUEX-LOCALSTORAGE
22

3-
> :dvd: Manage data with expires by localStorage or some else storage.
3+
> :dvd: Persist Vuex state with expires by localStorage or some else storage.
44
55
[![Travis](https://img.shields.io/travis/crossjs/vuex-localstorage.svg?style=flat-square)](https://travis-ci.org/crossjs/vuex-localstorage)
66
[![Coveralls](https://img.shields.io/coveralls/crossjs/vuex-localstorage.svg?style=flat-square)](https://coveralls.io/github/crossjs/vuex-localstorage)
77
[![dependencies](https://david-dm.org/crossjs/vuex-localstorage.svg?style=flat-square)](https://david-dm.org/crossjs/vuex-localstorage)
88
[![devDependency Status](https://david-dm.org/crossjs/vuex-localstorage/dev-status.svg?style=flat-square)](https://david-dm.org/crossjs/vuex-localstorage?type=dev)
99
[![NPM version](https://img.shields.io/npm/v/vuex-localstorage.svg?style=flat-square)](https://npmjs.org/package/vuex-localstorage)
1010

11-
## Notice
12-
13-
It's NOT a Vuex plugin.
14-
15-
If you are looking for a Vuex plugin, see: https://github.com/robinvdvleuten/vuex-persistedstate
16-
17-
Here is a example for using them together:
11+
## Usage
1812

1913
``` js
2014
import { Store } from 'vuex'
21-
import createPersistedState from 'vuex-persistedstate'
22-
import createStorage from 'vuex-localstorage'
23-
24-
const storage = createStorage('namespace-for-state', {
25-
// initial state
26-
}, {
27-
// ONE_WEEK
28-
expires: 7 * 24 * 60 * 60 * 1e3
29-
})
15+
import createPersist, { createStorage } from 'vuex-localstorage'
3016

3117
new Store({
32-
plugins: [createPersistedState({
33-
getState: storage.get,
34-
setState: storage.set
18+
plugins: [createPersist({
19+
namespace: 'namespace-for-state'
20+
initialState: {},
21+
// ONE_WEEK
22+
expires: 7 * 24 * 60 * 60 * 1e3
3523
})]
3624
}
3725
```
3826
39-
[Live Example at PLATO](https://github.com/crossjs/plato/blob/master/src/modules/persist/index.js)
40-
41-
## Usage
42-
43-
With default key
44-
45-
``` js
46-
const persist = createPersist()
47-
persist.set({ foo: 'bar' }) // equals to persist.set('default', { foo: 'bar' })
48-
persist.get() // equals to persist.get('default'), return { foo: 'bar' }
49-
```
27+
[Live Example at PLATO](https://github.com/platojs/plato/blob/master/src/modules/persist/index.js)
5028
51-
With customize keys
52-
53-
``` js
54-
const persist = createPersist('my-namespace', { foo: { baz: 'bar' } })
55-
persist.get() // return { foo: { baz: 'bar' } }
56-
persist.get('foo') // return { baz: 'bar' }
57-
persist.set('foo', { baz: 'baz' }) // should NOT override initialState
58-
persist.get() // return { foo: { baz: 'bar' } }
59-
persist.get('foo') // return { baz: 'baz' }
60-
```
61-
62-
### Options
63-
64-
``` js
65-
/**
66-
* createPersist
67-
* @param {String} [namespace] 命名空间
68-
* @param {Object} [initialState] 初始值/默认值
69-
* @param {Object} [config] 配置 provider/serialize/deserialize/expires
70-
* @return {Object} 带 get/set 方法的对象
71-
*/
72-
```
73-
74-
### Development Setup
29+
## Development Setup
7530
7631
``` bash
7732
# install deps
@@ -87,6 +42,10 @@ npm test
8742
npm run unit
8843
```
8944
45+
## Special Thanks
46+
47+
[vuex-persistedstate](https://github.com/robinvdvleuten/vuex-persistedstate)
48+
9049
## License
9150
9251
[MIT](http://opensource.org/licenses/MIT)

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vuex-localstorage",
33
"version": "0.5.0",
4-
"description": ":dvd: Sync Vuex modules state to localStorage or some else storage.",
4+
"description": ":dvd: Persist Vuex state with expires by localStorage or some else storage.",
55
"main": "dist/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
@@ -18,6 +18,7 @@
1818
"url": "git+https://github.com/crossjs/vuex-localstorage.git"
1919
},
2020
"keywords": [
21+
"expires",
2122
"localstorage",
2223
"persist",
2324
"vuex"
@@ -54,7 +55,11 @@
5455
"phantomjs-prebuilt": "^2.1.13",
5556
"rollup": "^0.36.3",
5657
"rollup-plugin-buble": "^0.14.0",
58+
"sinon": "^1.17.7",
59+
"sinon-chai": "^2.8.0",
5760
"uglify-js": "^2.7.4",
61+
"vue": "^2.1.10",
62+
"vuex": "^2.1.2",
5863
"webpack": "^1.13.3",
5964
"yargs": "^6.3.0"
6065
}

src/index.js

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
1+
let index = Date.now()
2+
13
/**
2-
* createPersist
4+
* createStorage
35
*
46
* @param {String} namespace namespace
57
* @param {Object} [initialState] 初始值/默认值
68
* @param {Object} [config] 自定义 provider/serialize/deserialize/expires
79
* @return {Object} get/set 方法
810
*/
9-
export default function (namespace, initialState = {}, config) {
10-
const {
11-
provider,
12-
serialize,
13-
deserialize,
14-
merge,
15-
expires
16-
} = Object.assign({
17-
provider: localStorage,
18-
serialize: JSON.stringify,
19-
deserialize: JSON.parse,
20-
merge (initialState, persistedState) {
21-
return Object.assign({}, initialState, persistedState)
22-
},
23-
expires: 0 // never expires
24-
}, config)
25-
11+
export function createStorage ({
12+
namespace,
13+
initialState = {},
14+
provider = localStorage,
15+
serialize = JSON.stringify,
16+
deserialize = JSON.parse,
17+
expires = 0, // never expires
18+
merge = defaultMerge
19+
} = {}) {
2620
if (!namespace) {
27-
namespace = rnd()
21+
namespace = `vuex-${++index}`
2822
}
2923

3024
return {
@@ -73,6 +67,49 @@ export default function (namespace, initialState = {}, config) {
7367
}
7468
}
7569

76-
export function rnd () {
77-
return Date.now().toString(32) + Math.random().toString(32).slice(2)
70+
export default function createPersist ({
71+
namespace,
72+
initialState,
73+
provider,
74+
serialize,
75+
deserialize,
76+
expires,
77+
merge = defaultMerge,
78+
reducer = defaultReducer,
79+
paths = []
80+
} = {}) {
81+
return store => {
82+
const storage = createStorage({
83+
namespace,
84+
initialState,
85+
provider,
86+
serialize,
87+
deserialize,
88+
merge,
89+
expires
90+
})
91+
92+
store.replaceState(
93+
merge(store.state, storage.get())
94+
)
95+
96+
store.subscribe((mutation, state) => {
97+
storage.set(reducer(state, paths))
98+
})
99+
}
100+
}
101+
102+
function defaultMerge (...args) {
103+
return Object.assign({}, ...args)
104+
}
105+
106+
function defaultReducer (state, paths) {
107+
return paths.length === 0
108+
? state
109+
: paths.reduce((substate, path) => {
110+
if (state.hasOwnProperty(path)) {
111+
return Object.assign(substate, { [path]: state[path] })
112+
}
113+
return substate
114+
}, {})
78115
}

test/unit/.eslintrc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
"mocha": true
55
},
66
"globals": {
7-
"chai": false,
7+
"sinon": false,
88
"assert": false,
9-
"expect": false,
10-
"should": false
9+
"expect": false
1110
}
1211
}

test/unit/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
// Test Environment Setup
33
// ---------------------------------------
44
import chai from 'chai'
5+
import sinonChai from 'sinon-chai'
6+
7+
localStorage.clear()
8+
9+
chai.use(sinonChai)
510

6-
global.chai = chai
711
global.assert = chai.assert
812
global.expect = chai.expect
9-
global.should = chai.should()
1013

1114
// ---------------------------------------
1215
// Require Tests

test/unit/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const karmaConfig = {
77
basePath: '../../', // project root in relation to bin/karma.js
88
files: [
99
'./node_modules/phantomjs-polyfill/bind-polyfill.js',
10+
'./node_modules/sinon/pkg/sinon.js',
1011
{
1112
pattern: './test/unit/index.js',
1213
watched: false,

test/unit/specs/index.spec.js

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)