qmwts 1.1.74 → 1.1.76

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.
@@ -26,10 +26,10 @@ declare const _default: {
26
26
  /**
27
27
  * 根据数组元素中的某一属性进行去重
28
28
  * @param array
29
- * @param keyName
29
+ * @param value 获取元素唯一标识的函数
30
30
  * @param override 遇到重复元素是否覆盖
31
31
  */
32
- uniqueBy<T>(array: T[] | undefined, keyName: string, override?: boolean): T[];
32
+ uniqueBy<T, K = unknown>(array: T[] | undefined, value: (item: T) => K, override?: boolean): T[];
33
33
  sort<T>(array: T[], order: "asc" | "desc", ...values: ((item: T) => number)[]): void;
34
34
  };
35
35
  export default _default;
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const object_utils_1 = require("./object-utils");
4
3
  exports.default = {
5
4
  /**
6
5
  * 数组转化为树形结构
@@ -14,8 +13,11 @@ exports.default = {
14
13
  const ids = []; // 记录所有的id,parentId不在这个集合内则说明是最上级
15
14
  const length = array.length;
16
15
  for (let i = 0; i < length; i++) {
17
- const e = array[i], id = e[idKey], pid = e[parentKey];
18
- const children = map.get(pid) || [];
16
+ const e = array[i];
17
+ if (e == null)
18
+ continue;
19
+ const id = e[idKey], pid = e[parentKey];
20
+ const children = map.get(pid) ?? [];
19
21
  children.push(e);
20
22
  map.set(pid, children);
21
23
  ids.push(id);
@@ -84,18 +86,17 @@ exports.default = {
84
86
  /**
85
87
  * 根据数组元素中的某一属性进行去重
86
88
  * @param array
87
- * @param keyName
89
+ * @param value 获取元素唯一标识的函数
88
90
  * @param override 遇到重复元素是否覆盖
89
91
  */
90
- uniqueBy(array = [], keyName, override = false) {
92
+ uniqueBy(array = [], value, override = false) {
93
+ array = array ?? [];
91
94
  const map = new Map();
92
- const length = array.length;
93
- for (let i = 0; i < length; i++) {
95
+ for (let i = 0; i < array.length; i++) {
94
96
  const e = array[i];
95
- const key = object_utils_1.default.optionalChaining(e, keyName);
96
- if (!override && map.has(key))
97
- continue;
98
- map.set(key, e);
97
+ const key = value(e);
98
+ if (!map.has(key) || override)
99
+ map.set(key, e);
99
100
  }
100
101
  return Array.from(map.values());
101
102
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qmwts",
3
- "version": "1.1.74",
3
+ "version": "1.1.76",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",