Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit 40d4e6b

Browse files
committedDec 3, 2017
First complete feature set
1 parent 601f68a commit 40d4e6b

13 files changed

+4225
-0
lines changed
 

‎.babelrc

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"modules": false,
7+
"targets": {
8+
"browsers": [
9+
"> 2%",
10+
"last 2 versions",
11+
"ie 11"
12+
],
13+
"uglify": true
14+
}
15+
}
16+
]
17+
],
18+
"plugins": [
19+
"transform-object-assign"
20+
],
21+
"env": {
22+
"test": {
23+
"presets": [
24+
[
25+
"env",
26+
{
27+
"targets": {
28+
"node": "current"
29+
}
30+
}
31+
]
32+
]
33+
}
34+
}
35+
}

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ composer.phar
6969
*.tgz
7070
/docs/
7171
/dist/
72+
/coverage
7273

‎.npmignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Don't publish these files and folders
2+
/.idea/
3+
/.github/
4+
/examples/
5+
/docs/
6+
/test/
7+
/__test__/
8+
/coverage/
9+
10+
.babelrc
11+
.editorconfig
12+
.gitattributes
13+
/.gitignore
14+
.npmignore
15+
.travis.yml
16+
17+
npm-shrinkwrap.json
18+
package-lock.json
19+
yarn.lock
20+
21+
webpack.config.dev.js
22+
webpack.config.js

