qmwts 1.1.77 → 1.1.79

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.
@@ -29,7 +29,8 @@ declare const _default: {
29
29
  * @param value 获取元素唯一标识的函数
30
30
  * @param override 遇到重复元素是否覆盖
31
31
  */
32
- uniqueBy<T, K = unknown>(array: T[] | undefined, value: (item: T) => K, override?: boolean): T[];
32
+ uniqueByKey<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
+ mergeByKey<T extends object>(keyExtractor: (item: T) => string | number | symbol, ...arrays: T[][]): T[];
34
35
  };
35
36
  export default _default;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lodash_es_1 = require("lodash-es");
3
4
  exports.default = {
4
5
  /**
5
6
  * 数组转化为树形结构
@@ -9,6 +10,7 @@ exports.default = {
9
10
  * @param childrenKey 下级字段
10
11
  */
11
12
  treeify(array = [], idKey = 'id', parentKey = 'parentId', childrenKey = 'children') {
13
+ var _a;
12
14
  const map = new Map(); // 先根据每项的parentId将该项放入Map
13
15
  const ids = []; // 记录所有的id,parentId不在这个集合内则说明是最上级
14
16
  const length = array.length;
@@ -17,7 +19,7 @@ exports.default = {
17
19
  if (e == null)
18
20
  continue;
19
21
  const id = e[idKey], pid = e[parentKey];
20
- const children = map.get(pid) ?? [];
22
+ const children = (_a = map.get(pid)) !== null && _a !== void 0 ? _a : [];
21
23
  children.push(e);
22
24
  map.set(pid, children);
23
25
  ids.push(id);
@@ -89,8 +91,8 @@ exports.default = {
89
91
  * @param value 获取元素唯一标识的函数
90
92
  * @param override 遇到重复元素是否覆盖
91
93
  */
92
- uniqueBy(array = [], value, override = false) {
93
- array = array ?? [];
94
+ uniqueByKey(array = [], value, override = false) {
95
+ array = array !== null && array !== void 0 ? array : [];
94
96
  const map = new Map();
95
97
  for (let i = 0; i < array.length; i++) {
96
98
  const e = array[i];
@@ -115,5 +117,34 @@ exports.default = {
115
117
  }
116
118
  return 0;
117
119
  });
120
+ },
121
+ mergeByKey(keyExtractor, // 限定key类型,避免非可哈希值
122
+ ...arrays) {
123
+ // 1. 空值防御:过滤空数组,避免无效遍历
124
+ const validArrays = arrays.filter(arr => Array.isArray(arr) && arr.length > 0);
125
+ if (validArrays.length === 0)
126
+ return [];
127
+ const mergeMap = new Map();
128
+ // 2. 扁平化数组并遍历(仅处理有效数组,提升性能)
129
+ const arr = validArrays.flat();
130
+ for (let i = 0; i < arr.length; i++) {
131
+ const item = arr[i];
132
+ // 3. 空项防御:跳过undefined/null项
133
+ if (!item)
134
+ continue;
135
+ const key = keyExtractor(item);
136
+ const existingItem = mergeMap.get(key);
137
+ if (existingItem) {
138
+ // 4. 深度合并:保留先插入的值(existingItem在前,item在后不覆盖)
139
+ // 使用lodash的merge,保证深度合并;空对象作为目标,避免修改原对象
140
+ mergeMap.set(key, (0, lodash_es_1.merge)({}, existingItem, item)); // 元素在后的覆盖前面的
141
+ }
142
+ else {
143
+ // 5. 首次插入:浅拷贝原对象,避免外部修改影响map内数据
144
+ mergeMap.set(key, Object.assign({}, item));
145
+ }
146
+ }
147
+ // 6. 转换为数组返回
148
+ return Array.from(mergeMap.values());
118
149
  }
119
150
  };
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  const axios_1 = require("axios");
4
13
  const param_builder_1 = require("./param-builder");
@@ -16,7 +25,7 @@ instance.interceptors.request.use(request => {
16
25
  }, error => {
17
26
  return Promise.reject(error);
18
27
  });
19
- instance.interceptors.response.use(async (response) => {
28
+ instance.interceptors.response.use((response) => __awaiter(void 0, void 0, void 0, function* () {
20
29
  if (response.config.responseType === 'blob') {
21
30
  const { data } = response;
22
31
  if (data.type === 'application/x-download') { // 不是json说明下载成功
@@ -31,11 +40,11 @@ instance.interceptors.response.use(async (response) => {
31
40
  return Promise.resolve(response);
32
41
  }
33
42
  else if (data.type === 'application/json') { // 是json说明有报错
34
- response.data = await new Response(data).json();
43
+ response.data = yield new Response(data).json();
35
44
  }
36
45
  }
37
46
  return response;
38
- }, error => {
47
+ }), error => {
39
48
  return Promise.reject(error);
40
49
  });
41
50
  exports.default = instance;
@@ -1,28 +1,39 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  const JSZip = require("jszip");
4
13
  const file_utils_1 = require("./file-utils");
5
14
  exports.default = {
6
- async zip(urls) {
7
- const { fetch, URL } = window;
8
- if (!fetch) {
9
- alert('您的浏览器不支持fetch');
10
- return;
11
- }
12
- urls = [urls].flat(Infinity);
13
- const zip = new JSZip();
14
- let index = 1;
15
- for (const url of urls) {
16
- const response = await fetch(url);
17
- const blob = await response.blob();
18
- zip.file([index++, file_utils_1.default.fileType(url)].filter(e => !!e).join('.'), blob);
19
- }
20
- zip.generateAsync({ type: 'blob' }).then(blob => {
21
- const a = document.createElement('a');
22
- a.href = URL.createObjectURL(blob);
23
- a.click();
24
- URL.revokeObjectURL(a.href);
25
- a.remove();
15
+ zip(urls) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ const { fetch, URL } = window;
18
+ if (!fetch) {
19
+ alert('您的浏览器不支持fetch');
20
+ return;
21
+ }
22
+ urls = [urls].flat(Infinity);
23
+ const zip = new JSZip();
24
+ let index = 1;
25
+ for (const url of urls) {
26
+ const response = yield fetch(url);
27
+ const blob = yield response.blob();
28
+ zip.file([index++, file_utils_1.default.fileType(url)].filter(e => !!e).join('.'), blob);
29
+ }
30
+ zip.generateAsync({ type: 'blob' }).then(blob => {
31
+ const a = document.createElement('a');
32
+ a.href = URL.createObjectURL(blob);
33
+ a.click();
34
+ URL.revokeObjectURL(a.href);
35
+ a.remove();
36
+ });
26
37
  });
27
38
  }
28
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qmwts",
3
- "version": "1.1.77",
3
+ "version": "1.1.79",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,6 +11,7 @@
11
11
  "author": "qmw",
12
12
  "license": "ISC",
13
13
  "devDependencies": {
14
+ "@types/lodash-es": "^4.17.12",
14
15
  "axios": "*",
15
16
  "typescript": "^5.9.2",
16
17
  "vitest": "^3.2.4"
@@ -19,7 +20,8 @@
19
20
  "dist"
20
21
  ],
21
22
  "dependencies": {
22
- "jszip": "^3.10.1"
23
+ "jszip": "^3.10.1",
24
+ "lodash-es": "^4.17.22"
23
25
  },
24
26
  "peerDependencies": {
25
27
  "axios": "^1.12.2"