-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
store.js
187 lines (182 loc) · 5.19 KB
/
store.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
/**!
* storejs v2.0.7
* Local storage localstorage package provides a simple API
*
* Copyright (c) 2024 kenny wang <wowohoo@qq.com>
* https://jaywcjlove.github.io/store.js/
*
* Licensed under the MIT license.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.store = factory());
})(this, (function () { 'use strict';
function isJSON(obj) {
obj = JSON.stringify(obj);
if (!/^\{[\s\S]*\}$/.test(obj)) {
return false;
}
return true;
}
function stringify(val) {
return val === undefined || typeof val === "function" ? val + '' : JSON.stringify(val);
}
function deserialize(value) {
if (typeof value !== 'string') {
return undefined;
}
try {
return JSON.parse(value);
} catch (e) {
return value;
}
}
function isFunction(value) {
return {}.toString.call(value) === "[object Function]";
}
function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
}
// https://github.com/jaywcjlove/store.js/pull/8
// Error: QuotaExceededError
function dealIncognito(storage) {
var _KEY = '_Is_Incognit',
_VALUE = 'yes';
try {
// NOTE: set default storage when not passed in
if (!storage) {
storage = window.localStorage;
}
storage.setItem(_KEY, _VALUE);
storage.removeItem(_KEY);
} catch (e) {
var inMemoryStorage = {};
inMemoryStorage._data = {};
inMemoryStorage.setItem = function (id, val) {
return inMemoryStorage._data[id] = String(val);
};
inMemoryStorage.getItem = function (id) {
return inMemoryStorage._data.hasOwnProperty(id) ? inMemoryStorage._data[id] : undefined;
};
inMemoryStorage.removeItem = function (id) {
return delete inMemoryStorage._data[id];
};
inMemoryStorage.clear = function () {
return inMemoryStorage._data = {};
};
storage = inMemoryStorage;
} finally {
if (storage.getItem(_KEY) === _VALUE) storage.removeItem(_KEY);
}
return storage;
}
// deal QuotaExceededError if user use incognito mode in browser
var storage = dealIncognito();
function Store() {
if (!(this instanceof Store)) {
return new Store();
}
}
Store.prototype = {
set: function set(key, val) {
if (key && !isJSON(key)) {
storage.setItem(key, stringify(val));
} else if (isJSON(key)) {
for (var a in key) this.set(a, key[a]);
}
return this;
},
get: function get(key) {
// Return all entries if no key
if (key === undefined) {
var ret = {};
this.forEach(function (key, val) {
return ret[key] = val;
});
return ret;
}
if (key.charAt(0) === '?') {
return this.has(key.substr(1));
}
var args = arguments;
if (args.length > 1) {
var dt = {};
for (var i = 0, len = args.length; i < len; i++) {
var value = deserialize(storage.getItem(args[i]));
if (this.has(args[i])) {
dt[args[i]] = value;
}
}
return dt;
}
return deserialize(storage.getItem(key));
},
clear: function clear() {
storage.clear();
return this;
},
remove: function remove(key) {
var val = this.get(key);
storage.removeItem(key);
return val;
},
has: function has(key) {
return {}.hasOwnProperty.call(this.get(), key);
},
keys: function keys() {
var d = [];
this.forEach(function (k) {
d.push(k);
});
return d;
},
forEach: function forEach(callback) {
for (var i = 0, len = storage.length; i < len; i++) {
var key = storage.key(i);
callback(key, this.get(key));
}
return this;
},
search: function search(str) {
var arr = this.keys(),
dt = {};
for (var i = 0, len = arr.length; i < len; i++) {
if (arr[i].indexOf(str) > -1) dt[arr[i]] = this.get(arr[i]);
}
return dt;
},
len: function len() {
return storage.length;
}
};
var _Store = null;
function store(key, data) {
var argm = arguments;
var dt = null;
if (!_Store) _Store = Store();
if (argm.length === 0) return _Store.get();
if (argm.length === 1) {
if (typeof key === "string") return _Store.get(key);
if (isJSON(key)) return _Store.set(key);
}
if (argm.length === 2 && typeof key === "string") {
if (!data) return _Store.remove(key);
if (data && typeof data === "string") return _Store.set(key, data);
if (data && isFunction(data)) {
dt = null;
dt = data(key, _Store.get(key));
store.set(key, dt);
}
}
if (argm.length === 2 && isArray(key) && isFunction(data)) {
for (var i = 0, len = key.length; i < len; i++) {
dt = data(key[i], _Store.get(key[i]));
store.set(key[i], dt);
}
}
return store;
}
for (var a in Store.prototype) store[a] = Store.prototype[a];
return store;
}));