‎.travis.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Config file for https://travis-ci.org/
2+
# Validate this file here - http://lint.travis-ci.org/
3+
4+
language: node_js
5+
node_js:
6+
- "6"
7+
8+
# Speed up git clones
9+
git:
10+
depth: 10
11+
submodules: false
12+
13+
# Install custom version
14+
before_install:
15+
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.3.2
16+
- export PATH="$HOME/.yarn/bin:$PATH"
17+
18+
install:
19+
- yarn install --non-interactive
20+
21+
script:
22+
- yarn test -- --verbose
23+
- yarn run build
24+
25+
after_success:
26+
- bash <(curl -s https://codecov.io/bash) -e TRAVIS_NODE_VERSION
27+
28+
# Tell Travis CI to monitor only these branches
29+
branches:
30+
only:
31+
- master
32+
- dev
33+
34+
# $HOME/.cache/yarn
35+
cache:
36+
yarn: true

‎LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Ankur Kumar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Vue-web-storage
2+
3+
[![vue-js](https://img.shields.io/badge/vue.js-2.x-brightgreen.svg?maxAge=604800)](https://vuejs.org/)
4+
[![downloads](https://img.shields.io/npm/dt/vue-web-storage.svg)](http://npm-stats.com/~packages/vue-web-storage)
5+
[![npm-version](https://img.shields.io/npm/v/vue-web-storage.svg)](https://www.npmjs.com/package/vue-web-storage)
6+
[![github-tag](https://img.shields.io/github/tag/ankurk91/vue-web-storage.svg?maxAge=1800)](https://github.com/ankurk91/vue-web-storage/)
7+
[![license](https://img.shields.io/github/license/ankurk91/vue-web-storage.svg?maxAge=1800)](https://yarnpkg.com/en/package/vue-web-storage)
8+
[![build-status](https://travis-ci.org/ankurk91/vue-web-storage.svg?branch=master)](https://travis-ci.org/ankurk91/vue-web-storage)
9+
[![codecov](https://codecov.io/gh/ankurk91/vue-web-storage/branch/master/graph/badge.svg)](https://codecov.io/gh/ankurk91/vue-web-storage)
10+
11+
Vue.js v2.x plugin for web storage
12+
13+
## Features
14+
* Choose between `localStorage` or `sessionStorage`
15+
* Prefix all of your stored keys
16+
* Auto `JSON.stringify` and `JSON.parse`
17+
18+
## Installation
19+
```bash
20+
# npm
21+
npm install vue-web-storage --save
22+
23+
# Yarn
24+
yarn add vue-web-storage
25+
```
26+
27+
## Usage
28+
```js
29+
import Vue from 'vue';
30+
import Storage from 'vue-web-storage';
31+
Vue.use(Storage);
32+
```
33+
34+
## Configuration
35+
```js
36+
Vue.use(Storage, {
37+
prefix: 'your_app_name',// default `app_`
38+
driver: 'session', // default 'local'
39+
})
40+
```
41+
42+
### Methods
43+
#### `set(key,value)`
44+
```js
45+
Vue.$storage.set('name', 'john')
46+
Vue.$storage.set('isAdmin', true)
47+
Vue.$storage.set('roles', ['admin', 'sub-admin'])
48+
Vue.$storage.set('permission', {id: 2, slug: 'edit_post'})
49+
```
50+
#### `get(key)`
51+
```js
52+
Vue.$storage.get('name')
53+
```
54+
#### `remove(key)`
55+
```js
56+
Vue.$storage.remove('name')
57+
```
58+
#### `clear()`
59+
```js
60+
Vue.$storage.clear()
61+
```
62+
#### `keys(withPrefix)`
63+
```js
64+
Vue.$storage.keys()
65+
```
66+
#### `hasKey(key)`
67+
```js
68+
Vue.$storage.hasKey('name')
69+
```
70+
#### `length()`
71+
```js
72+
Vue.$storage.length()
73+
```
74+
75+
## Install in non-module environments (without webpack)
76+
* Include required files
77+
```html
78+
<!-- Vue js -->
79+
<script src="https://unpkg.com/vue@2.5/dist/vue.min.js"></script>
80+
<!-- Lastly add this package -->
81+
<script src="https://unpkg.com/vue-web-storage"></script>
82+
```
83+
* Initialize
84+
```js
85+
Vue.use(VueWebStorage)
86+
```
87+
88+
## Testing
89+
* This package is using [Jest](https://github.com/facebook/jest) for testing
90+
* Tests can be found in `__test__` folder.
91+
* Execute tests with this command `yarn test`
92+
93+
## Resources
94+
* [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)
95+
* [Browser support status](https://caniuse.com/#feat=namevalue-storage), [Chrome](https://www.chromestatus.com/feature/5345825534246912), [Edge](https://developer.microsoft.com/en-us/microsoft-edge/platform/status/webstorage/)
96+
* [Web Storage Quota](https://www.html5rocks.com/en/tutorials/offline/quota-research/)
97+
98+
## License
99+
[MIT](LICENSE.txt) License

‎__test__/storage.test.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import Storage from '../src/storage';
2+
3+
describe('Storage class', () => {
4+
5+
let ls;
6+
7+
beforeEach(() => {
8+
ls = new Storage({
9+
prefix: 'app_',
10+
driver: 'local'
11+
});
12+
});
13+
14+
afterEach(() => {
15+
ls.clear();
16+
});
17+
18+
test('accepts prefix', () => {
19+
expect(ls.prefix).toBe('app_');
20+
});
21+
22+
test('can store strings', () => {
23+
let key = 'name';
24+
let value = 'john';
25+
26+
ls.set(key, value);
27+
expect(ls.get(key)).toBe(value)
28+
});
29+
30+
test('can store boolean', () => {
31+
let key = 'flag';
32+
let value = true;
33+
34+
ls.set(key, value);
35+
expect(ls.get(key)).toBe(value)
36+
});
37+
38+
test('can store objects', () => {
39+
let key = 'cat';
40+
let value = {id: 12, name: 'kitten'};
41+
42+
ls.set(key, value);
43+
expect(ls.get(key)).toEqual(value)
44+
});
45+
46+
47+
test('can store arrays', () => {
48+
let key = 'ids';
49+
let value = [123, 456, 789];
50+
51+
ls.set(key, value);
52+
expect(ls.get(key)).toEqual(value)
53+
});
54+
55+
test('can store array of objects', () => {
56+
let key = 'cats';
57+
let value = [{id: 1, name: 'kitten'}, {id: 2, name: 'mum'}];
58+
59+
ls.set(key, value);
60+
expect(ls.get(key)).toEqual(value)
61+
});
62+
63+
test('can store null', () => {
64+
let key = 'nullable';
65+
let value = null;
66+
67+
ls.set(key, value);
68+
expect(ls.get(key)).toBe(value)
69+
});
70+
71+
test('can store numbers', () => {
72+
let key = 'age';
73+
let value = 26;
74+
75+
ls.set(key, value);
76+
expect(ls.get(key)).toBe(value)
77+
});
78+
79+
test('can remove stored key', () => {
80+
let key = 'age';
81+
let value = 26;
82+
83+
ls.set(key, value);
84+
ls.remove(key);
85+
expect(ls.get(key)).toBe(null)
86+
});
87+
88+
test('can empty storage', () => {
89+
ls.set('name', 'jest');
90+
ls.set('age', 26);
91+
ls.clear();
92+
93+
expect(ls.get('age')).toBe(null);
94+
expect(ls.get('name')).toBe(null);
95+
expect(ls.length()).toEqual(0)
96+
});
97+
98+
test('can get stored key names', () => {
99+
ls.set('name', 'jest');
100+
ls.set('age', 26);
101+
102+
expect(ls.keys()).toEqual(['name', 'age'])
103+
});
104+
105+
test('can check if key exits', () => {
106+
ls.set('name', 'jest');
107+
108+
expect(ls.hasKey('name')).toBe(true);
109+
expect(ls.hasKey('unknown')).toBe(false)
110+
});
111+
112+
});

‎__test__/vue-storage.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {Storage, Plugin as VueWebStorage} from '../src/index';
2+
// Lets import full build
3+
import Vue from 'vue/dist/vue.common';
4+
5+
Vue.config.productionTip = false;
6+
7+
describe('Vue Storage plugin', () => {
8+
9+
10+
test('Vue.$storage', () => {
11+
Vue.use(VueWebStorage);
12+
13+
expect(Vue.$storage instanceof Storage).toBe(true);
14+
});
15+
16+
test('localVue.$storage', () => {
17+
let localVue = Vue.extend();
18+
localVue.use(VueWebStorage);
19+
20+
expect(localVue.$storage instanceof Storage).toBe(true);
21+
});
22+
23+
test('parameters', () => {
24+
let localVue = Vue.extend();
25+
localVue.use(VueWebStorage, {prefix: 'vue_'});
26+
27+
expect(localVue.$storage.prefix).toEqual('vue_');
28+
});
29+
30+
});

0 commit comments

Comments
 (0)
This repository has been archived.