@whitesev/utils 2.1.1 → 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.
package/dist/index.umd.js CHANGED
@@ -2639,8 +2639,6 @@
2639
2639
  let request = idbStore.put(inData);
2640
2640
  request.onsuccess = function (event) {
2641
2641
  /* 保存成功有success 字段 */
2642
- // @ts-ignore
2643
- event.target;
2644
2642
  resolve({
2645
2643
  success: true,
2646
2644
  code: that.#errorCode.success.code,
@@ -2649,8 +2647,6 @@
2649
2647
  });
2650
2648
  };
2651
2649
  request.onerror = function (event) {
2652
- // @ts-ignore
2653
- event.target;
2654
2650
  resolve({
2655
2651
  success: false,
2656
2652
  code: that.#errorCode.save.code,
@@ -2662,6 +2658,48 @@
2662
2658
  }, dbName);
2663
2659
  });
2664
2660
  }
2661
+ /**
2662
+ * 判断是否存在该数据
2663
+ * @param key 数据key
2664
+ */
2665
+ async has(key) {
2666
+ let that = this;
2667
+ return new Promise((resolve) => {
2668
+ let dbName = this.#dbName;
2669
+ this.open(function (idbStore, success) {
2670
+ /* 判断返回的数据中是否有error字段 */
2671
+ if (!success) {
2672
+ resolve({
2673
+ success: false,
2674
+ code: that.#errorCode.get.code,
2675
+ msg: that.#errorCode.get.msg,
2676
+ });
2677
+ }
2678
+ else {
2679
+ idbStore = idbStore;
2680
+ let request = idbStore.get(key);
2681
+ request.onsuccess = function (event) {
2682
+ /* result 返回的是 {key: string, value: any} */
2683
+ /* 键值对存储 */
2684
+ resolve({
2685
+ success: true,
2686
+ code: that.#errorCode.success.code,
2687
+ msg: that.#errorCode.success.msg,
2688
+ event: event,
2689
+ });
2690
+ };
2691
+ request.onerror = function (event) {
2692
+ resolve({
2693
+ success: false,
2694
+ code: that.#errorCode.get.code,
2695
+ msg: that.#errorCode.get.msg,
2696
+ event: event,
2697
+ });
2698
+ };
2699
+ }
2700
+ }, dbName);
2701
+ });
2702
+ }
2665
2703
  /**
2666
2704
  * 根据key获取值
2667
2705
  * @param key 数据key
@@ -2669,8 +2707,8 @@
2669
2707
  async get(key) {
2670
2708
  let that = this;
2671
2709
  return new Promise((resolve) => {
2672
- let dbName = that.#dbName;
2673
- that.open(function (idbStore, success) {
2710
+ let dbName = this.#dbName;
2711
+ this.open(function (idbStore, success) {
2674
2712
  /* 判断返回的数据中是否有error字段 */
2675
2713
  if (!success) {
2676
2714
  resolve({
@@ -2727,7 +2765,7 @@
2727
2765
  }
2728
2766
  /**
2729
2767
  * 正则获取数据
2730
- * @param key 数据键
2768
+ * @param key 数据key,可以是正则
2731
2769
  */
2732
2770
  async regexpGet(key) {
2733
2771
  let list = [];
@@ -2752,11 +2790,13 @@
2752
2790
  let target = event.target;
2753
2791
  let result = target.result;
2754
2792
  if (result.length !== 0) {
2755
- result.forEach((item, index) => {
2756
- if (item["key"].match(key)) {
2757
- let concatList = item["value"];
2758
- concatList["key"] = item["key"];
2759
- list = [...list, concatList];
2793
+ result.forEach((dataItem, index) => {
2794
+ // 当前项的key
2795
+ let __key = dataItem["key"];
2796
+ // 当前项的value
2797
+ let __value = dataItem["value"];
2798
+ if (__key.match(key)) {
2799
+ list = list.concat(__value);
2760
2800
  }
2761
2801
  });
2762
2802
  }
@@ -2785,7 +2825,7 @@
2785
2825
  }
2786
2826
  /**
2787
2827
  * 删除数据
2788
- * @param {string} key 数据键
2828
+ * @param key 数据key
2789
2829
  */
2790
2830
  async delete(key) {
2791
2831
  let that = this;