qmwts 1.1.54 → 1.1.55

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.
@@ -23,5 +23,12 @@ declare const _default: {
23
23
  * @param parentKey parent的key
24
24
  */
25
25
  getDescendants<T>(array: any[] | undefined, id: any, idKey?: string, parentKey?: string): T[];
26
+ /**
27
+ * 根据数组中的某一属性去重
28
+ * @param array
29
+ * @param keyName
30
+ * @param onDuplicate key重复是的行为,override=覆盖,ignore=忽略
31
+ */
32
+ uniqueBy<T>(array?: any[], keyName?: any, onDuplicate?: "override" | "ignore"): T[];
26
33
  };
27
34
  export default _default;
@@ -91,12 +91,32 @@ exports.default = {
91
91
  for (var i = array.length - 1; i >= 0; i--) {
92
92
  var e = array[i], itemId = e[idKey], pid = e[parentKey];
93
93
  if (pid === currentId) {
94
- result.push(e);
94
+ result.unshift(e);
95
95
  dfs(itemId);
96
96
  }
97
97
  }
98
98
  };
99
99
  dfs(id);
100
100
  return result;
101
+ },
102
+ /**
103
+ * 根据数组中的某一属性去重
104
+ * @param array
105
+ * @param keyName
106
+ * @param onDuplicate key重复是的行为,override=覆盖,ignore=忽略
107
+ */
108
+ uniqueBy: function (array, keyName, onDuplicate) {
109
+ if (array === void 0) { array = []; }
110
+ if (keyName === void 0) { keyName = 'id'; }
111
+ if (onDuplicate === void 0) { onDuplicate = 'ignore'; }
112
+ var map = new Map();
113
+ for (var i = array.length - 1; i >= 0; i--) {
114
+ var e = array[i];
115
+ var key = e[keyName];
116
+ if (onDuplicate === 'ignore' && map.has(key))
117
+ continue;
118
+ map.set(key, e);
119
+ }
120
+ return Array.from(map.values()).reverse();
101
121
  }
102
122
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qmwts",
3
- "version": "1.1.54",
3
+ "version": "1.1.55",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",