util-helpers 5.1.3 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/util-helpers.js +681 -72
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/VERSION.js +1 -1
- package/esm/getFileType.js +2 -2
- package/esm/getMimeType.js +43 -0
- package/esm/index.js +1 -0
- package/lib/VERSION.js +1 -1
- package/lib/getFileType.js +2 -2
- package/lib/getMimeType.js +45 -0
- package/lib/index.js +2 -0
- package/package.json +18 -18
- package/types/AsyncMemo.d.ts +1 -1
- package/types/BlobUrl.d.ts +41 -0
- package/types/ajax.d.ts +2 -2
- package/types/calculateCursorPosition.d.ts +2 -2
- package/types/checkFileType.d.ts +2 -2
- package/types/compressImage.d.ts +1 -1
- package/types/dataURLToBlob.d.ts +1 -1
- package/types/download.d.ts +3 -3
- package/types/fileReader.d.ts +1 -1
- package/types/gcd.d.ts +1 -1
- package/types/getFileType.d.ts +1 -1
- package/types/getMimeType.d.ts +25 -0
- package/types/index.d.ts +3 -2
- package/types/isBankCard.d.ts +1 -1
- package/types/isBusinessLicense.d.ts +1 -1
- package/types/isChinese.d.ts +3 -3
- package/types/isHMCard.d.ts +1 -1
- package/types/isIdCard.d.ts +2 -2
- package/types/isPassport.d.ts +1 -1
- package/types/isPassword.d.ts +1 -1
- package/types/isSocialCreditCode.d.ts +1 -1
- package/types/isSwiftCode.d.ts +1 -1
- package/types/isTWCard.d.ts +1 -1
- package/types/isUrl.d.ts +1 -1
- package/types/isVehicle.d.ts +1 -1
- package/types/lcm.d.ts +1 -1
- package/types/numberToChinese.d.ts +1 -1
- package/types/parseIdCard.d.ts +1 -1
- package/types/safeDate.d.ts +1 -1
- package/types/setDataURLPrefix.d.ts +2 -2
- package/types/validatePassword.d.ts +1 -1
package/dist/util-helpers.js
CHANGED
|
@@ -107,6 +107,15 @@
|
|
|
107
107
|
return value != null && isLength(value.length) && !isFunction(value);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
var nth = function (array, n) {
|
|
111
|
+
if (n === void 0) { n = 0; }
|
|
112
|
+
if (!isArrayLike(array)) {
|
|
113
|
+
return nativeUndefined$1;
|
|
114
|
+
}
|
|
115
|
+
n += n < 0 ? array.length : 0;
|
|
116
|
+
return array[n];
|
|
117
|
+
};
|
|
118
|
+
|
|
110
119
|
var MAX_VALUE = 1.7976931348623157e308;
|
|
111
120
|
function toFinite(value) {
|
|
112
121
|
if (!value) {
|
|
@@ -689,7 +698,7 @@
|
|
|
689
698
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
690
699
|
PERFORMANCE OF THIS SOFTWARE.
|
|
691
700
|
***************************************************************************** */
|
|
692
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
701
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
693
702
|
|
|
694
703
|
var extendStatics = function(d, b) {
|
|
695
704
|
extendStatics = Object.setPrototypeOf ||
|
|
@@ -740,8 +749,8 @@
|
|
|
740
749
|
}
|
|
741
750
|
|
|
742
751
|
function __generator(thisArg, body) {
|
|
743
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
744
|
-
return g =
|
|
752
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
753
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
745
754
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
746
755
|
function step(op) {
|
|
747
756
|
if (f) throw new TypeError("Generator is already executing.");
|
|
@@ -1725,8 +1734,8 @@
|
|
|
1725
1734
|
|
|
1726
1735
|
var config = {
|
|
1727
1736
|
image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
|
|
1728
|
-
audio: 'audio/*,.mp3,.wav',
|
|
1729
|
-
video: 'video/*,.mp4,.webm,.ogg
|
|
1737
|
+
audio: 'audio/*,.mp3,.wav,.aac,.flac',
|
|
1738
|
+
video: 'video/*,.mp4,.webm,.ogg',
|
|
1730
1739
|
pdf: 'application/pdf,.pdf',
|
|
1731
1740
|
word: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,.doc,.docx',
|
|
1732
1741
|
excel: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.xls,.xlsx'
|
|
@@ -1765,6 +1774,45 @@
|
|
|
1765
1774
|
});
|
|
1766
1775
|
}
|
|
1767
1776
|
|
|
1777
|
+
var mimeTypes = [
|
|
1778
|
+
['text/plain', ['txt']],
|
|
1779
|
+
['text/css', ['css']],
|
|
1780
|
+
['text/html', ['htm', 'html']],
|
|
1781
|
+
['text/javascript', ['js', 'mjs']],
|
|
1782
|
+
['text/csv', ['csv']],
|
|
1783
|
+
['text/markdown', ['md', 'markdown']],
|
|
1784
|
+
['image/gif', ['gif']],
|
|
1785
|
+
['image/jpeg', ['jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp']],
|
|
1786
|
+
['image/png', ['png']],
|
|
1787
|
+
['image/svg+xml', ['svg']],
|
|
1788
|
+
['image/webp', ['webp']],
|
|
1789
|
+
['image/apng', ['apng']],
|
|
1790
|
+
['image/avif', ['avif']],
|
|
1791
|
+
['image/bmp', ['bmp']],
|
|
1792
|
+
['image/x-icon', ['ico', 'cur']],
|
|
1793
|
+
['image/tiff', ['tif', 'tiff']],
|
|
1794
|
+
['application/xml', ['xml']],
|
|
1795
|
+
['application/zip', ['zip']],
|
|
1796
|
+
['application/pdf', ['pdf']],
|
|
1797
|
+
['application/json', ['json']],
|
|
1798
|
+
['application/yaml', ['yaml', 'yml']],
|
|
1799
|
+
['application/vnd.openxmlformats-officedocument.wordprocessingml.document', ['doc', 'docx']],
|
|
1800
|
+
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ['xls', 'xlsx']],
|
|
1801
|
+
['audio/mp3', ['mp3']],
|
|
1802
|
+
['audio/wav', ['wav']],
|
|
1803
|
+
['audio/aac', ['aac']],
|
|
1804
|
+
['audio/flac', ['flac']],
|
|
1805
|
+
['video/mp4', ['mp4']],
|
|
1806
|
+
['video/ogg', ['ogg']],
|
|
1807
|
+
['video/webm', ['webm']],
|
|
1808
|
+
['video/quicktime', ['mov']]
|
|
1809
|
+
];
|
|
1810
|
+
function getMimeType(fileName) {
|
|
1811
|
+
var _a;
|
|
1812
|
+
var ext = isString(fileName) ? nth(fileName.split('.'), -1) : '';
|
|
1813
|
+
return ext ? (_a = mimeTypes.find(function (item) { return item[1].includes(ext); })) === null || _a === void 0 ? void 0 : _a[0] : nativeUndefined;
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1768
1816
|
function loadScript(src, options) {
|
|
1769
1817
|
return new Promise(function (resolve, reject) {
|
|
1770
1818
|
var head = document.head;
|
|
@@ -2076,26 +2124,111 @@
|
|
|
2076
2124
|
return internalFindTreeSelect(tree, predicate, childrenField);
|
|
2077
2125
|
}
|
|
2078
2126
|
|
|
2079
|
-
var VERSION = "5.
|
|
2080
|
-
|
|
2127
|
+
var VERSION = "5.2.0";
|
|
2128
|
+
|
|
2129
|
+
/**
|
|
2130
|
+
* 事件触发器,支持浏览器端和 node 端。
|
|
2131
|
+
*
|
|
2132
|
+
* @class
|
|
2133
|
+
* @example
|
|
2134
|
+
*
|
|
2135
|
+
* const emitter = new Emitter();
|
|
2136
|
+
*
|
|
2137
|
+
* // 注册监听方法
|
|
2138
|
+
* emitter.on('foo', () => console.log('foo 1'));
|
|
2139
|
+
* emitter.on('foo', () => console.log('foo 2'));
|
|
2140
|
+
*
|
|
2141
|
+
* // 触发方法
|
|
2142
|
+
* emitter.emit('foo');
|
|
2143
|
+
* // foo 1
|
|
2144
|
+
* // foo 2
|
|
2145
|
+
*
|
|
2146
|
+
* // 取消监听方法
|
|
2147
|
+
* emitter.off('foo');
|
|
2148
|
+
*
|
|
2149
|
+
* // 支持链式调用
|
|
2150
|
+
* emitter.on('foo', () => {})
|
|
2151
|
+
* .on('foo', () => {})
|
|
2152
|
+
* .off('foo');
|
|
2153
|
+
*/
|
|
2081
2154
|
var EmitterPro = /** @class */ (function () {
|
|
2082
2155
|
function EmitterPro() {
|
|
2083
2156
|
this.handlers = {};
|
|
2084
2157
|
}
|
|
2158
|
+
/**
|
|
2159
|
+
* 获取全部事件名称。
|
|
2160
|
+
*
|
|
2161
|
+
* @returns 事件名称数组。
|
|
2162
|
+
* @example
|
|
2163
|
+
* emitter.on('foo', () => {});
|
|
2164
|
+
* emitter.on('bar', () => {});
|
|
2165
|
+
*
|
|
2166
|
+
* emitter.eventNames(); // ['foo', 'bar']
|
|
2167
|
+
*/
|
|
2085
2168
|
EmitterPro.prototype.eventNames = function () {
|
|
2086
2169
|
var _a;
|
|
2087
2170
|
var symbols = ((_a = Object.getOwnPropertySymbols) === null || _a === void 0 ? void 0 : _a.call(Object, this.handlers)) || [];
|
|
2088
2171
|
var keys = Object.keys(this.handlers);
|
|
2089
2172
|
return keys.concat(symbols);
|
|
2090
2173
|
};
|
|
2174
|
+
/**
|
|
2175
|
+
* 获取事件名称的全部监听方法(原始方法,未经过包装处理)。
|
|
2176
|
+
*
|
|
2177
|
+
* @param eventName 事件名称
|
|
2178
|
+
* @returns 对应事件名称的监听方法数组
|
|
2179
|
+
* @example
|
|
2180
|
+
* const fn1 = () => console.log('bar');
|
|
2181
|
+
* const fn2 = () => console.log('baz');
|
|
2182
|
+
*
|
|
2183
|
+
* emitter.on('test', fn1);
|
|
2184
|
+
* emitter.once('test', fn2);
|
|
2185
|
+
*
|
|
2186
|
+
* emitter.rawListeners('test'); // [fn1, fn2]
|
|
2187
|
+
*/
|
|
2091
2188
|
EmitterPro.prototype.rawListeners = function (eventName) {
|
|
2092
2189
|
var handler = this.handlers[eventName];
|
|
2093
2190
|
return handler ? handler.map(function (item) { return item.raw; }) : [];
|
|
2094
2191
|
};
|
|
2192
|
+
/**
|
|
2193
|
+
* 获取事件名称的全部监听方法(如通过 `once` 方法注册,返回的是包装方法)。
|
|
2194
|
+
*
|
|
2195
|
+
* @param eventName 事件名称
|
|
2196
|
+
* @returns 对应事件名称的监听方法数组
|
|
2197
|
+
* @example
|
|
2198
|
+
* const fn1 = () => console.log('bar');
|
|
2199
|
+
* const fn2 = () => console.log('baz');
|
|
2200
|
+
*
|
|
2201
|
+
* emitter.on('test', fn1);
|
|
2202
|
+
* emitter.once('test', fn2);
|
|
2203
|
+
*
|
|
2204
|
+
* emitter.rawListeners('test'); // [fn1, wrapFn2]
|
|
2205
|
+
*/
|
|
2095
2206
|
EmitterPro.prototype.listeners = function (eventName) {
|
|
2096
2207
|
var handler = this.handlers[eventName];
|
|
2097
2208
|
return handler ? handler.map(function (item) { return item.wrap; }) : [];
|
|
2098
2209
|
};
|
|
2210
|
+
/**
|
|
2211
|
+
* 判断事件名称对应的监听方法是否存在。
|
|
2212
|
+
*
|
|
2213
|
+
* @param eventName 事件名称
|
|
2214
|
+
* @param listener 监听方法
|
|
2215
|
+
* @returns 如果事件名称存在该事件方法返回 `true`,否则返回 `false`。
|
|
2216
|
+
* @example
|
|
2217
|
+
* const fn1 = () => console.log('bar');
|
|
2218
|
+
* const fn2 = () => console.log('baz');
|
|
2219
|
+
*
|
|
2220
|
+
* emitter.on('test', fn1);
|
|
2221
|
+
* emitter.once('test', fn2);
|
|
2222
|
+
*
|
|
2223
|
+
* emitter.hasListener('test', fn1); // true
|
|
2224
|
+
* emitter.hasListener('test', fn2); // true
|
|
2225
|
+
*
|
|
2226
|
+
* // fn2 是通过 once 方法注册,执行一次后自动解绑
|
|
2227
|
+
* emitter.emit('test');
|
|
2228
|
+
*
|
|
2229
|
+
* emitter.hasListener('test', fn1); // true
|
|
2230
|
+
* emitter.hasListener('test', fn2); // false
|
|
2231
|
+
*/
|
|
2099
2232
|
EmitterPro.prototype.hasListener = function (eventName, listener) {
|
|
2100
2233
|
return this.rawListeners(eventName).some(function (item) { return item === listener; });
|
|
2101
2234
|
};
|
|
@@ -2112,9 +2245,39 @@
|
|
|
2112
2245
|
}
|
|
2113
2246
|
return this;
|
|
2114
2247
|
};
|
|
2248
|
+
/**
|
|
2249
|
+
* 注册监听方法。同 `on` 方法,只是将监听方法添加到最前面(事件触发是按顺序执行)。
|
|
2250
|
+
*
|
|
2251
|
+
* @param eventName 事件名称
|
|
2252
|
+
* @param listener 监听方法
|
|
2253
|
+
* @param context 执行上下文
|
|
2254
|
+
* @returns 事件触发器实例。
|
|
2255
|
+
* @example
|
|
2256
|
+
* emitter.on('foo', () => console.log('bar'));
|
|
2257
|
+
* emitter.prependListener('foo', () => console.log(42));
|
|
2258
|
+
*
|
|
2259
|
+
* emitter.emit('foo');
|
|
2260
|
+
* // 42
|
|
2261
|
+
* // bar
|
|
2262
|
+
*/
|
|
2115
2263
|
EmitterPro.prototype.prependListener = function (eventName, listener, context) {
|
|
2116
2264
|
return this._on(eventName, listener, listener, context, 0);
|
|
2117
2265
|
};
|
|
2266
|
+
/**
|
|
2267
|
+
* 注册监听方法。允许多次添加同一引用的函数。
|
|
2268
|
+
*
|
|
2269
|
+
* @param eventName 事件名称
|
|
2270
|
+
* @param listener 监听方法
|
|
2271
|
+
* @param context 执行上下文
|
|
2272
|
+
* @returns 事件触发器实例。
|
|
2273
|
+
* @example
|
|
2274
|
+
* emitter.on('foo', () => console.log('bar'));
|
|
2275
|
+
* emitter.on('foo', () => console.log(42));
|
|
2276
|
+
*
|
|
2277
|
+
* emitter.emit('foo');
|
|
2278
|
+
* // bar
|
|
2279
|
+
* // 42
|
|
2280
|
+
*/
|
|
2118
2281
|
EmitterPro.prototype.on = function (eventName, listener, context) {
|
|
2119
2282
|
return this._on(eventName, listener, listener, context);
|
|
2120
2283
|
};
|
|
@@ -2131,14 +2294,76 @@
|
|
|
2131
2294
|
});
|
|
2132
2295
|
return wrap;
|
|
2133
2296
|
};
|
|
2297
|
+
/**
|
|
2298
|
+
* 仅触发一次的监听方法。使用方法同 `on` 。
|
|
2299
|
+
*
|
|
2300
|
+
* @param eventName 事件名称
|
|
2301
|
+
* @param listener 监听方法
|
|
2302
|
+
* @param context 执行上下文
|
|
2303
|
+
* @returns 事件触发器实例。
|
|
2304
|
+
* @example
|
|
2305
|
+
* emitter.on('foo', () => console.log('bar'));
|
|
2306
|
+
* emitter.once('foo', () => console.log(42));
|
|
2307
|
+
*
|
|
2308
|
+
* emitter.emit('foo');
|
|
2309
|
+
* // bar
|
|
2310
|
+
* // 42
|
|
2311
|
+
*
|
|
2312
|
+
* emitter.emit('foo');
|
|
2313
|
+
* // bar
|
|
2314
|
+
*/
|
|
2134
2315
|
EmitterPro.prototype.once = function (eventName, listener, context) {
|
|
2135
2316
|
var wrap = this._wrapOnce(eventName, listener, context);
|
|
2136
2317
|
return this._on(eventName, listener, wrap, context);
|
|
2137
2318
|
};
|
|
2319
|
+
/**
|
|
2320
|
+
* 仅触发一次的监听方法。同 `once` 方法,只是添加到最前面(事件触发是按顺序执行)。
|
|
2321
|
+
*
|
|
2322
|
+
* @param eventName 事件名称
|
|
2323
|
+
* @param listener 监听方法
|
|
2324
|
+
* @param context 执行上下文
|
|
2325
|
+
* @returns 事件触发器实例。
|
|
2326
|
+
* @example
|
|
2327
|
+
* emitter.on('foo', () => console.log('bar'));
|
|
2328
|
+
* emitter.prependOnceListener('foo', () => console.log(42));
|
|
2329
|
+
*
|
|
2330
|
+
* emitter.emit('foo');
|
|
2331
|
+
* // 42
|
|
2332
|
+
* // bar
|
|
2333
|
+
*
|
|
2334
|
+
* emitter.emit('foo');
|
|
2335
|
+
* // bar
|
|
2336
|
+
*/
|
|
2138
2337
|
EmitterPro.prototype.prependOnceListener = function (eventName, listener, context) {
|
|
2139
2338
|
var wrap = this._wrapOnce(eventName, listener, context);
|
|
2140
2339
|
return this._on(eventName, listener, wrap, context, 0);
|
|
2141
2340
|
};
|
|
2341
|
+
/**
|
|
2342
|
+
* 取消监听方法。如果不传第二个参数,将取消该事件名称的全部监听方法。如果多次添加同一引用的函数,需要多次删除。
|
|
2343
|
+
*
|
|
2344
|
+
* @param eventName 事件名称
|
|
2345
|
+
* @param listener 监听方法
|
|
2346
|
+
* @returns 事件触发器实例。
|
|
2347
|
+
* @example
|
|
2348
|
+
* const fn = () => console.log('bar');
|
|
2349
|
+
* emitter.on('foo', fn);
|
|
2350
|
+
* emitter.on('foo', () => console.log('baz'));
|
|
2351
|
+
* emitter.on('foo', () => console.log(42));
|
|
2352
|
+
*
|
|
2353
|
+
* emitter.emit('foo');
|
|
2354
|
+
* // bar
|
|
2355
|
+
* // baz
|
|
2356
|
+
* // 42
|
|
2357
|
+
*
|
|
2358
|
+
* emitter.off('foo', fn); // 取消 foo 的监听方法 fn
|
|
2359
|
+
*
|
|
2360
|
+
* emitter.emit('foo');
|
|
2361
|
+
* // bar
|
|
2362
|
+
* // 42
|
|
2363
|
+
*
|
|
2364
|
+
* emitter.off('foo'); // 取消 foo 的全部监听方法
|
|
2365
|
+
* emitter.emit('foo'); // 什么都没发生
|
|
2366
|
+
*/
|
|
2142
2367
|
EmitterPro.prototype.off = function (eventName, listener) {
|
|
2143
2368
|
var handler = this.handlers[eventName];
|
|
2144
2369
|
if (handler) {
|
|
@@ -2154,10 +2379,63 @@
|
|
|
2154
2379
|
}
|
|
2155
2380
|
return this;
|
|
2156
2381
|
};
|
|
2382
|
+
/**
|
|
2383
|
+
* 取消全部事件名称的监听方法。
|
|
2384
|
+
*
|
|
2385
|
+
* @returns 事件触发器实例。
|
|
2386
|
+
* @example
|
|
2387
|
+
* const fn = () => console.log('bar');
|
|
2388
|
+
* emitter.on('test', fn);
|
|
2389
|
+
* emitter.on('test', () => console.log('baz'));
|
|
2390
|
+
* emitter.on('test', () => console.log(42));
|
|
2391
|
+
*
|
|
2392
|
+
* emitter.on('other', fn);
|
|
2393
|
+
* emitter.on('other', () => console.log('baz'));
|
|
2394
|
+
*
|
|
2395
|
+
* emitter.emit('test');
|
|
2396
|
+
* // bar
|
|
2397
|
+
* // baz
|
|
2398
|
+
* // 42
|
|
2399
|
+
*
|
|
2400
|
+
* emitter.emit('other');
|
|
2401
|
+
* // bar
|
|
2402
|
+
* // baz
|
|
2403
|
+
*
|
|
2404
|
+
* emitter.offAll(); // 取消全部监听方法
|
|
2405
|
+
*
|
|
2406
|
+
* emitter.emit('test'); // 什么都没发生
|
|
2407
|
+
* emitter.emit('other'); // 什么都没发生
|
|
2408
|
+
*/
|
|
2157
2409
|
EmitterPro.prototype.offAll = function () {
|
|
2158
2410
|
this.handlers = {};
|
|
2159
2411
|
return this;
|
|
2160
2412
|
};
|
|
2413
|
+
/**
|
|
2414
|
+
* 触发监听方法。
|
|
2415
|
+
*
|
|
2416
|
+
* @param eventName 事件名称
|
|
2417
|
+
* @param args 触发监听方法的参数(从第二个参数开始都将传给监听方法)
|
|
2418
|
+
* @returns 如果触发成功返回 `true`,否则返回 `false`。
|
|
2419
|
+
* @example
|
|
2420
|
+
* emitter.on('foo', () => console.log('bar'));
|
|
2421
|
+
* emitter.on('foo', () => console.log(42));
|
|
2422
|
+
*
|
|
2423
|
+
* emitter.emit('foo');
|
|
2424
|
+
* // bar
|
|
2425
|
+
* // 42
|
|
2426
|
+
*
|
|
2427
|
+
* // 支持传入参数
|
|
2428
|
+
* emitter.on('test' (a, b) => console.log(a + b));
|
|
2429
|
+
* emitter.on('test' (a, b) => console.log(a * b));
|
|
2430
|
+
*
|
|
2431
|
+
* emitter.emit('other', 2, 5);
|
|
2432
|
+
* // 7
|
|
2433
|
+
* // 10
|
|
2434
|
+
*
|
|
2435
|
+
* emitter.emit('other', 5, 5);
|
|
2436
|
+
* // 10
|
|
2437
|
+
* // 25
|
|
2438
|
+
*/
|
|
2161
2439
|
EmitterPro.prototype.emit = function (eventName) {
|
|
2162
2440
|
var args = [];
|
|
2163
2441
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
@@ -2175,16 +2453,36 @@
|
|
|
2175
2453
|
return EmitterPro;
|
|
2176
2454
|
}());
|
|
2177
2455
|
|
|
2456
|
+
var cache = {};
|
|
2457
|
+
var MemoryStorage = /** @class */ (function () {
|
|
2458
|
+
function MemoryStorage(scope) {
|
|
2459
|
+
if (scope === void 0) { scope = 'default'; }
|
|
2460
|
+
this.scope = scope;
|
|
2461
|
+
if (!cache[this.scope]) {
|
|
2462
|
+
cache[this.scope] = {};
|
|
2463
|
+
}
|
|
2464
|
+
this.data = cache[this.scope];
|
|
2465
|
+
}
|
|
2466
|
+
MemoryStorage.prototype.getItem = function (key) {
|
|
2467
|
+
return key in this.data ? this.data[key] : null;
|
|
2468
|
+
};
|
|
2469
|
+
MemoryStorage.prototype.setItem = function (key, value) {
|
|
2470
|
+
this.data[key] = value;
|
|
2471
|
+
};
|
|
2472
|
+
MemoryStorage.prototype.removeItem = function (key) {
|
|
2473
|
+
delete this.data[key];
|
|
2474
|
+
};
|
|
2475
|
+
MemoryStorage.prototype.clear = function () {
|
|
2476
|
+
cache[this.scope] = {};
|
|
2477
|
+
this.data = cache[this.scope];
|
|
2478
|
+
};
|
|
2479
|
+
return MemoryStorage;
|
|
2480
|
+
}());
|
|
2481
|
+
|
|
2178
2482
|
// 随机字符串
|
|
2179
2483
|
function randomString() {
|
|
2180
2484
|
return Math.random().toString(16).substring(2, 8);
|
|
2181
2485
|
}
|
|
2182
|
-
// 内部自增id
|
|
2183
|
-
var uid = 1;
|
|
2184
|
-
// 返回唯一标识
|
|
2185
|
-
function getUniqueId() {
|
|
2186
|
-
return "".concat(randomString(), "_").concat(uid++);
|
|
2187
|
-
}
|
|
2188
2486
|
// 是否支持 storage
|
|
2189
2487
|
function isStorageSupported(storage) {
|
|
2190
2488
|
try {
|
|
@@ -2194,7 +2492,7 @@
|
|
|
2194
2492
|
!!storage.getItem &&
|
|
2195
2493
|
!!storage.removeItem;
|
|
2196
2494
|
if (isSupport) {
|
|
2197
|
-
var key =
|
|
2495
|
+
var key = randomString() + new Date().getTime();
|
|
2198
2496
|
var value = '1';
|
|
2199
2497
|
storage.setItem(key, value);
|
|
2200
2498
|
if (storage.getItem(key) !== value) {
|
|
@@ -2221,49 +2519,120 @@
|
|
|
2221
2519
|
return JSON.stringify(value, replacer);
|
|
2222
2520
|
}
|
|
2223
2521
|
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2522
|
+
/**
|
|
2523
|
+
* 数据存储管理。
|
|
2524
|
+
*
|
|
2525
|
+
* @class
|
|
2526
|
+
* @param {Object} [storage] 自定义缓存对象要包含 `getItem` `setItem` `removeItem` 方法。默认使用内存缓存。
|
|
2527
|
+
* @param {Object} [options] 配置项。可选。
|
|
2528
|
+
* @param {boolean} [options.needParsed] 存取数据时是否需要序列化和解析数据。如果使用内置的内存缓存,默认 `false`,如果自定义 `storage` 默认 `true`。
|
|
2529
|
+
* @param {Function} [options.replacer] 数据存储时序列化的参数,透传给 [JSON.stringify](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) 的 `replacer` 参数。仅在 `needParsed=true` 时生效。
|
|
2530
|
+
* @param {Function} [options.reviver] 数据获取时转换的参数,透传给 [JSON.parse](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) 的 `reviver` 参数。仅在 `needParsed=true` 时生效。
|
|
2531
|
+
* @param {string} [options.prefix] 缓存键前缀。便于管理同域名下的不同项目缓存。
|
|
2532
|
+
* @example
|
|
2533
|
+
* // 使用内存缓存
|
|
2534
|
+
* const memory = new Storage();
|
|
2535
|
+
* memory.set('foo', { baz: 42 });
|
|
2536
|
+
* memory.get('foo');
|
|
2537
|
+
* // { baz: 42 }
|
|
2538
|
+
*
|
|
2539
|
+
* // 自定义缓存 sessionStorage 。
|
|
2540
|
+
* const session = new Storage(window.sessionStorage);
|
|
2541
|
+
* session.set('foo', { a: 1, b: ['bar'], c: ['x', 2, 3] });
|
|
2542
|
+
* session.get('foo');
|
|
2543
|
+
* // { a: 1, b: ['bar'], c: ['x', 2, 3] }
|
|
2544
|
+
*
|
|
2545
|
+
* session.del('foo'); // 删除缓存
|
|
2546
|
+
* session.get('foo');
|
|
2547
|
+
* // null
|
|
2548
|
+
*
|
|
2549
|
+
* // 使用缓存键前缀。
|
|
2550
|
+
* // 如果要使用内存缓存, storage 传 `undefined`。
|
|
2551
|
+
* const local = new Storage(window.localStorage, { prefix: 'project_name' });
|
|
2552
|
+
* local.set('foo', { baz: 42 });
|
|
2553
|
+
* local.get('foo');
|
|
2554
|
+
* // { baz: 42 }
|
|
2555
|
+
*/
|
|
2250
2556
|
var Storage = /** @class */ (function () {
|
|
2251
2557
|
function Storage(storage, options) {
|
|
2252
2558
|
if (options === void 0) { options = {}; }
|
|
2253
2559
|
var isSupported = storage ? isStorageSupported(storage) : false;
|
|
2254
|
-
this.options = __assign({ needParsed: isSupported }, options);
|
|
2560
|
+
this.options = __assign({ needParsed: isSupported, prefix: '' }, options);
|
|
2255
2561
|
this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
|
|
2256
2562
|
}
|
|
2563
|
+
/**
|
|
2564
|
+
* 内部用于获取存储的键名称。
|
|
2565
|
+
*
|
|
2566
|
+
* 如果实例有设置 `prefix`,返回 `prefix + key`。
|
|
2567
|
+
*
|
|
2568
|
+
* @protected
|
|
2569
|
+
* @param key 原键名称
|
|
2570
|
+
* @returns 存储的键名称
|
|
2571
|
+
*/
|
|
2572
|
+
Storage.prototype.getKey = function (key) {
|
|
2573
|
+
return this.options.prefix + key;
|
|
2574
|
+
};
|
|
2575
|
+
/**
|
|
2576
|
+
* 获取存储的数据。
|
|
2577
|
+
*
|
|
2578
|
+
* @param {string} key 键名称。
|
|
2579
|
+
* @returns 如果键值存在返回键值,否则返回 `null`。
|
|
2580
|
+
* @example
|
|
2581
|
+
* const local = new Storage(window.localStorage);
|
|
2582
|
+
* local.set('foo', { baz: 42 });
|
|
2583
|
+
* local.get('foo');
|
|
2584
|
+
* // { baz: 42 }
|
|
2585
|
+
*/
|
|
2257
2586
|
Storage.prototype.get = function (key) {
|
|
2258
|
-
var
|
|
2259
|
-
return this.options.needParsed ? parse(
|
|
2587
|
+
var value = this.storage.getItem(this.getKey(key));
|
|
2588
|
+
return this.options.needParsed ? parse(value, this.options.reviver) : value;
|
|
2260
2589
|
};
|
|
2261
|
-
|
|
2262
|
-
|
|
2590
|
+
/**
|
|
2591
|
+
* 存储数据。
|
|
2592
|
+
*
|
|
2593
|
+
* @param key 键名称。
|
|
2594
|
+
* @param value 键值。
|
|
2595
|
+
* @example
|
|
2596
|
+
* const local = new Storage(window.localStorage);
|
|
2597
|
+
* local.set('foo', { baz: 42 });
|
|
2598
|
+
* local.get('foo');
|
|
2599
|
+
* // { baz: 42 }
|
|
2600
|
+
*/
|
|
2601
|
+
Storage.prototype.set = function (key, value) {
|
|
2602
|
+
this.storage.setItem(this.getKey(key), this.options.needParsed ? stringify(value, this.options.replacer) : value);
|
|
2263
2603
|
};
|
|
2604
|
+
/**
|
|
2605
|
+
* 删除存储的数据。
|
|
2606
|
+
*
|
|
2607
|
+
* @param key 键名称。
|
|
2608
|
+
* @example
|
|
2609
|
+
* const local = new Storage(window.localStorage);
|
|
2610
|
+
* local.set('foo', { baz: 42 });
|
|
2611
|
+
* local.get('foo');
|
|
2612
|
+
* // { baz: 42 }
|
|
2613
|
+
*
|
|
2614
|
+
* local.del('foo');
|
|
2615
|
+
* local.get('foo');
|
|
2616
|
+
* // null
|
|
2617
|
+
*/
|
|
2264
2618
|
Storage.prototype.del = function (key) {
|
|
2265
|
-
this.storage.removeItem(key);
|
|
2619
|
+
this.storage.removeItem(this.getKey(key));
|
|
2266
2620
|
};
|
|
2621
|
+
/**
|
|
2622
|
+
* 清除存储的所有键。
|
|
2623
|
+
*
|
|
2624
|
+
* 注意:该方法调用 `storage.clear()`,可能会将同域下的不同实例的所有键都清除。如果要避免这种情况,建议使用 `import { Cache } 'cache2'`。
|
|
2625
|
+
*
|
|
2626
|
+
* @example
|
|
2627
|
+
* const local = new Storage(window.localStorage);
|
|
2628
|
+
* local.set('foo', { baz: 42 });
|
|
2629
|
+
* local.get('foo');
|
|
2630
|
+
* // { baz: 42 }
|
|
2631
|
+
*
|
|
2632
|
+
* local.clear();
|
|
2633
|
+
* local.get('foo');
|
|
2634
|
+
* // null
|
|
2635
|
+
*/
|
|
2267
2636
|
Storage.prototype.clear = function () {
|
|
2268
2637
|
if (typeof this.storage.clear === 'function') {
|
|
2269
2638
|
this.storage.clear();
|
|
@@ -2272,15 +2641,55 @@
|
|
|
2272
2641
|
return Storage;
|
|
2273
2642
|
}());
|
|
2274
2643
|
|
|
2275
|
-
|
|
2644
|
+
// 命名空间缓存键前缀。
|
|
2645
|
+
var defaultPrefix = 'cache2_';
|
|
2276
2646
|
var defaultNamespace = 'default';
|
|
2647
|
+
/**
|
|
2648
|
+
* 功能丰富的数据存储管理,支持 `自定义缓存` `命名空间` `数据过期时间` `限制缓存数量` `自定义事件`。
|
|
2649
|
+
*
|
|
2650
|
+
* 注意:如果你需要的是简单的基本数据存储管理,例如浏览器存储,建议使用 `import { Storage } from 'cache2'`。
|
|
2651
|
+
*
|
|
2652
|
+
* @class
|
|
2653
|
+
* @param {string} [namespace] 命名空间。可选。
|
|
2654
|
+
* @param {Object} [options] 配置项。可选。
|
|
2655
|
+
* @param {Object} [options.storage] 自定义缓存对象要包含 `getItem` `setItem` `removeItem` 方法。默认使用内置的内存缓存。
|
|
2656
|
+
* @param {number} [options.max=-1] 最大缓存数据数量。`-1` 表示无限制。默认 `-1`。
|
|
2657
|
+
* @param {'limited' | 'replaced'} [options.maxStrategy='limited'] 当达到最大缓存数量限制时的缓存策略。`limited` 表示达到限制数量后不存入数据,保存时返回 `false`。`replaced` 表示优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理,保存时始终返回 `true`。默认 `limited`。
|
|
2658
|
+
* @param {number} [options.stdTTL=0] 相对当前时间的数据存活时间,应用于当前实例的所有缓存数据。单位为毫秒,`0` 表示无期限。默认 `0`。
|
|
2659
|
+
* @param {number} [options.checkperiod=0] 定时检查过期数据,单位毫秒。如果小于等于 `0` 表示不启动定时器检查。默认 `0`。
|
|
2660
|
+
* @param {boolean} [options.needParsed] 存取数据时是否需要序列化和解析数据。如果使用内置的内存缓存,默认 `false`,如果自定义 `storage` 默认 `true`。
|
|
2661
|
+
* @param {Function} [options.replacer] 数据存储时序列化的参数,透传给 [JSON.stringify](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) 的 `replacer` 参数。仅在 `needParsed=true` 时生效。
|
|
2662
|
+
* @param {Function} [options.reviver] 数据获取时转换的参数,透传给 [JSON.parse](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) 的 `reviver` 参数。仅在 `needParsed=true` 时生效。
|
|
2663
|
+
* @param {string} [options.prefix] 缓存键前缀。
|
|
2664
|
+
* @example
|
|
2665
|
+
* // 自定义过期时间
|
|
2666
|
+
* const memoryCache = new Cache({ stdTTL: 60 * 1000 });
|
|
2667
|
+
* memoryCache.set('foo', { baz: 42 });
|
|
2668
|
+
* memoryCache.get('foo');
|
|
2669
|
+
* // { baz: 42 }
|
|
2670
|
+
*
|
|
2671
|
+
* // 60 seconds later
|
|
2672
|
+
*
|
|
2673
|
+
* memoryCache.get('foo');
|
|
2674
|
+
* // undefined
|
|
2675
|
+
*
|
|
2676
|
+
* // 命名空间、自定义缓存
|
|
2677
|
+
* const localCache = new Cache('namespace', { storage: window.localStorage });
|
|
2678
|
+
* localCache.set('foo', { baz: 42 });
|
|
2679
|
+
* localCache.get('foo');
|
|
2680
|
+
* // { baz: 42 }
|
|
2681
|
+
*
|
|
2682
|
+
* localCache.del('foo');
|
|
2683
|
+
* localCache.get('foo');
|
|
2684
|
+
* // undefined
|
|
2685
|
+
*/
|
|
2277
2686
|
var Cache = /** @class */ (function (_super) {
|
|
2278
2687
|
__extends(Cache, _super);
|
|
2279
2688
|
function Cache(namespace, options) {
|
|
2280
2689
|
var _this = _super.call(this) || this;
|
|
2281
2690
|
var ns = defaultNamespace, opts;
|
|
2282
2691
|
if (typeof namespace === 'string') {
|
|
2283
|
-
ns = namespace;
|
|
2692
|
+
ns = namespace || defaultNamespace;
|
|
2284
2693
|
}
|
|
2285
2694
|
else if (typeof namespace === 'object') {
|
|
2286
2695
|
opts = namespace;
|
|
@@ -2289,12 +2698,18 @@
|
|
|
2289
2698
|
opts = options;
|
|
2290
2699
|
}
|
|
2291
2700
|
_this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
|
|
2292
|
-
_this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns },
|
|
2293
|
-
_this.cacheKey =
|
|
2701
|
+
_this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, _this.options));
|
|
2702
|
+
_this.cacheKey = ns;
|
|
2294
2703
|
_this.startCheckperiod();
|
|
2295
2704
|
return _this;
|
|
2296
2705
|
}
|
|
2297
|
-
|
|
2706
|
+
/**
|
|
2707
|
+
* 检查当前键值是否过期,如果过期将会自动删除。
|
|
2708
|
+
*
|
|
2709
|
+
* @param key 键名称。
|
|
2710
|
+
* @param data 缓存数据。
|
|
2711
|
+
* @returns 如果键值已过期返回 `false` ,否则返回 `true`。
|
|
2712
|
+
*/
|
|
2298
2713
|
Cache.prototype._check = function (key, data) {
|
|
2299
2714
|
var ret = true;
|
|
2300
2715
|
if (data.t !== 0 && data.t < Date.now()) {
|
|
@@ -2328,7 +2743,9 @@
|
|
|
2328
2743
|
return retkey;
|
|
2329
2744
|
};
|
|
2330
2745
|
Object.defineProperty(Cache.prototype, "cacheValues", {
|
|
2331
|
-
|
|
2746
|
+
/**
|
|
2747
|
+
* 获取全部缓存数据,不处理过期数据和排序
|
|
2748
|
+
*/
|
|
2332
2749
|
get: function () {
|
|
2333
2750
|
return this.storage.get(this.cacheKey) || {};
|
|
2334
2751
|
},
|
|
@@ -2339,7 +2756,19 @@
|
|
|
2339
2756
|
Cache.prototype.setCacheValues = function (values) {
|
|
2340
2757
|
this.storage.set(this.cacheKey, values);
|
|
2341
2758
|
};
|
|
2342
|
-
|
|
2759
|
+
/**
|
|
2760
|
+
* 获取缓存值。
|
|
2761
|
+
*
|
|
2762
|
+
* @param {string} key 键名称。
|
|
2763
|
+
* @returns {*} 如果找到该值,则返回该值。如果未找到或已过期,则返回 `undefined`。
|
|
2764
|
+
* @example
|
|
2765
|
+
* myCache.set('myKey', obj, 5 * 60 * 1000);
|
|
2766
|
+
* myCache.get('myKey');
|
|
2767
|
+
* // { foo: 'bar', baz: 42 }
|
|
2768
|
+
*
|
|
2769
|
+
* myCache.get('myKey2');
|
|
2770
|
+
* // undefined
|
|
2771
|
+
*/
|
|
2343
2772
|
Cache.prototype.get = function (key) {
|
|
2344
2773
|
var data = this.cacheValues[key];
|
|
2345
2774
|
if (data && this._check(key, data)) {
|
|
@@ -2347,7 +2776,24 @@
|
|
|
2347
2776
|
}
|
|
2348
2777
|
return;
|
|
2349
2778
|
};
|
|
2350
|
-
|
|
2779
|
+
/**
|
|
2780
|
+
* 获取多个缓存值。
|
|
2781
|
+
*
|
|
2782
|
+
* @param {string[]} keys 多个键名称。
|
|
2783
|
+
* @returns {Object} 如果找到对应键名的值,返回一个具有键值对的对象。如果未找到或已过期,则返回一个空对象 `{}`。
|
|
2784
|
+
* @example
|
|
2785
|
+
* myCache.mset([
|
|
2786
|
+
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2787
|
+
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2788
|
+
* { key: 'myKey3', value: 'abc' }
|
|
2789
|
+
* ]);
|
|
2790
|
+
*
|
|
2791
|
+
* myCache.mget(['myKey', 'myKey2']);
|
|
2792
|
+
* // {
|
|
2793
|
+
* // myKey: { foo: 'bar', baz: 42 },
|
|
2794
|
+
* // myKey2: { a: 1, b: 2 }
|
|
2795
|
+
* // }
|
|
2796
|
+
*/
|
|
2351
2797
|
Cache.prototype.mget = function (keys) {
|
|
2352
2798
|
var _this = this;
|
|
2353
2799
|
var ret = {};
|
|
@@ -2363,12 +2809,41 @@
|
|
|
2363
2809
|
});
|
|
2364
2810
|
return ret;
|
|
2365
2811
|
};
|
|
2366
|
-
|
|
2812
|
+
/**
|
|
2813
|
+
* 获取全部缓存值。
|
|
2814
|
+
*
|
|
2815
|
+
* @returns {Object} 返回一个具有键值对的对象。
|
|
2816
|
+
* @example
|
|
2817
|
+
* myCache.mset([
|
|
2818
|
+
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2819
|
+
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2820
|
+
* { key: 'myKey3', value: 'abc' }
|
|
2821
|
+
* ]);
|
|
2822
|
+
*
|
|
2823
|
+
* myCache.getAll();
|
|
2824
|
+
* // {
|
|
2825
|
+
* // myKey: { foo: 'bar', baz: 42 },
|
|
2826
|
+
* // myKey2: { a: 1, b: 2 }
|
|
2827
|
+
* // myKey3: 'abc'
|
|
2828
|
+
* // }
|
|
2829
|
+
*/
|
|
2367
2830
|
Cache.prototype.getAll = function () {
|
|
2368
2831
|
var keys = Object.keys(this.cacheValues);
|
|
2369
2832
|
return this.mget(keys);
|
|
2370
2833
|
};
|
|
2371
|
-
|
|
2834
|
+
/**
|
|
2835
|
+
* 设置缓存数据。
|
|
2836
|
+
*
|
|
2837
|
+
* 如果超出缓存数量,可能会设置失败。
|
|
2838
|
+
*
|
|
2839
|
+
* @param {string} key 键名称。
|
|
2840
|
+
* @param {*} value 键值。
|
|
2841
|
+
* @param {number} [ttl] 数据存活时间。单位毫秒 `ms`。
|
|
2842
|
+
* @returns {boolean} 如果设置成功返回 `true`,否则返回 `false`。
|
|
2843
|
+
* @example
|
|
2844
|
+
* myCache.set('myKey', { foo: 'bar', baz: 42 }, 5 * 60 * 1000);
|
|
2845
|
+
* // true
|
|
2846
|
+
*/
|
|
2372
2847
|
Cache.prototype.set = function (key, value, ttl) {
|
|
2373
2848
|
if (this.options.max === 0) {
|
|
2374
2849
|
return false;
|
|
@@ -2395,7 +2870,19 @@
|
|
|
2395
2870
|
this.emit('set', key, cacheValues[key].v);
|
|
2396
2871
|
return true;
|
|
2397
2872
|
};
|
|
2398
|
-
|
|
2873
|
+
/**
|
|
2874
|
+
* 设置多个缓存数据。
|
|
2875
|
+
*
|
|
2876
|
+
* @param {Object[]} keyValueSet 多个键值对数据。
|
|
2877
|
+
* @returns {boolean} 如果全部设置成功返回 `true`,否则返回 `false`。
|
|
2878
|
+
* @example
|
|
2879
|
+
* myCache.mset([
|
|
2880
|
+
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2881
|
+
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2882
|
+
* { key: 'myKey3', value: 'abc' }
|
|
2883
|
+
* ]);
|
|
2884
|
+
* // true
|
|
2885
|
+
*/
|
|
2399
2886
|
Cache.prototype.mset = function (keyValueSet) {
|
|
2400
2887
|
var _this = this;
|
|
2401
2888
|
// 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
|
|
@@ -2408,7 +2895,23 @@
|
|
|
2408
2895
|
});
|
|
2409
2896
|
return ret;
|
|
2410
2897
|
};
|
|
2411
|
-
|
|
2898
|
+
/**
|
|
2899
|
+
* 删除一个或多个键。
|
|
2900
|
+
*
|
|
2901
|
+
* @param {string|string[]} key 要删除的键名。
|
|
2902
|
+
* @returns {number} 返回已删除的数量。
|
|
2903
|
+
* @example
|
|
2904
|
+
* myCache.set('myKey', { foo: 'bar', baz: 42 });
|
|
2905
|
+
* myCache.del('myKey'); // 1
|
|
2906
|
+
* myCache.del('not found'); // 0
|
|
2907
|
+
*
|
|
2908
|
+
* myCache.mset([
|
|
2909
|
+
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2910
|
+
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2911
|
+
* { key: 'myKey3', value: 'abc' }
|
|
2912
|
+
* ]);
|
|
2913
|
+
* myCache.del(['myKey', 'myKey2']); // 2
|
|
2914
|
+
*/
|
|
2412
2915
|
Cache.prototype.del = function (key) {
|
|
2413
2916
|
var _this = this;
|
|
2414
2917
|
var cacheValues = this.cacheValues;
|
|
@@ -2427,23 +2930,63 @@
|
|
|
2427
2930
|
}
|
|
2428
2931
|
return count;
|
|
2429
2932
|
};
|
|
2430
|
-
|
|
2933
|
+
/**
|
|
2934
|
+
* 清除全部缓存的数据。
|
|
2935
|
+
*
|
|
2936
|
+
* @example
|
|
2937
|
+
* myCache.set('bar', 1);
|
|
2938
|
+
* myCache.set('foo', 2);
|
|
2939
|
+
* myCache.keys(); // ['bar', 'foo']
|
|
2940
|
+
*
|
|
2941
|
+
* myCache.clear();
|
|
2942
|
+
* myCache.keys(); // []
|
|
2943
|
+
*/
|
|
2431
2944
|
Cache.prototype.clear = function () {
|
|
2432
2945
|
this.storage.del(this.cacheKey);
|
|
2433
2946
|
};
|
|
2434
|
-
|
|
2947
|
+
/**
|
|
2948
|
+
* 获取全部键名的数组。
|
|
2949
|
+
*
|
|
2950
|
+
* @returns {string[]} 返回全部键名的数组。
|
|
2951
|
+
* @example
|
|
2952
|
+
* myCache.set('bar', 1);
|
|
2953
|
+
* myCache.set('foo', 2);
|
|
2954
|
+
*
|
|
2955
|
+
* myCache.keys(); // ['bar', 'foo']
|
|
2956
|
+
*/
|
|
2435
2957
|
Cache.prototype.keys = function () {
|
|
2436
2958
|
var _this = this;
|
|
2437
2959
|
var cacheValues = this.cacheValues;
|
|
2438
2960
|
var keys = Object.keys(cacheValues);
|
|
2439
2961
|
return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
|
|
2440
2962
|
};
|
|
2441
|
-
|
|
2963
|
+
/**
|
|
2964
|
+
* 判断是否存在某个键。
|
|
2965
|
+
*
|
|
2966
|
+
* @param {string} key 键名称。
|
|
2967
|
+
* @returns {boolean} 如果包含该键返回 `true`,否则返回 `false`。
|
|
2968
|
+
* @example
|
|
2969
|
+
* myCache.has('foo'); // false
|
|
2970
|
+
*
|
|
2971
|
+
* myCache.set('foo', 1);
|
|
2972
|
+
* myCache.has('foo'); // true
|
|
2973
|
+
*/
|
|
2442
2974
|
Cache.prototype.has = function (key) {
|
|
2443
2975
|
var data = this.cacheValues[key];
|
|
2444
2976
|
return !!(data && this._check(key, data));
|
|
2445
2977
|
};
|
|
2446
|
-
|
|
2978
|
+
/**
|
|
2979
|
+
* 获取缓存值并从缓存中删除键。
|
|
2980
|
+
*
|
|
2981
|
+
* @param {string} key 键名称。
|
|
2982
|
+
* @returns {*} 如果找到该值,则返回该值,并从缓存中删除该键。如果未找到或已过期,则返回 `undefined`。
|
|
2983
|
+
* @example
|
|
2984
|
+
* myCache.set('myKey', 'myValue');
|
|
2985
|
+
* myCache.has('myKey'); // true
|
|
2986
|
+
*
|
|
2987
|
+
* myCache.take('myKey'); // 'myValue'
|
|
2988
|
+
* myCache.has('myKey'); // false
|
|
2989
|
+
*/
|
|
2447
2990
|
Cache.prototype.take = function (key) {
|
|
2448
2991
|
var ret;
|
|
2449
2992
|
var data = this.cacheValues[key];
|
|
@@ -2453,7 +2996,20 @@
|
|
|
2453
2996
|
}
|
|
2454
2997
|
return ret;
|
|
2455
2998
|
};
|
|
2456
|
-
|
|
2999
|
+
/**
|
|
3000
|
+
* 更新缓存键值的数据存活时间。
|
|
3001
|
+
*
|
|
3002
|
+
* @param {string} key 键名称。
|
|
3003
|
+
* @param {number} ttl 数据存活时间。
|
|
3004
|
+
* @returns {boolean} 如果找到并更新成功,则返回 `true`,否则返回 `false`。
|
|
3005
|
+
* @example
|
|
3006
|
+
* myCache.set('myKey', { foo: 'bar', baz: 42 }, 5 * 60 * 1000);
|
|
3007
|
+
* myCache.ttl('myKey', 60 * 1000);
|
|
3008
|
+
* // true
|
|
3009
|
+
*
|
|
3010
|
+
* myCache.ttl('not found', 1000);
|
|
3011
|
+
* // false
|
|
3012
|
+
*/
|
|
2457
3013
|
Cache.prototype.ttl = function (key, ttl) {
|
|
2458
3014
|
var cacheValues = this.cacheValues;
|
|
2459
3015
|
var data = cacheValues[key];
|
|
@@ -2463,10 +3019,22 @@
|
|
|
2463
3019
|
}
|
|
2464
3020
|
return false;
|
|
2465
3021
|
};
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
3022
|
+
/**
|
|
3023
|
+
* 获取某个键的过期时间戳。
|
|
3024
|
+
*
|
|
3025
|
+
* @param {string} key 键名称。
|
|
3026
|
+
* @returns {number | undefined} 如果未找到键或已过期,返回 `undefined`。如果 `ttl` 为 `0`,返回 `0`,否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
3027
|
+
* @example
|
|
3028
|
+
* const myCache = new Cache({ stdTTL: 5 * 1000 });
|
|
3029
|
+
*
|
|
3030
|
+
* // 假如 Date.now() = 1673330000000
|
|
3031
|
+
* myCache.set('ttlKey', 'expireData');
|
|
3032
|
+
* myCache.set('noTtlKey', 'nonExpireData', 0);
|
|
3033
|
+
*
|
|
3034
|
+
* myCache.getTtl('ttlKey'); // 1673330005000
|
|
3035
|
+
* myCache.getTtl('noTtlKey'); // 0
|
|
3036
|
+
* myCache.getTtl('unknownKey'); // undefined
|
|
3037
|
+
*/
|
|
2470
3038
|
Cache.prototype.getTtl = function (key) {
|
|
2471
3039
|
var cacheValues = this.cacheValues;
|
|
2472
3040
|
var data = cacheValues[key];
|
|
@@ -2475,9 +3043,22 @@
|
|
|
2475
3043
|
}
|
|
2476
3044
|
return;
|
|
2477
3045
|
};
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
3046
|
+
/**
|
|
3047
|
+
* 获取某个键值的最后修改时间。
|
|
3048
|
+
*
|
|
3049
|
+
* @param {string} key 键名称。
|
|
3050
|
+
* @returns {number | undefined} 如果未找到键或已过期,返回 `undefined`,否则返回一个以毫秒时间戳,表示键值最后修改时间。
|
|
3051
|
+
* @example
|
|
3052
|
+
* const myCache = new Cache();
|
|
3053
|
+
*
|
|
3054
|
+
* // 假如 Date.now() = 1673330000000
|
|
3055
|
+
* myCache.set('myKey', 'foo');
|
|
3056
|
+
* myCache.getLastModified('myKey'); // 1673330000000
|
|
3057
|
+
*
|
|
3058
|
+
* // 5000ms later
|
|
3059
|
+
* myCache.set('myKey', 'bar');
|
|
3060
|
+
* myCache.getLastModified('myKey'); // 1673330005000
|
|
3061
|
+
*/
|
|
2481
3062
|
Cache.prototype.getLastModified = function (key) {
|
|
2482
3063
|
var cacheValues = this.cacheValues;
|
|
2483
3064
|
var data = cacheValues[key];
|
|
@@ -2486,7 +3067,23 @@
|
|
|
2486
3067
|
}
|
|
2487
3068
|
return;
|
|
2488
3069
|
};
|
|
2489
|
-
|
|
3070
|
+
/**
|
|
3071
|
+
* 启动定时校验过期数据。
|
|
3072
|
+
*
|
|
3073
|
+
* 注意,如果没有设置 `checkperiod` 将不会触发定时器。
|
|
3074
|
+
*
|
|
3075
|
+
* @example
|
|
3076
|
+
* // 设置 checkperiod 之后自动生效
|
|
3077
|
+
* const myCache = new Cache({
|
|
3078
|
+
* checkperiod: 10 * 60 * 1000 // 10分钟检查一次数据是否过期
|
|
3079
|
+
* });
|
|
3080
|
+
*
|
|
3081
|
+
* // 停止定时校验过期数据
|
|
3082
|
+
* myCache.stopCheckperiod();
|
|
3083
|
+
*
|
|
3084
|
+
* // 启动定时校验过期数据
|
|
3085
|
+
* myCache.startCheckperiod();
|
|
3086
|
+
*/
|
|
2490
3087
|
Cache.prototype.startCheckperiod = function () {
|
|
2491
3088
|
var _this = this;
|
|
2492
3089
|
// 触发全部缓存数据是否过期校验
|
|
@@ -2498,7 +3095,18 @@
|
|
|
2498
3095
|
}, this.options.checkperiod);
|
|
2499
3096
|
}
|
|
2500
3097
|
};
|
|
2501
|
-
|
|
3098
|
+
/**
|
|
3099
|
+
* 停止定时校验过期数据。
|
|
3100
|
+
*
|
|
3101
|
+
* @example
|
|
3102
|
+
* // 设置 checkperiod 之后自动生效
|
|
3103
|
+
* const myCache = new Cache({
|
|
3104
|
+
* checkperiod: 10 * 60 * 1000 // 10分钟检查一次数据是否过期
|
|
3105
|
+
* });
|
|
3106
|
+
*
|
|
3107
|
+
* // 停止定时校验过期数据
|
|
3108
|
+
* myCache.stopCheckperiod();
|
|
3109
|
+
*/
|
|
2502
3110
|
Cache.prototype.stopCheckperiod = function () {
|
|
2503
3111
|
clearTimeout(this._checkTimeout);
|
|
2504
3112
|
};
|
|
@@ -2561,6 +3169,7 @@
|
|
|
2561
3169
|
exports.getFileBlob = getFileBlob;
|
|
2562
3170
|
exports.getFileType = getFileType;
|
|
2563
3171
|
exports.getImageInfo = getImageInfo;
|
|
3172
|
+
exports.getMimeType = getMimeType;
|
|
2564
3173
|
exports.isBankCard = isBankCard;
|
|
2565
3174
|
exports.isBusinessLicense = isBusinessLicense;
|
|
2566
3175
|
exports.isChinese = isChinese;
|