befly-vite 1.1.12 → 1.1.13

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.
Files changed (2) hide show
  1. package/package.json +16 -16
  2. package/utils/arrayToTree.js +59 -14
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "befly-vite",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
+ "gitHead": "3a0df4a847cf3da97bb2d09455fabba62027e6c0",
4
5
  "private": false,
5
6
  "description": "Befly Vite 配置预设和插件集合",
6
7
  "keywords": [
@@ -11,22 +12,26 @@
11
12
  "vue"
12
13
  ],
13
14
  "homepage": "https://chensuiyi.me",
14
- "author": "chensuiyi <bimostyle@qq.com>",
15
15
  "license": "Apache-2.0",
16
- "type": "module",
17
- "main": "index.js",
18
- "exports": {
19
- ".": "./index.js",
20
- "./utils/*": "./utils/*.js"
21
- },
16
+ "author": "chensuiyi <bimostyle@qq.com>",
22
17
  "files": [
23
- "README.md",
24
- "configs/",
25
18
  "index.js",
26
19
  "package.json",
20
+ "README.md",
21
+ "configs/",
27
22
  "plugins/",
28
23
  "utils/"
29
24
  ],
25
+ "type": "module",
26
+ "main": "index.js",
27
+ "exports": {
28
+ ".": "./index.js",
29
+ "./utils/*": "./utils/*.js"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public",
33
+ "registry": "https://registry.npmjs.org"
34
+ },
30
35
  "dependencies": {
31
36
  "@unocss/preset-attributify": "^66.5.10",
32
37
  "@unocss/preset-uno": "^66.5.10",
@@ -48,10 +53,5 @@
48
53
  },
49
54
  "engines": {
50
55
  "bun": ">=1.3.0"
51
- },
52
- "publishConfig": {
53
- "access": "public",
54
- "registry": "https://registry.npmjs.org"
55
- },
56
- "gitHead": "d42bf60ba6b8b11fef7e759fa008ada075829772"
56
+ }
57
57
  }
@@ -4,6 +4,7 @@
4
4
  * @property {string=} pidField
5
5
  * @property {string=} childrenField
6
6
  * @property {any=} rootPid
7
+ * @property {string=} sortField
7
8
  * @property {(node: any) => any=} mapFn
8
9
  */
9
10
 
@@ -18,29 +19,50 @@ export function arrayToTree(items, options = {}) {
18
19
  const pidField = typeof options.pidField === "string" ? options.pidField : "pid";
19
20
  const childrenField = typeof options.childrenField === "string" ? options.childrenField : "children";
20
21
  const rootPid = "rootPid" in options ? options.rootPid : 0;
22
+ const sortField = "sortField" in options ? (typeof options.sortField === "string" && options.sortField.length > 0 ? options.sortField : null) : "sort";
21
23
  const mapFn = typeof options.mapFn === "function" ? options.mapFn : null;
22
24
 
23
- /** @type {T[]} */
24
- const tree = [];
25
+ /**
26
+ * pid -> items[]
27
+ * @type {Map<any, T[]>}
28
+ */
29
+ const pidMap = new Map();
25
30
 
26
31
  for (const item of items) {
27
32
  // @ts-ignore
28
- const pid = item[pidField];
33
+ const pid = item ? item[pidField] : undefined;
34
+ const list = pidMap.get(pid);
35
+ if (list) {
36
+ list.push(item);
37
+ } else {
38
+ pidMap.set(pid, [item]);
39
+ }
40
+ }
29
41
 
30
- if (Object.is(pid, rootPid)) {
42
+ /**
43
+ * @param {any} pid
44
+ * @param {Set<any>} stack
45
+ * @returns {T[]}
46
+ */
47
+ const build = (pid, stack) => {
48
+ /** @type {T[]} */
49
+ const tree = [];
50
+ const list = pidMap.get(pid) || [];
51
+
52
+ for (const item of list) {
31
53
  const node = Object.assign({}, item);
32
54
  const mappedNode = mapFn ? mapFn(node) : node;
33
55
 
34
56
  // 子节点 rootPid = node[id]
35
57
  // @ts-ignore
36
- const nextRootPid = mappedNode[idField];
37
- const children = arrayToTree(items, {
38
- idField: idField,
39
- pidField: pidField,
40
- childrenField: childrenField,
41
- rootPid: nextRootPid,
42
- mapFn: mapFn
43
- });
58
+ const nextRootPid = mappedNode ? mappedNode[idField] : undefined;
59
+
60
+ let children = [];
61
+ if (!stack.has(nextRootPid)) {
62
+ stack.add(nextRootPid);
63
+ children = build(nextRootPid, stack);
64
+ stack.delete(nextRootPid);
65
+ }
44
66
 
45
67
  if (children.length > 0) {
46
68
  // @ts-ignore
@@ -49,7 +71,30 @@ export function arrayToTree(items, options = {}) {
49
71
 
50
72
  tree.push(mappedNode);
51
73
  }
52
- }
53
74
 
54
- return tree;
75
+ if (sortField) {
76
+ tree.sort((a, b) => {
77
+ // @ts-ignore
78
+ const av = a ? a[sortField] : undefined;
79
+ // @ts-ignore
80
+ const bv = b ? b[sortField] : undefined;
81
+
82
+ const aMissing = av === undefined || av === null;
83
+ const bMissing = bv === undefined || bv === null;
84
+ if (aMissing && bMissing) return 0;
85
+ if (aMissing) return 1;
86
+ if (bMissing) return -1;
87
+
88
+ if (typeof av === "number" && typeof bv === "number") {
89
+ return av - bv;
90
+ }
91
+
92
+ return String(av).localeCompare(String(bv), undefined, { numeric: true, sensitivity: "base" });
93
+ });
94
+ }
95
+
96
+ return tree;
97
+ };
98
+
99
+ return build(rootPid, new Set());
55
100
  }