@whitesev/utils 2.1.2 → 2.1.3

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.
@@ -31,6 +31,19 @@ declare class indexedDB {
31
31
  target: IDBRequest<T>;
32
32
  } & Event;
33
33
  }>;
34
+ /**
35
+ * 判断是否存在该数据
36
+ * @param key 数据key
37
+ */
38
+ has(key: string): Promise<{
39
+ success: boolean;
40
+ code: number;
41
+ msg: string;
42
+ event?: {
43
+ srcElement: IDBRequest;
44
+ target: IDBRequest;
45
+ } & Event;
46
+ }>;
34
47
  /**
35
48
  * 根据key获取值
36
49
  * @param key 数据key
@@ -51,9 +64,9 @@ declare class indexedDB {
51
64
  }>;
52
65
  /**
53
66
  * 正则获取数据
54
- * @param key 数据键
67
+ * @param key 数据key,可以是正则
55
68
  */
56
- regexpGet<T extends any>(key: string): Promise<{
69
+ regexpGet<T extends any>(key: string | RegExp): Promise<{
57
70
  success: boolean;
58
71
  code: number;
59
72
  msg: string;
@@ -65,7 +78,7 @@ declare class indexedDB {
65
78
  }>;
66
79
  /**
67
80
  * 删除数据
68
- * @param {string} key 数据键
81
+ * @param key 数据key
69
82
  */
70
83
  delete(key: string): Promise<{
71
84
  success: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.1.2",
3
+ "version": "2.1.3",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
package/src/indexedDB.ts CHANGED
@@ -184,6 +184,55 @@ class indexedDB {
184
184
  });
185
185
  }
186
186
 
187
+ /**
188
+ * 判断是否存在该数据
189
+ * @param key 数据key
190
+ */
191
+ async has(key: string): Promise<{
192
+ success: boolean;
193
+ code: number;
194
+ msg: string;
195
+ event?: {
196
+ srcElement: IDBRequest;
197
+ target: IDBRequest;
198
+ } & Event;
199
+ }> {
200
+ let that = this;
201
+ return new Promise((resolve) => {
202
+ let dbName = this.#dbName;
203
+ this.open(function (idbStore, success) {
204
+ /* 判断返回的数据中是否有error字段 */
205
+ if (!success) {
206
+ resolve({
207
+ success: false,
208
+ code: that.#errorCode.get.code,
209
+ msg: that.#errorCode.get.msg,
210
+ });
211
+ } else {
212
+ idbStore = idbStore as IDBObjectStore;
213
+ let request = idbStore.get(key);
214
+ request.onsuccess = function (event: any) {
215
+ /* result 返回的是 {key: string, value: any} */
216
+ /* 键值对存储 */
217
+ resolve({
218
+ success: true,
219
+ code: that.#errorCode.success.code,
220
+ msg: that.#errorCode.success.msg,
221
+ event: event,
222
+ });
223
+ };
224
+ request.onerror = function (event: any) {
225
+ resolve({
226
+ success: false,
227
+ code: that.#errorCode.get.code,
228
+ msg: that.#errorCode.get.msg,
229
+ event: event,
230
+ });
231
+ };
232
+ }
233
+ }, dbName);
234
+ });
235
+ }
187
236
  /**
188
237
  * 根据key获取值
189
238
  * @param key 数据key
@@ -222,10 +271,15 @@ class indexedDB {
222
271
  let request = idbStore.get(key);
223
272
  request.onsuccess = function (event: any) {
224
273
  let target = event.target as IDBRequest;
225
- let result = target.result;
274
+ let result = target.result as
275
+ | {
276
+ key: string;
277
+ value: T;
278
+ }
279
+ | undefined;
226
280
  /* result 返回的是 {key: string, value: any} */
227
281
  /* 键值对存储 */
228
- let data: T = result ? result.value : void 0;
282
+ let data = result ? result.value : void 0;
229
283
  if (data) {
230
284
  resolve({
231
285
  success: true,
@@ -267,10 +321,10 @@ class indexedDB {
267
321
 
268
322
  /**
269
323
  * 正则获取数据
270
- * @param key 数据键
324
+ * @param key 数据key,可以是正则
271
325
  */
272
326
  async regexpGet<T extends any>(
273
- key: string
327
+ key: string | RegExp
274
328
  ): Promise<{
275
329
  success: boolean;
276
330
  code: number;
@@ -300,14 +354,18 @@ class indexedDB {
300
354
  idbStore = idbStore as IDBObjectStore;
301
355
  let request = idbStore.getAll();
302
356
  request.onsuccess = function (event: any) {
303
- let target = event.target as IDBRequest;
357
+ let target = event.target as IDBRequest<
358
+ { key: string; value: T }[]
359
+ >;
304
360
  let result = target.result;
305
361
  if (result.length !== 0) {
306
- result.forEach((item: any, index: any) => {
307
- if (item["key"].match(key)) {
308
- let concatList = item["value"];
309
- concatList["key"] = item["key"];
310
- list = [...list, concatList];
362
+ result.forEach((dataItem, index) => {
363
+ // 当前项的key
364
+ let __key = dataItem["key"];
365
+ // 当前项的value
366
+ let __value = dataItem["value"];
367
+ if (__key.match(key)) {
368
+ list = list.concat(__value);
311
369
  }
312
370
  });
313
371
  }
@@ -339,7 +397,7 @@ class indexedDB {
339
397
 
340
398
  /**
341
399
  * 删除数据
342
- * @param {string} key 数据键
400
+ * @param key 数据key
343
401
  */
344
402
  async delete(key: string): Promise<{
345
403
  success: boolean;