Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit 28b3bc3

Browse files
committed
initial commit
0 parents  commit 28b3bc3

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed

.gitignore

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
node_modules
28+
29+
# Optional npm cache directory
30+
.npm
31+
32+
# Optional REPL history
33+
.node_repl_history

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Kyle J. Kemp
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

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# ng2-storage
2+
A service wrapping local and session storage for ng2.
3+
4+
# Install
5+
`npm i -s ng2-storage`
6+
7+
# Browser Support
8+
This library makes heavy use of [ES6 Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), meaning it only has support in the latest Edge, Chrome, Firefox, and Opera.
9+
10+
# Usage
11+
First, bootstrap the service globally:
12+
13+
```js
14+
import { StorageSettings } from 'ng2-storage';
15+
16+
bootstrap(App, [
17+
provide(StorageSettings, { useValue: { prefix: 'ng2-storage' } })
18+
]);
19+
```
20+
21+
Next, inject it into a component:
22+
```js
23+
import { StorageService } from 'ng2-storage';
24+
25+
@Component({
26+
providers: [StorageService],
27+
template: `<button (click)="incrementStoredData()">click</button>`
28+
})
29+
export class MyComponent {
30+
31+
static get parameters() {
32+
return [[StorageService]];
33+
}
34+
35+
constructor(storage) {
36+
// you can also use storage.session for sessionStorage
37+
this.storage = storage.local;
38+
}
39+
40+
incrementStoredData() {
41+
this.storage.data = this.storage.data || 0;
42+
this.storage.data++;
43+
}
44+
}
45+
```
46+
47+
# Options
48+
Name | Default | Description
49+
---- | ------- | -----------
50+
prefix | 'ng2-storage' | The key prefix when assigning data to local or session storage.
51+
serialize | window.JSON | Used when de/serializing data from the storage container. Both `serialize` and `parse` attributes must be specified and must be functions if you want custom ones.

index.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
import { window } from '@angular/platform-browser/src/facade/browser';
3+
import { Json } from '@angular/platform-browser/src/facade/lang';
4+
5+
const storageAvailable = (type) => {
6+
try {
7+
const storage = window[type];
8+
const test = '__storage_test__';
9+
storage.setItem(test, test);
10+
storage.removeItem(test);
11+
return true;
12+
}
13+
catch(e) {
14+
return false;
15+
}
16+
};
17+
18+
const buildProxy = (type, { prefix, serialize }) => {
19+
const service = window[type];
20+
21+
const proxyData = {
22+
get: (target, key) => {
23+
24+
// || null to prevent undefined errors
25+
return serialize.parse(target[`${prefix}-${key}`] || null);
26+
},
27+
28+
set: (target, key, value) => {
29+
return target[`${prefix}-${key}`] = serialize.stringify(value);
30+
}
31+
};
32+
33+
return new Proxy(service, proxyData);
34+
};
35+
36+
export class StorageSettings {
37+
constructor() {}
38+
}
39+
40+
41+
export class StorageService {
42+
static get parameters() {
43+
return [[StorageSettings]];
44+
}
45+
46+
constructor(storageSettings) {
47+
this.storageSettings = Object.assign({ prefix: 'ng2-storage', serialize: Json }, storageSettings);
48+
if(typeof this.storageSettings.prefix === 'undefined') {
49+
throw new Error('storageSettings.prefix must be a string');
50+
}
51+
52+
if(!this.storageSettings.serialize) {
53+
throw new Error('storageSettings.serialize must be an object { stringify, parse }');
54+
}
55+
56+
if(typeof this.storageSettings.serialize.stringify !== 'function') {
57+
throw new Error('storageSettings.serialize.stringify must be a function');
58+
}
59+
60+
if(typeof this.storageSettings.serialize.parse !== 'function') {
61+
throw new Error('storageSettings.serialize.parse must be a function');
62+
}
63+
64+
if(!storageAvailable('localStorage')) {
65+
console.warn('localStorage is not available!');
66+
} else {
67+
this.local = buildProxy('localStorage', this.storageSettings);
68+
}
69+
70+
if(!storageAvailable('sessionStorage')) {
71+
console.warn('sessionStorage is not available!');
72+
} else {
73+
this.session = buildProxy('sessionStorage', this.storageSettings);
74+
}
75+
76+
}
77+
}

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "ng2-storage",
3+
"version": "0.0.1",
4+
"description": "A service wrapping local and session storage for angular2.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/seiyria/ng2-sweetalert2.git"
12+
},
13+
"keywords": [
14+
"ng2",
15+
"angular2",
16+
"localstorage",
17+
"sessionstorage",
18+
"storage",
19+
"ng2-storage",
20+
"angular2-storage"
21+
],
22+
"author": "Kyle Kemp <kyle@seiyria.com>",
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "https://github.com/seiyria/ng2-storage/issues"
26+
},
27+
"homepage": "https://github.com/seiyria/ng2-storage#readme",
28+
"peerDependencies": {
29+
"@angular/platform-browser": "2.0.0-rc.1"
30+
}
31+
}

0 commit comments

Comments
 (0)