@whitesev/utils 2.1.2 → 2.1.4

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.
@@ -2523,22 +2523,22 @@ var Utils = (function () {
2523
2523
  #db = {};
2524
2524
  // @ts-ignore
2525
2525
  #store = null;
2526
- #errorCode = {
2527
- /* 错误码 */
2528
- success: {
2526
+ /** 状态码 */
2527
+ #statusCode = {
2528
+ operationSuccess: {
2529
2529
  code: 200,
2530
2530
  msg: "操作成功",
2531
2531
  },
2532
- error: {
2532
+ operationFailed: {
2533
2533
  code: 401,
2534
2534
  msg: "操作失败",
2535
2535
  },
2536
- open: { code: 91001, msg: "打开数据库失败" },
2537
- save: { code: 91002, msg: "保存数据失败" },
2538
- get: { code: 91003, msg: "获取数据失败" },
2539
- delete: { code: 91004, msg: "删除数据失败" },
2540
- deleteAll: { code: 91005, msg: "清空数据库失败" },
2541
- regexpGet: { code: 91006, msg: "正则获取数据失败" },
2536
+ openFailed: { code: 91001, msg: "打开数据库失败" },
2537
+ saveFailed: { code: 91002, msg: "保存数据失败" },
2538
+ getFailed: { code: 91003, msg: "获取数据失败" },
2539
+ deleteFailed: { code: 91004, msg: "删除数据失败" },
2540
+ deleteAllFailed: { code: 91005, msg: "清空数据库失败" },
2541
+ regexpGetFailed: { code: 91006, msg: "正则获取数据失败" },
2542
2542
  };
2543
2543
  /**
2544
2544
  * @param dbName 数据存储名,默认为:default_db
@@ -2579,11 +2579,11 @@ var Utils = (function () {
2579
2579
  /* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
2580
2580
  let request = that.#indexedDB.open(dbName, that.#dbVersion);
2581
2581
  request.onerror = function (event) {
2582
- callback({
2583
- code: that.#errorCode.open.code,
2584
- msg: that.#errorCode.open.msg,
2582
+ callback(null, {
2583
+ code: that.#statusCode.openFailed.code,
2584
+ msg: that.#statusCode.openFailed.msg,
2585
2585
  event: event,
2586
- }, false);
2586
+ });
2587
2587
  };
2588
2588
  request.onsuccess = function (event) {
2589
2589
  if (!that.#db[dbName]) {
@@ -2591,7 +2591,7 @@ var Utils = (function () {
2591
2591
  that.#db[dbName] = target.result;
2592
2592
  }
2593
2593
  let store = that.createStore(dbName);
2594
- callback(store, true);
2594
+ callback(store);
2595
2595
  };
2596
2596
  request.onupgradeneeded = function (event) {
2597
2597
  let target = event.target;
@@ -2600,14 +2600,14 @@ var Utils = (function () {
2600
2600
  keyPath: "key",
2601
2601
  });
2602
2602
  store.transaction.oncomplete = function (event) {
2603
- callback(store, true);
2603
+ callback(store);
2604
2604
  };
2605
2605
  };
2606
2606
  }
2607
2607
  else {
2608
2608
  /* 如果缓存中已经打开了数据库,就直接使用 */
2609
- let store = that.createStore(dbName);
2610
- callback(store, true);
2609
+ let store = this.createStore(dbName);
2610
+ callback(store);
2611
2611
  }
2612
2612
  }
2613
2613
  /**
@@ -2618,36 +2618,76 @@ var Utils = (function () {
2618
2618
  async save(key, value) {
2619
2619
  let that = this;
2620
2620
  return new Promise((resolve) => {
2621
- let dbName = that.#dbName;
2621
+ let dbName = this.#dbName;
2622
2622
  let inData = {
2623
2623
  key: key,
2624
2624
  value: value,
2625
2625
  };
2626
- that.open(function (idbStore, success) {
2627
- if (!success) {
2626
+ this.open(function (idbStore) {
2627
+ if (idbStore == null) {
2628
2628
  resolve({
2629
2629
  success: false,
2630
- code: that.#errorCode.save.code,
2631
- msg: that.#errorCode.save.msg,
2630
+ code: that.#statusCode.saveFailed.code,
2631
+ msg: that.#statusCode.saveFailed.msg,
2632
2632
  });
2633
2633
  }
2634
2634
  else {
2635
- idbStore = idbStore;
2636
2635
  let request = idbStore.put(inData);
2637
2636
  request.onsuccess = function (event) {
2638
2637
  /* 保存成功有success 字段 */
2639
2638
  resolve({
2640
2639
  success: true,
2641
- code: that.#errorCode.success.code,
2642
- msg: that.#errorCode.success.msg,
2640
+ code: that.#statusCode.operationSuccess.code,
2641
+ msg: that.#statusCode.operationSuccess.msg,
2643
2642
  event: event,
2644
2643
  });
2645
2644
  };
