-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtz-storage.js
215 lines (188 loc) · 5.8 KB
/
tz-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* tz-storage v1.0.7
* (c) 2019 Tianzhen mecoepcoo@vip.qq.com
* @license MIT
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.storage = factory());
}(this, function () { 'use strict';
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a 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);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
if (staticProps) _defineProperties(Constructor, staticProps);
return Constructor;
}
var Storage =
/*#__PURE__*/
function () {
function Storage() {
_classCallCheck(this, Storage);
this._namespace = '';
this._defaultValue = null;
}
/**
* check supported getter
* @readonly
* @memberof Storage
*/
_createClass(Storage, [{
key: "config",
/**
* init config
* @memberof Storage
*/
value: function config() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$namespace = _ref.namespace,
namespace = _ref$namespace === void 0 ? '' : _ref$namespace,
_ref$defaultValue = _ref.defaultValue,
defaultValue = _ref$defaultValue === void 0 ? null : _ref$defaultValue;
this.namespace = namespace;
this.defaultValue = defaultValue;
}
/**
* set localstorage value
* @param {string} key
* @param {string} value
* @param {number} [expire] expire timestamp, pass 0 to disable expire
* @returns
* @memberof Storage
*/
}, {
key: "set",
value: function set(key, value) {
var expire = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
if (!this._checkSupported) return false;
key = this._keyHandle(key);
value = JSON.stringify({
data: value,
expire: expire
});
window.localStorage.setItem(key, value);
return true;
}
/**
* get localstorage value
* @param {string} key
* @param {object} options
* @param {any} options.defaultValue returned when the value is empty
* @param {string=["string","number","boolean"]} options.type parse type of the value
*/
}, {
key: "get",
value: function get(key) {
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_ref2$defaultValue = _ref2.defaultValue,
defaultValue = _ref2$defaultValue === void 0 ? this._defaultValue : _ref2$defaultValue,
type = _ref2.type;
if (!this._checkSupported) return defaultValue;
key = this._keyHandle(key);
var _value = JSON.parse(window.localStorage.getItem(key));
if (_value === null) return defaultValue;
var data = _value.data,
expire = _value.expire;
var now = new Date().getTime();
if (expire && expire < now) {
window.localStorage.removeItem(key);
return defaultValue;
}
try {
if (type && ['number', 'string', 'boolean'].indexOf(type) < 0) throw "Type Error: unsupported type param.";
} catch (e) {
console.error(e);
return false;
}
switch (type) {
case 'string':
data = "".concat(data);
break;
case 'number':
data = Number(data);
break;
case 'boolean':
data = Boolean(data);
break;
default:
break;
}
return data === null ? defaultValue : data;
}
/**
* remove the specified localstorage
* @param {string} key
*/
}, {
key: "remove",
value: function remove(key) {
if (!this._checkSupported) return false;
key = this._keyHandle(key);
window.localStorage.removeItem(key);
return true;
}
}, {
key: "_keyHandle",
value: function _keyHandle(key) {
return this._namespace + key;
}
}, {
key: "create",
value: function create() {
return new Storage();
}
}, {
key: "_checkSupported",
get: function get() {
var isSupported = window.localStorage && (window.localStorage.setItem('_testStorage', 'storage'), window.localStorage.getItem('_testStorage') === 'storage') ? true : false;
window.localStorage && window.localStorage.removeItem('_testStorage');
return isSupported;
}
}, {
key: "isSupported",
get: function get() {
return this._checkSupported;
}
/**
* namespace getter
* @memberof Storage
* @returns {string}
*/
}, {
key: "namespace",
get: function get() {
return this._namespace;
}
/**
* namespace setter
* @memberof Storage
*/
,
set: function set(value) {
this._namespace = value ? "".concat(value, ".") : '';
}
}, {
key: "defaultValue",
set: function set(value) {
this._defaultValue = value;
}
}]);
return Storage;
}();
var index = new Storage();
return index;
}));
//# sourceMappingURL=tz-storage.js.map