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

Commit 7f5356a

Browse files
committed
compile ng2-storage
1 parent 28b3bc3 commit 7f5356a

File tree

3 files changed

+134
-33
lines changed

3 files changed

+134
-33
lines changed

index.js

+50-32
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,95 @@
1+
'use strict';
12

2-
import { window } from '@angular/platform-browser/src/facade/browser';
3-
import { Json } from '@angular/platform-browser/src/facade/lang';
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.StorageService = exports.StorageSettings = undefined;
47

5-
const storageAvailable = (type) => {
8+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
9+
10+
var _browser = require('@angular/platform-browser/src/facade/browser');
11+
12+
var _lang = require('@angular/platform-browser/src/facade/lang');
13+
14+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
15+
16+
var storageAvailable = function storageAvailable(type) {
617
try {
7-
const storage = window[type];
8-
const test = '__storage_test__';
18+
var storage = _browser.window[type];
19+
var test = '__storage_test__';
920
storage.setItem(test, test);
1021
storage.removeItem(test);
1122
return true;
12-
}
13-
catch(e) {
23+
} catch (e) {
1424
return false;
1525
}
1626
};
1727

18-
const buildProxy = (type, { prefix, serialize }) => {
19-
const service = window[type];
28+
var buildProxy = function buildProxy(type, _ref) {
29+
var prefix = _ref.prefix;
30+
var serialize = _ref.serialize;
31+
32+
var service = _browser.window[type];
2033

21-
const proxyData = {
22-
get: (target, key) => {
34+
var proxyData = {
35+
get: function get(target, key) {
2336

2437
// || null to prevent undefined errors
25-
return serialize.parse(target[`${prefix}-${key}`] || null);
38+
return serialize.parse(target[prefix + '-' + key] || null);
2639
},
2740

28-
set: (target, key, value) => {
29-
return target[`${prefix}-${key}`] = serialize.stringify(value);
41+
set: function set(target, key, value) {
42+
return target[prefix + '-' + key] = serialize.stringify(value);
3043
}
3144
};
3245

3346
return new Proxy(service, proxyData);
3447
};
3548

36-
export class StorageSettings {
37-
constructor() {}
38-
}
49+
var StorageSettings = exports.StorageSettings = function StorageSettings() {
50+
_classCallCheck(this, StorageSettings);
51+
};
3952

53+
var StorageService = exports.StorageService = function () {
54+
_createClass(StorageService, null, [{
55+
key: 'parameters',
56+
get: function get() {
57+
return [[StorageSettings]];
58+
}
59+
}]);
4060

41-
export class StorageService {
42-
static get parameters() {
43-
return [[StorageSettings]];
44-
}
61+
function StorageService(storageSettings) {
62+
_classCallCheck(this, StorageService);
4563

46-
constructor(storageSettings) {
47-
this.storageSettings = Object.assign({ prefix: 'ng2-storage', serialize: Json }, storageSettings);
48-
if(typeof this.storageSettings.prefix === 'undefined') {
64+
this.storageSettings = Object.assign({ prefix: 'ng2-storage', serialize: _lang.Json }, storageSettings);
65+
if (typeof this.storageSettings.prefix === 'undefined') {
4966
throw new Error('storageSettings.prefix must be a string');
5067
}
5168

52-
if(!this.storageSettings.serialize) {
69+
if (!this.storageSettings.serialize) {
5370
throw new Error('storageSettings.serialize must be an object { stringify, parse }');
5471
}
5572

56-
if(typeof this.storageSettings.serialize.stringify !== 'function') {
73+
if (typeof this.storageSettings.serialize.stringify !== 'function') {
5774
throw new Error('storageSettings.serialize.stringify must be a function');
5875
}
5976

60-
if(typeof this.storageSettings.serialize.parse !== 'function') {
77+
if (typeof this.storageSettings.serialize.parse !== 'function') {
6178
throw new Error('storageSettings.serialize.parse must be a function');
6279
}
6380

64-
if(!storageAvailable('localStorage')) {
81+
if (!storageAvailable('localStorage')) {
6582
console.warn('localStorage is not available!');
6683
} else {
67-
this.local = buildProxy('localStorage', this.storageSettings);
84+
this.local = buildProxy('localStorage', this.storageSettings);
6885
}
6986

70-
if(!storageAvailable('sessionStorage')) {
87+
if (!storageAvailable('sessionStorage')) {
7188
console.warn('sessionStorage is not available!');
7289
} else {
7390
this.session = buildProxy('sessionStorage', this.storageSettings);
7491
}
75-
7692
}
77-
}
93+
94+
return StorageService;
95+
}();

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng2-storage",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A service wrapping local and session storage for angular2.",
55
"main": "index.js",
66
"scripts": {
@@ -24,8 +24,14 @@
2424
"bugs": {
2525
"url": "https://github.com/seiyria/ng2-storage/issues"
2626
},
27+
"scripts": {
28+
"compile": "babel src/index.js -o index.js --presets es2015"
29+
},
2730
"homepage": "https://github.com/seiyria/ng2-storage#readme",
2831
"peerDependencies": {
2932
"@angular/platform-browser": "2.0.0-rc.1"
33+
},
34+
"devDependencies": {
35+
"babel-preset-es2015": "^6.9.0"
3036
}
3137
}

src/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+
}

0 commit comments

Comments
 (0)