2646
2645
  request.onerror = function (event) {
2647
2646
  resolve({
2648
2647
  success: false,
2649
- code: that.#errorCode.save.code,
2650
- msg: that.#errorCode.save.msg,
2648
+ code: that.#statusCode.saveFailed.code,
2649
+ msg: that.#statusCode.saveFailed.msg,
2650
+ event: event,
2651
+ });
2652
+ };
2653
+ }
2654
+ }, dbName);
2655
+ });
2656
+ }
2657
+ /**
2658
+ * 判断是否存在该数据
2659
+ * @param key 数据key
2660
+ */
2661
+ async has(key) {
2662
+ let that = this;
2663
+ return new Promise((resolve) => {
2664
+ let dbName = this.#dbName;
2665
+ this.open(function (idbStore) {
2666
+ /* 判断返回的数据中是否有error字段 */
2667
+ if (idbStore == null) {
2668
+ resolve({
2669
+ success: false,
2670
+ code: that.#statusCode.getFailed.code,
2671
+ msg: that.#statusCode.getFailed.msg,
2672
+ });
2673
+ }
2674
+ else {
2675
+ let request = idbStore.get(key);
2676
+ request.onsuccess = function (event) {
2677
+ /* result 返回的是 {key: string, value: any} */
2678
+ /* 键值对存储 */
2679
+ resolve({
2680
+ success: true,
2681
+ code: that.#statusCode.operationSuccess.code,
2682
+ msg: that.#statusCode.operationSuccess.msg,
2683
+ event: event,
2684
+ });
2685
+ };
2686
+ request.onerror = function (event) {
2687
+ resolve({
2688
+ success: false,
2689
+ code: that.#statusCode.getFailed.code,
2690
+ msg: that.#statusCode.getFailed.msg,
2651
2691
  event: event,
2652
2692
  });
2653
2693
  };
@@ -2663,18 +2703,17 @@ var Utils = (function () {
2663
2703
  let that = this;
2664
2704
  return new Promise((resolve) => {
2665
2705
  let dbName = this.#dbName;
2666
- this.open(function (idbStore, success) {
2706
+ this.open(function (idbStore) {
2667
2707
  /* 判断返回的数据中是否有error字段 */
2668
- if (!success) {
2708
+ if (idbStore == null) {
2669
2709
  resolve({
2670
2710
  success: false,
2671
- code: that.#errorCode.get.code,
2672
- msg: that.#errorCode.get.msg,
2711
+ code: that.#statusCode.getFailed.code,
2712
+ msg: that.#statusCode.getFailed.msg,
2673
2713
  data: void 0,
2674
2714
  });
2675
2715
  }
2676
2716
  else {
2677
- idbStore = idbStore;
2678
2717
  let request = idbStore.get(key);
2679
2718
  request.onsuccess = function (event) {
2680
2719
  let target = event.target;
@@ -2685,8 +2724,8 @@ var Utils = (function () {
2685
2724
  if (data) {
2686
2725
  resolve({
2687
2726
  success: true,
2688
- code: that.#errorCode.success.code,
2689
- msg: that.#errorCode.success.msg,
2727
+ code: that.#statusCode.operationSuccess.code,
2728
+ msg: that.#statusCode.operationSuccess.msg,
2690
2729
  data: data,
2691
2730
  event: event,
2692
2731
  result: result,
@@ -2695,8 +2734,8 @@ var Utils = (function () {
2695
2734
  else {
2696
2735
  resolve({
2697
2736
  success: false,
2698
- code: that.#errorCode.error.code,
2699
- msg: that.#errorCode.error.msg,
2737
+ code: that.#statusCode.operationFailed.code,
2738
+ msg: that.#statusCode.operationFailed.msg,
2700
2739
  data: void 0,
2701
2740
  event: event,
2702
2741
  result: result,
@@ -2704,12 +2743,10 @@ var Utils = (function () {
2704
2743
  }
2705
2744
  };
2706
2745
  request.onerror = function (event) {
2707
- // @ts-ignore
2708
- event.target;
2709
2746
  resolve({
2710
2747
  success: false,
2711
- code: that.#errorCode.get.code,
2712
- msg: that.#errorCode.get.msg,
2748
+ code: that.#statusCode.getFailed.code,
2749
+ msg: that.#statusCode.getFailed.msg,
2713
2750
  data: void 0,
2714
2751
  event: event,
2715
2752
  });
@@ -2720,7 +2757,7 @@ var Utils = (function () {
2720
2757
  }
2721
2758
  /**
2722
2759
  * 正则获取数据
2723
- * @param key 数据键
2760
+ * @param key 数据key,可以是正则
2724
2761
  */
2725
2762
  async regexpGet(key) {
2726
2763
  let list = [];
@@ -2728,46 +2765,45 @@ var Utils = (function () {
2728
2765
  return new Promise((resolve) => {
2729
2766
  /* 正则查询 */
2730
2767
  let dbName = that.#dbName;
2731
- that.open(function (idbStore, success) {
2768
+ this.open(function (idbStore) {
2732
2769
  /* 判断返回的数据中是否有error字段 */
2733
- if (!success) {
2770
+ if (idbStore == null) {
2734
2771
  resolve({
2735
2772
  success: false,
2736
- code: that.#errorCode.regexpGet.code,
2737
- msg: that.#errorCode.regexpGet.msg,
2773
+ code: that.#statusCode.regexpGetFailed.code,
2774
+ msg: that.#statusCode.regexpGetFailed.msg,
2738
2775
  data: [],
2739
2776
  });
2740
2777
  }
2741
2778
  else {
2742
- idbStore = idbStore;
2743
2779
  let request = idbStore.getAll();
2744
2780
  request.onsuccess = function (event) {
2745
2781
  let target = event.target;
2746
2782
  let result = target.result;
2747
2783
  if (result.length !== 0) {
2748
- result.forEach((item, index) => {
2749
- if (item["key"].match(key)) {
2750
- let concatList = item["value"];
2751
- concatList["key"] = item["key"];
2752
- list = [...list, concatList];
2784
+ result.forEach((dataItem, index) => {
2785
+ // 当前项的key
2786
+ let __key = dataItem["key"];
2787
+ // 当前项的value
2788
+ let __value = dataItem["value"];
2789
+ if (__key.match(key)) {
2790
+ list = list.concat(__value);
2753
2791
  }
2754
2792
  });
2755
2793
  }
2756
2794
  resolve({
2757
2795
  success: true,
2758
- code: that.#errorCode.success.code,
2759
- msg: that.#errorCode.success.msg,
2796
+ code: that.#statusCode.operationSuccess.code,
2797
+ msg: that.#statusCode.operationSuccess.msg,
2760
2798
  data: list,
2761
2799
  event: event,
2762
2800
  });
2763
2801
  };
2764
2802
  request.onerror = function (event) {
2765
- // @ts-ignore
2766
- event.target;
2767
2803
  resolve({
2768
2804
  success: false,
2769
- code: that.#errorCode.get.code,
2770
- msg: that.#errorCode.get.msg,
2805
+ code: that.#statusCode.getFailed.code,
2806
+ msg: that.#statusCode.getFailed.msg,
2771
2807
  data: [],
2772
2808
  event: event,
2773
2809
  });
@@ -2778,51 +2814,37 @@ var Utils = (function () {
2778
2814
  }
2779
2815
  /**
2780
2816
  * 删除数据
2781
- * @param {string} key 数据键
2817
+ * @param key 数据key
2782
2818
  */
2783
2819
  async delete(key) {
2784
2820
  let that = this;
2785
2821
  return new Promise((resolve) => {
2786
2822
  /* 根据key删除某条数据 */
2787
2823
  let dbName = that.#dbName;
2788
- that.open(function (idbStore, success) {
2789
- if (!success) {
2824
+ this.open(function (idbStore) {
2825
+ if (idbStore == null) {
2790
2826
  resolve({
2791
2827
  success: false,
2792
- code: that.#errorCode.delete.code,
2793
- msg: that.#errorCode.delete.msg,
2828
+ code: that.#statusCode.deleteFailed.code,
2829
+ msg: that.#statusCode.deleteFailed.msg,
2794
2830
  });
2795
2831
  }
2796
2832
  else {
2797
- idbStore = idbStore;
2798
- let request = idbStore.get(key);
2833
+ // 删除键
2834
+ let request = idbStore.delete(key);
2799
2835
  request.onsuccess = function (event) {
2800
- let target = event.target;
2801
- let recode = target.result;
2802
- if (recode) {
2803
- /* 成功 */
2804
- request = idbStore.delete(key);
2805
- resolve({
2806
- success: true,
2807
- code: that.#errorCode.success.code,
2808
- msg: that.#errorCode.success.msg,
2809
- });
2810
- }
2811
- else {
2812
- resolve({
2813
- success: false,
2814
- code: that.#errorCode.error.code,
2815
- msg: that.#errorCode.error.msg,
2816
- });
2817
- }
2836
+ resolve({
2837
+ success: true,
2838
+ code: that.#statusCode.operationSuccess.code,
2839
+ msg: that.#statusCode.operationSuccess.msg,
2840
+ event: event,
2841
+ });
2818
2842
  };
2819
2843
  request.onerror = function (event) {
2820
- // @ts-ignore
2821
- event.target;
2822
2844
  resolve({
2823
2845
  success: false,
2824
- code: that.#errorCode.delete.code,
2825
- msg: that.#errorCode.delete.msg,
2846
+ code: that.#statusCode.deleteFailed.code,
2847
+ msg: that.#statusCode.deleteFailed.msg,
2826
2848
  event: event,
2827
2849
  });
2828
2850
  };
@@ -2838,22 +2860,33 @@ var Utils = (function () {
2838
2860
  return new Promise((resolve) => {
2839
2861
  /* 清空数据库 */
2840
2862
  let dbName = that.#dbName;
2841
- that.open(function (idbStore, success) {
2842
- if (!success) {
2863
+ this.open(function (idbStore) {
2864
+ if (idbStore == null) {
2843
2865
  resolve({
2844
2866
  success: false,
2845
- code: that.#errorCode.deleteAll.code,
2846
- msg: that.#errorCode.deleteAll.msg,
2867
+ code: that.#statusCode.deleteAllFailed.code,
2868
+ msg: that.#statusCode.deleteAllFailed.msg,
2847
2869
  });
2848
2870
  }
2849
2871
  else {
2850
- idbStore = idbStore;
2851
- idbStore.clear();
2852
- resolve({
2853
- success: true,
2854
- code: that.#errorCode.success.code,
2855
- msg: that.#errorCode.success.msg,
2856
- });
2872
+ // 清空
2873
+ let operateResult = idbStore.clear();
2874
+ operateResult.onsuccess = function (event) {
2875
+ resolve({
2876
+ success: true,
2877
+ code: that.#statusCode.operationSuccess.code,
2878
+ msg: that.#statusCode.operationSuccess.msg,
2879
+ event: event,
2880
+ });
2881
+ };
2882
+ operateResult.onerror = function (event) {
2883
+ resolve({
2884
+ success: false,
2885
+ code: that.#statusCode.deleteAllFailed.code,
2886
+ msg: that.#statusCode.deleteAllFailed.msg,
2887
+ event: event,
2888
+ });
2889
+ };
2857
2890
  }
2858
2891
  }, dbName);
2859
2892
  });