a2bei4-utils 1.0.3 → 1.0.5

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"menu.cjs","sources":["../src/source/menu.js"],"sourcesContent":["/**\r\n * 处理数据库菜单项,生成 UI 菜单树和路由配置。\r\n *\r\n * @param {Array<Object>} menuItems - 原始菜单项数组,树形结构\r\n * @param {Object} [options] - 配置选项\r\n * @param {string} [options.idKey='id'] - 数据源 ID 键名\r\n * @param {string} [options.codeKey='code'] - 数据源编码键名, 用于拼接路由名称和路由路径\r\n * @param {string} [options.labelKey='text'] - 数据源标签键名\r\n * @param {string} [options.childrenKey='children'] - 数据源子节点键名\r\n * @param {string} [options.extendKey='extend'] - 数据源扩展对象键名\r\n * @param {string} [options.menuIdKey='key'] - 输出菜单 ID 键名\r\n * @param {string} [options.menuLabelKey='label'] - 输出菜单标签键名\r\n * @param {string} [options.menuChildrenKey='children'] - 输出菜单子节点键名\r\n * @param {string} [options.menuExtendKey='extend'] - 输出菜单扩展对象键名\r\n * @param {string} [options.routeNameKey='name'] - 路由名称键名\r\n * @param {string} [options.routePathKey='path'] - 路由路径键名\r\n * @param {string} [options.routeMetaKey='meta'] - 路由元数据键名\r\n * @param {Function} [options.handleNodeItem] - 节点处理钩子,参数:(nodeSimple) => void\r\n * @param {Function} [options.handleMenuItem] - 菜单项处理钩子,参数:(uiMenuItem, nodeSimple) => void\r\n * @param {Function} [options.handleRouteItem] - 路由项处理钩子,参数:(routeItem, nodeSimple) => void\r\n *\r\n * @returns {Object} 处理结果\r\n * @returns {Array<Object>} returns.uiMenuItems - UI 菜单树形结构数组\r\n * @returns {Array<Object>} returns.routeItems - 扁平化路由配置数组\r\n * @returns {Map<string, Object>} returns.nodeMap - 节点 ID 到节点数据的映射表\r\n *\r\n * @example\r\n * const menuItems = [\r\n * { id: '1', code: 'system', text: '系统管理', children: [\r\n * { id: '1-1', code: 'user', text: '用户管理' }\r\n * ]}\r\n * ];\r\n *\r\n * const result = handleDbMenuItems(menuItems, {\r\n * handleRouteItem: (route, node) => {\r\n * route.component = () => import(`./views/${node.code}.vue`);\r\n * }\r\n * });\r\n *\r\n * // result.uiMenuItems: [{ key: '1', label: '系统管理', children: [...] }]\r\n * // result.routeItems: [{ name: 'system.user', path: '/system/user', meta: { id: '1-1' } }]\r\n * // result.nodeMap: Map { '1' => {...}, '1-1' => {...} }\r\n */\r\nexport function handleDbMenuItems(menuItems, options = {}) {\r\n // 1. 统一配置项,简化调用\r\n const {\r\n // 数据源键名\r\n idKey = \"id\",\r\n codeKey = \"code\",\r\n labelKey = \"text\",\r\n childrenKey = \"children\",\r\n extendKey = \"extend\",\r\n\r\n // 输出目标键名\r\n menuIdKey = \"key\",\r\n menuLabelKey = \"label\",\r\n menuChildrenKey = \"children\",\r\n menuExtendKey = \"extend\",\r\n\r\n routeNameKey = \"name\",\r\n routePathKey = \"path\",\r\n routeMetaKey = \"meta\",\r\n\r\n // 钩子函数\r\n handleNodeItem,\r\n handleMenuItem,\r\n handleRouteItem\r\n } = options;\r\n\r\n const uiMenuItems = [];\r\n const routeItems = [];\r\n const nodeMap = new Map(); // id -> node (纯净的节点数据)\r\n const nodePathMap = new Map(); // id -> [{ code, id }] (仅存储路径计算所需的轻量数据)\r\n\r\n /* ---------- 1. 建立索引:扁平化节点 & 构建路径索引 ---------- */\r\n // 使用栈进行深度优先遍历,记录路径\r\n const stack = [];\r\n if (Array.isArray(menuItems)) {\r\n menuItems.forEach((n) => stack.push({ node: n, parentPath: [] }));\r\n }\r\n\r\n while (stack.length) {\r\n const { node, parentPath } = stack.pop();\r\n const idValue = node[idKey];\r\n\r\n // 构建当前节点的路径片段\r\n const currentPathSegment = { [idKey]: idValue };\r\n const fullPath = [...parentPath, currentPathSegment];\r\n\r\n // 存储路径信息用于后续路由生成\r\n nodePathMap.set(idValue, fullPath);\r\n\r\n // 创建轻量级的节点对象存入 nodeMap\r\n // 注意:这里先创建基础结构,路由信息在第二步补充\r\n const nodeSimple = {\r\n ...node,\r\n [extendKey]: {}, // 初始化扩展对象\r\n [childrenKey]: null // 去除原始 children,避免引用污染\r\n };\r\n nodeMap.set(idValue, nodeSimple);\r\n\r\n // 处理子节点\r\n const childrenNodes = node[childrenKey];\r\n if (Array.isArray(childrenNodes)) {\r\n // 逆序压栈,保持原序\r\n for (let i = childrenNodes.length - 1; i >= 0; i--) {\r\n stack.push({ node: childrenNodes[i], parentPath: fullPath });\r\n }\r\n }\r\n }\r\n\r\n /* ---------- 2. 构建树形结构 & 生成路由 ---------- */\r\n const buildStack = [];\r\n if (Array.isArray(menuItems)) {\r\n for (let i = menuItems.length - 1; i >= 0; i--) {\r\n buildStack.push({ node: menuItems[i], parentUi: undefined });\r\n }\r\n }\r\n\r\n while (buildStack.length) {\r\n const { node, parentUi } = buildStack.pop();\r\n const idValue = node[idKey];\r\n\r\n // 从 Map 中取出之前创建好的节点对象\r\n const nodeSimple = nodeMap.get(idValue);\r\n\r\n // 2.1 构建 UI 菜单项\r\n const uiMenuItem = {\r\n [menuIdKey]: idValue,\r\n [menuLabelKey]: node[labelKey],\r\n [menuExtendKey]: {} // UI 菜单专用扩展\r\n };\r\n\r\n // 处理子节点\r\n const childrenNodes = node[childrenKey];\r\n const hasChildren = Array.isArray(childrenNodes) && childrenNodes.length > 0;\r\n\r\n if (hasChildren) {\r\n // 有子节点 -> 继续压栈,传递当前 uiMenuItem 作为父级\r\n for (let i = childrenNodes.length - 1; i >= 0; i--) {\r\n buildStack.push({ node: childrenNodes[i], parentUi: uiMenuItem });\r\n }\r\n } else {\r\n // 2.2 无子节点 -> 生成路由配置\r\n\r\n // 提取 code 和 id 数组\r\n const codePaths = [],\r\n keyPaths = [];\r\n nodePathMap.get(idValue).forEach((item) => {\r\n const idValue = item[idKey];\r\n const nodeSimple = nodeMap.get(idValue);\r\n keyPaths.push(idValue);\r\n codePaths.push(nodeSimple[codeKey]);\r\n });\r\n\r\n const routeName = codePaths.join(\".\");\r\n const routePath = \"/\" + codePaths.join(\"/\");\r\n\r\n const routeItem = {\r\n [routeNameKey]: routeName,\r\n [routePathKey]: routePath,\r\n [routeMetaKey]: { [idKey]: idValue }\r\n };\r\n\r\n // 执行路由钩子\r\n if (typeof handleRouteItem === \"function\") {\r\n handleRouteItem(routeItem, nodeSimple);\r\n }\r\n routeItems.push(routeItem);\r\n\r\n // 补充扩展信息\r\n uiMenuItem[menuExtendKey].routeName = routeName;\r\n uiMenuItem[menuExtendKey].keyPaths = keyPaths;\r\n\r\n nodeSimple[extendKey].routeName = routeName;\r\n nodeSimple[extendKey].keyPaths = keyPaths;\r\n }\r\n\r\n // 2.3 挂载到父级 UI 或根数组\r\n if (parentUi) {\r\n // 逻辑赋值,确保 children 数组存在\r\n (parentUi[menuChildrenKey] || (parentUi[menuChildrenKey] = [])).push(uiMenuItem);\r\n } else {\r\n if (typeof handleMenuItem === \"function\") {\r\n handleMenuItem(uiMenuItem, nodeSimple);\r\n }\r\n uiMenuItems.push(uiMenuItem);\r\n }\r\n\r\n // 执行节点钩子\r\n if (typeof handleNodeItem === \"function\") {\r\n handleNodeItem(nodeSimple);\r\n }\r\n }\r\n\r\n return { uiMenuItems, routeItems, nodeMap };\r\n}\r\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,iBAAiB,CAAC,SAAS,EAAE,OAAO,GAAG,EAAE,EAAE;AAC3D;AACA,IAAI,MAAM;AACV;AACA,QAAQ,KAAK,GAAG,IAAI;AACpB,QAAQ,OAAO,GAAG,MAAM;AACxB,QAAQ,QAAQ,GAAG,MAAM;AACzB,QAAQ,WAAW,GAAG,UAAU;AAChC,QAAQ,SAAS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AY,CAAC;AACb,YAAY,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;AACzC,QAAQ,CAAC;AACT;AACA;AACA,QAAQ,IAAI,OAAO,cAAc,KAAK,UAAU,EAAE;AAClD,YAAY,cAAc,CAAC,UAAU,CAAC,CAAC;AACvC,QAAQ,CAAC;AACT,IAAI,CAAC;AACL;AACA,IAAI,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;AAChD;;;;"}
package/dist/menu.js ADDED
@@ -0,0 +1,200 @@
1
+ /**
2
+ * 处理数据库菜单项,生成 UI 菜单树和路由配置。
3
+ *
4
+ * @param {Array<Object>} menuItems - 原始菜单项数组,树形结构
5
+ * @param {Object} [options] - 配置选项
6
+ * @param {string} [options.idKey='id'] - 数据源 ID 键名
7
+ * @param {string} [options.codeKey='code'] - 数据源编码键名, 用于拼接路由名称和路由路径
8
+ * @param {string} [options.labelKey='text'] - 数据源标签键名
9
+ * @param {string} [options.childrenKey='children'] - 数据源子节点键名
10
+ * @param {string} [options.extendKey='extend'] - 数据源扩展对象键名
11
+ * @param {string} [options.menuIdKey='key'] - 输出菜单 ID 键名
12
+ * @param {string} [options.menuLabelKey='label'] - 输出菜单标签键名
13
+ * @param {string} [options.menuChildrenKey='children'] - 输出菜单子节点键名
14
+ * @param {string} [options.menuExtendKey='extend'] - 输出菜单扩展对象键名
15
+ * @param {string} [options.routeNameKey='name'] - 路由名称键名
16
+ * @param {string} [options.routePathKey='path'] - 路由路径键名
17
+ * @param {string} [options.routeMetaKey='meta'] - 路由元数据键名
18
+ * @param {Function} [options.handleNodeItem] - 节点处理钩子,参数:(nodeSimple) => void
19
+ * @param {Function} [options.handleMenuItem] - 菜单项处理钩子,参数:(uiMenuItem, nodeSimple) => void
20
+ * @param {Function} [options.handleRouteItem] - 路由项处理钩子,参数:(routeItem, nodeSimple) => void
21
+ *
22
+ * @returns {Object} 处理结果
23
+ * @returns {Array<Object>} returns.uiMenuItems - UI 菜单树形结构数组
24
+ * @returns {Array<Object>} returns.routeItems - 扁平化路由配置数组
25
+ * @returns {Map<string, Object>} returns.nodeMap - 节点 ID 到节点数据的映射表
26
+ *
27
+ * @example
28
+ * const menuItems = [
29
+ * { id: '1', code: 'system', text: '系统管理', children: [
30
+ * { id: '1-1', code: 'user', text: '用户管理' }
31
+ * ]}
32
+ * ];
33
+ *
34
+ * const result = handleDbMenuItems(menuItems, {
35
+ * handleRouteItem: (route, node) => {
36
+ * route.component = () => import(`./views/${node.code}.vue`);
37
+ * }
38
+ * });
39
+ *
40
+ * // result.uiMenuItems: [{ key: '1', label: '系统管理', children: [...] }]
41
+ * // result.routeItems: [{ name: 'system.user', path: '/system/user', meta: { id: '1-1' } }]
42
+ * // result.nodeMap: Map { '1' => {...}, '1-1' => {...} }
43
+ */
44
+ function handleDbMenuItems(menuItems, options = {}) {
45
+ // 1. 统一配置项,简化调用
46
+ const {
47
+ // 数据源键名
48
+ idKey = "id",
49
+ codeKey = "code",
50
+ labelKey = "text",
51
+ childrenKey = "children",
52
+ extendKey = "extend",
53
+
54
+ // 输出目标键名
55
+ menuIdKey = "key",
56
+ menuLabelKey = "label",
57
+ menuChildrenKey = "children",
58
+ menuExtendKey = "extend",
59
+
60
+ routeNameKey = "name",
61
+ routePathKey = "path",
62
+ routeMetaKey = "meta",
63
+
64
+ // 钩子函数
65
+ handleNodeItem,
66
+ handleMenuItem,
67
+ handleRouteItem
68
+ } = options;
69
+
70
+ const uiMenuItems = [];
71
+ const routeItems = [];
72
+ const nodeMap = new Map(); // id -> node (纯净的节点数据)
73
+ const nodePathMap = new Map(); // id -> [{ code, id }] (仅存储路径计算所需的轻量数据)
74
+
75
+ /* ---------- 1. 建立索引:扁平化节点 & 构建路径索引 ---------- */
76
+ // 使用栈进行深度优先遍历,记录路径
77
+ const stack = [];
78
+ if (Array.isArray(menuItems)) {
79
+ menuItems.forEach((n) => stack.push({ node: n, parentPath: [] }));
80
+ }
81
+
82
+ while (stack.length) {
83
+ const { node, parentPath } = stack.pop();
84
+ const idValue = node[idKey];
85
+
86
+ // 构建当前节点的路径片段
87
+ const currentPathSegment = { [idKey]: idValue };
88
+ const fullPath = [...parentPath, currentPathSegment];
89
+
90
+ // 存储路径信息用于后续路由生成
91
+ nodePathMap.set(idValue, fullPath);
92
+
93
+ // 创建轻量级的节点对象存入 nodeMap
94
+ // 注意:这里先创建基础结构,路由信息在第二步补充
95
+ const nodeSimple = {
96
+ ...node,
97
+ [extendKey]: {}, // 初始化扩展对象
98
+ [childrenKey]: null // 去除原始 children,避免引用污染
99
+ };
100
+ nodeMap.set(idValue, nodeSimple);
101
+
102
+ // 处理子节点
103
+ const childrenNodes = node[childrenKey];
104
+ if (Array.isArray(childrenNodes)) {
105
+ // 逆序压栈,保持原序
106
+ for (let i = childrenNodes.length - 1; i >= 0; i--) {
107
+ stack.push({ node: childrenNodes[i], parentPath: fullPath });
108
+ }
109
+ }
110
+ }
111
+
112
+ /* ---------- 2. 构建树形结构 & 生成路由 ---------- */
113
+ const buildStack = [];
114
+ if (Array.isArray(menuItems)) {
115
+ for (let i = menuItems.length - 1; i >= 0; i--) {
116
+ buildStack.push({ node: menuItems[i], parentUi: undefined });
117
+ }
118
+ }
119
+
120
+ while (buildStack.length) {
121
+ const { node, parentUi } = buildStack.pop();
122
+ const idValue = node[idKey];
123
+
124
+ // 从 Map 中取出之前创建好的节点对象
125
+ const nodeSimple = nodeMap.get(idValue);
126
+
127
+ // 2.1 构建 UI 菜单项
128
+ const uiMenuItem = {
129
+ [menuIdKey]: idValue,
130
+ [menuLabelKey]: node[labelKey],
131
+ [menuExtendKey]: {} // UI 菜单专用扩展
132
+ };
133
+
134
+ // 处理子节点
135
+ const childrenNodes = node[childrenKey];
136
+ const hasChildren = Array.isArray(childrenNodes) && childrenNodes.length > 0;
137
+
138
+ if (hasChildren) {
139
+ // 有子节点 -> 继续压栈,传递当前 uiMenuItem 作为父级
140
+ for (let i = childrenNodes.length - 1; i >= 0; i--) {
141
+ buildStack.push({ node: childrenNodes[i], parentUi: uiMenuItem });
142
+ }
143
+ } else {
144
+ // 2.2 无子节点 -> 生成路由配置
145
+
146
+ // 提取 code 和 id 数组
147
+ const codePaths = [],
148
+ keyPaths = [];
149
+ nodePathMap.get(idValue).forEach((item) => {
150
+ const idValue = item[idKey];
151
+ const nodeSimple = nodeMap.get(idValue);
152
+ keyPaths.push(idValue);
153
+ codePaths.push(nodeSimple[codeKey]);
154
+ });
155
+
156
+ const routeName = codePaths.join(".");
157
+ const routePath = "/" + codePaths.join("/");
158
+
159
+ const routeItem = {
160
+ [routeNameKey]: routeName,
161
+ [routePathKey]: routePath,
162
+ [routeMetaKey]: { [idKey]: idValue }
163
+ };
164
+
165
+ // 执行路由钩子
166
+ if (typeof handleRouteItem === "function") {
167
+ handleRouteItem(routeItem, nodeSimple);
168
+ }
169
+ routeItems.push(routeItem);
170
+
171
+ // 补充扩展信息
172
+ uiMenuItem[menuExtendKey].routeName = routeName;
173
+ uiMenuItem[menuExtendKey].keyPaths = keyPaths;
174
+
175
+ nodeSimple[extendKey].routeName = routeName;
176
+ nodeSimple[extendKey].keyPaths = keyPaths;
177
+ }
178
+
179
+ // 2.3 挂载到父级 UI 或根数组
180
+ if (parentUi) {
181
+ // 逻辑赋值,确保 children 数组存在
182
+ (parentUi[menuChildrenKey] || (parentUi[menuChildrenKey] = [])).push(uiMenuItem);
183
+ } else {
184
+ if (typeof handleMenuItem === "function") {
185
+ handleMenuItem(uiMenuItem, nodeSimple);
186
+ }
187
+ uiMenuItems.push(uiMenuItem);
188
+ }
189
+
190
+ // 执行节点钩子
191
+ if (typeof handleNodeItem === "function") {
192
+ handleNodeItem(nodeSimple);
193
+ }
194
+ }
195
+
196
+ return { uiMenuItems, routeItems, nodeMap };
197
+ }
198
+
199
+ export { handleDbMenuItems };
200
+ //# sourceMappingURL=menu.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"menu.js","sources":["../src/source/menu.js"],"sourcesContent":["/**\r\n * 处理数据库菜单项,生成 UI 菜单树和路由配置。\r\n *\r\n * @param {Array<Object>} menuItems - 原始菜单项数组,树形结构\r\n * @param {Object} [options] - 配置选项\r\n * @param {string} [options.idKey='id'] - 数据源 ID 键名\r\n * @param {string} [options.codeKey='code'] - 数据源编码键名, 用于拼接路由名称和路由路径\r\n * @param {string} [options.labelKey='text'] - 数据源标签键名\r\n * @param {string} [options.childrenKey='children'] - 数据源子节点键名\r\n * @param {string} [options.extendKey='extend'] - 数据源扩展对象键名\r\n * @param {string} [options.menuIdKey='key'] - 输出菜单 ID 键名\r\n * @param {string} [options.menuLabelKey='label'] - 输出菜单标签键名\r\n * @param {string} [options.menuChildrenKey='children'] - 输出菜单子节点键名\r\n * @param {string} [options.menuExtendKey='extend'] - 输出菜单扩展对象键名\r\n * @param {string} [options.routeNameKey='name'] - 路由名称键名\r\n * @param {string} [options.routePathKey='path'] - 路由路径键名\r\n * @param {string} [options.routeMetaKey='meta'] - 路由元数据键名\r\n * @param {Function} [options.handleNodeItem] - 节点处理钩子,参数:(nodeSimple) => void\r\n * @param {Function} [options.handleMenuItem] - 菜单项处理钩子,参数:(uiMenuItem, nodeSimple) => void\r\n * @param {Function} [options.handleRouteItem] - 路由项处理钩子,参数:(routeItem, nodeSimple) => void\r\n *\r\n * @returns {Object} 处理结果\r\n * @returns {Array<Object>} returns.uiMenuItems - UI 菜单树形结构数组\r\n * @returns {Array<Object>} returns.routeItems - 扁平化路由配置数组\r\n * @returns {Map<string, Object>} returns.nodeMap - 节点 ID 到节点数据的映射表\r\n *\r\n * @example\r\n * const menuItems = [\r\n * { id: '1', code: 'system', text: '系统管理', children: [\r\n * { id: '1-1', code: 'user', text: '用户管理' }\r\n * ]}\r\n * ];\r\n *\r\n * const result = handleDbMenuItems(menuItems, {\r\n * handleRouteItem: (route, node) => {\r\n * route.component = () => import(`./views/${node.code}.vue`);\r\n * }\r\n * });\r\n *\r\n * // result.uiMenuItems: [{ key: '1', label: '系统管理', children: [...] }]\r\n * // result.routeItems: [{ name: 'system.user', path: '/system/user', meta: { id: '1-1' } }]\r\n * // result.nodeMap: Map { '1' => {...}, '1-1' => {...} }\r\n */\r\nexport function handleDbMenuItems(menuItems, options = {}) {\r\n // 1. 统一配置项,简化调用\r\n const {\r\n // 数据源键名\r\n idKey = \"id\",\r\n codeKey = \"code\",\r\n labelKey = \"text\",\r\n childrenKey = \"children\",\r\n extendKey = \"extend\",\r\n\r\n // 输出目标键名\r\n menuIdKey = \"key\",\r\n menuLabelKey = \"label\",\r\n menuChildrenKey = \"children\",\r\n menuExtendKey = \"extend\",\r\n\r\n routeNameKey = \"name\",\r\n routePathKey = \"path\",\r\n routeMetaKey = \"meta\",\r\n\r\n // 钩子函数\r\n handleNodeItem,\r\n handleMenuItem,\r\n handleRouteItem\r\n } = options;\r\n\r\n const uiMenuItems = [];\r\n const routeItems = [];\r\n const nodeMap = new Map(); // id -> node (纯净的节点数据)\r\n const nodePathMap = new Map(); // id -> [{ code, id }] (仅存储路径计算所需的轻量数据)\r\n\r\n /* ---------- 1. 建立索引:扁平化节点 & 构建路径索引 ---------- */\r\n // 使用栈进行深度优先遍历,记录路径\r\n const stack = [];\r\n if (Array.isArray(menuItems)) {\r\n menuItems.forEach((n) => stack.push({ node: n, parentPath: [] }));\r\n }\r\n\r\n while (stack.length) {\r\n const { node, parentPath } = stack.pop();\r\n const idValue = node[idKey];\r\n\r\n // 构建当前节点的路径片段\r\n const currentPathSegment = { [idKey]: idValue };\r\n const fullPath = [...parentPath, currentPathSegment];\r\n\r\n // 存储路径信息用于后续路由生成\r\n nodePathMap.set(idValue, fullPath);\r\n\r\n // 创建轻量级的节点对象存入 nodeMap\r\n // 注意:这里先创建基础结构,路由信息在第二步补充\r\n const nodeSimple = {\r\n ...node,\r\n [extendKey]: {}, // 初始化扩展对象\r\n [childrenKey]: null // 去除原始 children,避免引用污染\r\n };\r\n nodeMap.set(idValue, nodeSimple);\r\n\r\n // 处理子节点\r\n const childrenNodes = node[childrenKey];\r\n if (Array.isArray(childrenNodes)) {\r\n // 逆序压栈,保持原序\r\n for (let i = childrenNodes.length - 1; i >= 0; i--) {\r\n stack.push({ node: childrenNodes[i], parentPath: fullPath });\r\n }\r\n }\r\n }\r\n\r\n /* ---------- 2. 构建树形结构 & 生成路由 ---------- */\r\n const buildStack = [];\r\n if (Array.isArray(menuItems)) {\r\n for (let i = menuItems.length - 1; i >= 0; i--) {\r\n buildStack.push({ node: menuItems[i], parentUi: undefined });\r\n }\r\n }\r\n\r\n while (buildStack.length) {\r\n const { node, parentUi } = buildStack.pop();\r\n const idValue = node[idKey];\r\n\r\n // 从 Map 中取出之前创建好的节点对象\r\n const nodeSimple = nodeMap.get(idValue);\r\n\r\n // 2.1 构建 UI 菜单项\r\n const uiMenuItem = {\r\n [menuIdKey]: idValue,\r\n [menuLabelKey]: node[labelKey],\r\n [menuExtendKey]: {} // UI 菜单专用扩展\r\n };\r\n\r\n // 处理子节点\r\n const childrenNodes = node[childrenKey];\r\n const hasChildren = Array.isArray(childrenNodes) && childrenNodes.length > 0;\r\n\r\n if (hasChildren) {\r\n // 有子节点 -> 继续压栈,传递当前 uiMenuItem 作为父级\r\n for (let i = childrenNodes.length - 1; i >= 0; i--) {\r\n buildStack.push({ node: childrenNodes[i], parentUi: uiMenuItem });\r\n }\r\n } else {\r\n // 2.2 无子节点 -> 生成路由配置\r\n\r\n // 提取 code 和 id 数组\r\n const codePaths = [],\r\n keyPaths = [];\r\n nodePathMap.get(idValue).forEach((item) => {\r\n const idValue = item[idKey];\r\n const nodeSimple = nodeMap.get(idValue);\r\n keyPaths.push(idValue);\r\n codePaths.push(nodeSimple[codeKey]);\r\n });\r\n\r\n const routeName = codePaths.join(\".\");\r\n const routePath = \"/\" + codePaths.join(\"/\");\r\n\r\n const routeItem = {\r\n [routeNameKey]: routeName,\r\n [routePathKey]: routePath,\r\n [routeMetaKey]: { [idKey]: idValue }\r\n };\r\n\r\n // 执行路由钩子\r\n if (typeof handleRouteItem === \"function\") {\r\n handleRouteItem(routeItem, nodeSimple);\r\n }\r\n routeItems.push(routeItem);\r\n\r\n // 补充扩展信息\r\n uiMenuItem[menuExtendKey].routeName = routeName;\r\n uiMenuItem[menuExtendKey].keyPaths = keyPaths;\r\n\r\n nodeSimple[extendKey].routeName = routeName;\r\n nodeSimple[extendKey].keyPaths = keyPaths;\r\n }\r\n\r\n // 2.3 挂载到父级 UI 或根数组\r\n if (parentUi) {\r\n // 逻辑赋值,确保 children 数组存在\r\n (parentUi[menuChildrenKey] || (parentUi[menuChildrenKey] = [])).push(uiMenuItem);\r\n } else {\r\n if (typeof handleMenuItem === \"function\") {\r\n handleMenuItem(uiMenuItem, nodeSimple);\r\n }\r\n uiMenuItems.push(uiMenuItem);\r\n }\r\n\r\n // 执行节点钩子\r\n if (typeof handleNodeItem === \"function\") {\r\n handleNodeItem(nodeSimple);\r\n }\r\n }\r\n\r\n return { uiMenuItems, routeItems, nodeMap };\r\n}\r\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,iBAAiB,CAAC,SAAS,EAAE,OAAO,GAAG,EAAE,EAAE;AAC3D;AACA,IAAI,MAAM;AACV;AACA,QAAQ,KAAK,GAAG,IAAI;AACpB,QAAQ,OAAO,GAAG,MAAM;AACxB,QAAQ,QAAQ,GAAG,MAAM;AACzB,QAAQ,WAAW,GAAG,UAAU;AAChC,QAAQ,SAAS,GAAG,QAAQ;AAC5B;AACA;AACA,QAAQ,SAAS,GAAG,KAAK;AACzB,QAAQ,YAAY,GAAG,OAAO;AAC9B,QAAQ,eAAe,GAAG,UAAU;AACpC,QAAQ,aAAa,GAAG,QAAQ;AAChC;AACA,QAAQ,YAAY,GAAG,MAAM;AAC7B,QAAQ,YA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IAAI,CAAC;AACL;AACA,IAAI,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC;AAChD;;;;"}
package/dist/tree.cjs CHANGED
@@ -67,24 +67,23 @@ function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = "id", parent
67
67
  }
68
68
 
69
69
  /**
70
- * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
70
+ * 在嵌套树中按 `id` 递归查找节点
71
71
  *
72
72
  * @template T extends Record<PropertyKey, any>
73
73
  * @param {string | number} id - 要查找的 id
74
74
  * @param {T[]} arr - 嵌套树森林
75
- * @param {string} [resultKey='name'] - 需要返回的字段
76
75
  * @param {string} [idKey='id'] - 主键字段
77
76
  * @param {string} [childrenKey='children'] - 子节点字段
78
- * @returns {any} 找到的值;未找到返回 `undefined`
77
+ * @returns {any} 找到的节点;未找到返回 `undefined`
79
78
  */
80
- const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
79
+ const findTreeNodeById = function findTreeNodeByIdFn(id, arr, idKey = "id", childrenKey = "children") {
81
80
  if (Array.isArray(arr) && arr.length > 0) {
82
81
  for (let i = 0; i < arr.length; i++) {
83
82
  const item = arr[i];
84
83
  if (item[idKey]?.toString() === id?.toString()) {
85
- return item[resultKey];
84
+ return item;
86
85
  } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {
87
- const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);
86
+ const result = findTreeNodeByIdFn(id, item[childrenKey], idKey, childrenKey);
88
87
  if (result) {
89
88
  return result;
90
89
  }
@@ -93,6 +92,21 @@ const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey
93
92
  }
94
93
  };
95
94
 
95
+ /**
96
+ * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
97
+ *
98
+ * @template T extends Record<PropertyKey, any>
99
+ * @param {string | number} id - 要查找的 id
100
+ * @param {T[]} arr - 嵌套树森林
101
+ * @param {string} [resultKey='name'] - 需要返回的字段
102
+ * @param {string} [idKey='id'] - 主键字段
103
+ * @param {string} [childrenKey='children'] - 子节点字段
104
+ * @returns {any} 找到的值;未找到返回 `undefined`
105
+ */
106
+ function findObjAttrValueById(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
107
+ return findTreeNodeById(id, arr, idKey, childrenKey)?.[resultKey];
108
+ }
109
+
96
110
  /**
97
111
  * 从服务端返回的已选 id 数组里,提取出
98
112
  * 1. 叶子节点
@@ -169,6 +183,7 @@ function extractFullyCheckedKeys(treeData, selectedKeys, idKey = "id", childrenK
169
183
 
170
184
  exports.extractFullyCheckedKeys = extractFullyCheckedKeys;
171
185
  exports.findObjAttrValueById = findObjAttrValueById;
186
+ exports.findTreeNodeById = findTreeNodeById;
172
187
  exports.flatCompleteTree2NestedTree = flatCompleteTree2NestedTree;
173
188
  exports.nestedTree2IdMap = nestedTree2IdMap;
174
189
  //# sourceMappingURL=tree.cjs.map
package/dist/tree.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"tree.cjs","sources":["../src/source/tree.js"],"sourcesContent":["/**\r\n * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} data - 嵌套树森林\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表\r\n */\r\nexport function nestedTree2IdMap(data, idKey = \"id\", childrenKey = \"children\") {\r\n const retObj = {};\r\n function fn(nodes) {\r\n if (Array.isArray(nodes) && nodes.length > 0) {\r\n nodes.forEach((node) => {\r\n retObj[node[idKey]] = { ...node };\r\n retObj[node[idKey]][childrenKey] = null;\r\n\r\n fn(node[childrenKey]);\r\n });\r\n }\r\n }\r\n fn(data);\r\n return retObj;\r\n}\r\n\r\n/**\r\n * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)\r\n * @param {number | string} [parentId=0] - 根节点标识值\r\n * @param {Object} [opts] - 字段映射配置\r\n * @param {string} [opts.idKey='id'] - 节点主键\r\n * @param {string} [opts.parentKey='parentId'] - 父节点外键\r\n * @param {string} [opts.childrenKey='children'] - 存放子节点的字段\r\n * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林\r\n */\r\nexport function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = \"id\", parentKey = \"parentId\", childrenKey = \"children\" } = {}) {\r\n const map = new Map(); // id -> node\r\n const items = []; // 多根森林\r\n\r\n // 1. 初始化:保证每个节点都有 children,并存入 map\r\n for (const item of nodes) {\r\n const node = { ...item, [childrenKey]: [] };\r\n map.set(item[idKey], node);\r\n }\r\n\r\n // 2. 建立父子关系\r\n for (const item of nodes) {\r\n const node = map.get(item[idKey]);\r\n const parentIdVal = item[parentKey];\r\n\r\n if (parentIdVal === parentId) {\r\n // 根层\r\n items.push(node);\r\n } else {\r\n // 非根层:找到父节点,把自己挂上去\r\n const parent = map.get(parentIdVal);\r\n if (parent) parent[childrenKey].push(node);\r\n // 如果 parent 不存在,说明数据不完整,可自定义处理\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\n/**\r\n * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {string | number} id - 要查找的 id\r\n * @param {T[]} arr - 嵌套树森林\r\n * @param {string} [resultKey='name'] - 需要返回的字段\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {any} 找到的值;未找到返回 `undefined`\r\n */\r\nexport const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = \"name\", idKey = \"id\", childrenKey = \"children\") {\r\n if (Array.isArray(arr) && arr.length > 0) {\r\n for (let i = 0; i < arr.length; i++) {\r\n const item = arr[i];\r\n if (item[idKey]?.toString() === id?.toString()) {\r\n return item[resultKey];\r\n } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {\r\n const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);\r\n if (result) {\r\n return result;\r\n }\r\n }\r\n }\r\n }\r\n};\r\n\r\n/**\r\n * 从服务端返回的已选 id 数组里,提取出\r\n * 1. 叶子节点\r\n * 2. 所有子节点都被选中的父节点\r\n * 其余父节点一律丢弃(由前端 Tree 自动算半选)\r\n *\r\n * @param {Array} treeData 完整树\r\n * @param {Array} selectedKeys 后端给的选中 id 数组\r\n * @param {String} idKey 节点唯一字段\r\n * @param {String} childrenKey 子节点字段\r\n * @returns {{checked: string[], halfChecked: string[]}}\r\n */\r\nexport function extractFullyCheckedKeys(treeData, selectedKeys, idKey = \"id\", childrenKey = \"children\") {\r\n const selectedSet = new Set(selectedKeys);\r\n const checked = new Set();\r\n const halfChecked = new Set();\r\n\r\n /* 返回值含义\r\n 0 - 未选中\r\n 1 - 半选\r\n 2 - 全选\r\n */\r\n function dfs(node) {\r\n const nodeId = node[idKey];\r\n const children = node[childrenKey] || [];\r\n\r\n // 叶子\r\n if (!children.length) {\r\n if (selectedSet.has(nodeId)) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n return 0;\r\n }\r\n\r\n // 非叶子\r\n let allChecked = true;\r\n let someChecked = false;\r\n\r\n children.forEach((child) => {\r\n const childState = dfs(child);\r\n if (childState !== 2) allChecked = false;\r\n if (childState >= 1) someChecked = true;\r\n });\r\n\r\n // 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选\r\n if (selectedSet.has(nodeId)) {\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n\r\n // 当前节点不在 selectedKeys 里,看子节点\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n if (someChecked) {\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n return 0;\r\n }\r\n\r\n treeData.forEach(dfs);\r\n return {\r\n checked: [...checked],\r\n halfChecked: [...halfChecked]\r\n };\r\n}\r\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,WAAW,GAAG,UAAU,EAAE;AAC/E,IAAI,MAAM,MAAM,GAAG,EAAE,CAAC;AACtB,IAAI,SAAS,EAAE,CAAC,KAAK,EAAE;AACvB,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AACtD,YAAY,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK;AACpC,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,CAAC;AAClD,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC;AACxD;AACA,gBAAgB,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;AACtC,YAAY,CAAC,CAAC,CAAC;AACf,QAAQ,CAAC;AACT,IAAI,CAAC;AACL,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC;AACb,IAAI,OAAO,MAAM,CAAC;AAClB,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,2BAA2B,CAAC,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,EAAE,KAAK,GAAG,IAAI,EAAE,SAAS,GAAG,UAAU,EAAE,WAAW,GAAG,UAAU,EAAE,GAAG,EAAE,EAAE;AAC1I,IAAI,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAC1B,I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AAC;AACrB,QAAQ,CAAC;AACT,QAAQ,OAAO,CAAC,CAAC;AACjB,IAAI,CAAC;AACL;AACA,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC1B,IAAI,OAAO;AACX,QAAQ,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;AAC7B,QAAQ,WAAW,EAAE,CAAC,GAAG,WAAW,CAAC;AACrC,KAAK,CAAC;AACN;;;;;;;"}
1
+ {"version":3,"file":"tree.cjs","sources":["../src/source/tree.js"],"sourcesContent":["/**\r\n * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} data - 嵌套树森林\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表\r\n */\r\nexport function nestedTree2IdMap(data, idKey = \"id\", childrenKey = \"children\") {\r\n const retObj = {};\r\n function fn(nodes) {\r\n if (Array.isArray(nodes) && nodes.length > 0) {\r\n nodes.forEach((node) => {\r\n retObj[node[idKey]] = { ...node };\r\n retObj[node[idKey]][childrenKey] = null;\r\n\r\n fn(node[childrenKey]);\r\n });\r\n }\r\n }\r\n fn(data);\r\n return retObj;\r\n}\r\n\r\n/**\r\n * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)\r\n * @param {number | string} [parentId=0] - 根节点标识值\r\n * @param {Object} [opts] - 字段映射配置\r\n * @param {string} [opts.idKey='id'] - 节点主键\r\n * @param {string} [opts.parentKey='parentId'] - 父节点外键\r\n * @param {string} [opts.childrenKey='children'] - 存放子节点的字段\r\n * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林\r\n */\r\nexport function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = \"id\", parentKey = \"parentId\", childrenKey = \"children\" } = {}) {\r\n const map = new Map(); // id -> node\r\n const items = []; // 多根森林\r\n\r\n // 1. 初始化:保证每个节点都有 children,并存入 map\r\n for (const item of nodes) {\r\n const node = { ...item, [childrenKey]: [] };\r\n map.set(item[idKey], node);\r\n }\r\n\r\n // 2. 建立父子关系\r\n for (const item of nodes) {\r\n const node = map.get(item[idKey]);\r\n const parentIdVal = item[parentKey];\r\n\r\n if (parentIdVal === parentId) {\r\n // 根层\r\n items.push(node);\r\n } else {\r\n // 非根层:找到父节点,把自己挂上去\r\n const parent = map.get(parentIdVal);\r\n if (parent) parent[childrenKey].push(node);\r\n // 如果 parent 不存在,说明数据不完整,可自定义处理\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\n/**\r\n * 在嵌套树中按 `id` 递归查找节点\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {string | number} id - 要查找的 id\r\n * @param {T[]} arr - 嵌套树森林\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {any} 找到的节点;未找到返回 `undefined`\r\n */\r\nexport const findTreeNodeById = function findTreeNodeByIdFn(id, arr, idKey = \"id\", childrenKey = \"children\") {\r\n if (Array.isArray(arr) && arr.length > 0) {\r\n for (let i = 0; i < arr.length; i++) {\r\n const item = arr[i];\r\n if (item[idKey]?.toString() === id?.toString()) {\r\n return item;\r\n } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {\r\n const result = findTreeNodeByIdFn(id, item[childrenKey], idKey, childrenKey);\r\n if (result) {\r\n return result;\r\n }\r\n }\r\n }\r\n }\r\n};\r\n\r\n/**\r\n * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {string | number} id - 要查找的 id\r\n * @param {T[]} arr - 嵌套树森林\r\n * @param {string} [resultKey='name'] - 需要返回的字段\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {any} 找到的值;未找到返回 `undefined`\r\n */\r\nexport function findObjAttrValueById(id, arr, resultKey = \"name\", idKey = \"id\", childrenKey = \"children\") {\r\n return findTreeNodeById(id, arr, idKey, childrenKey)?.[resultKey];\r\n}\r\n\r\n/**\r\n * 从服务端返回的已选 id 数组里,提取出\r\n * 1. 叶子节点\r\n * 2. 所有子节点都被选中的父节点\r\n * 其余父节点一律丢弃(由前端 Tree 自动算半选)\r\n *\r\n * @param {Array} treeData 完整树\r\n * @param {Array} selectedKeys 后端给的选中 id 数组\r\n * @param {String} idKey 节点唯一字段\r\n * @param {String} childrenKey 子节点字段\r\n * @returns {{checked: string[], halfChecked: string[]}}\r\n */\r\nexport function extractFullyCheckedKeys(treeData, selectedKeys, idKey = \"id\", childrenKey = \"children\") {\r\n const selectedSet = new Set(selectedKeys);\r\n const checked = new Set();\r\n const halfChecked = new Set();\r\n\r\n /* 返回值含义\r\n 0 - 未选中\r\n 1 - 半选\r\n 2 - 全选\r\n */\r\n function dfs(node) {\r\n const nodeId = node[idKey];\r\n const children = node[childrenKey] || [];\r\n\r\n // 叶子\r\n if (!children.length) {\r\n if (selectedSet.has(nodeId)) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n return 0;\r\n }\r\n\r\n // 非叶子\r\n let allChecked = true;\r\n let someChecked = false;\r\n\r\n children.forEach((child) => {\r\n const childState = dfs(child);\r\n if (childState !== 2) allChecked = false;\r\n if (childState >= 1) someChecked = true;\r\n });\r\n\r\n // 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选\r\n if (selectedSet.has(nodeId)) {\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n\r\n // 当前节点不在 selectedKeys 里,看子节点\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n if (someChecked) {\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n return 0;\r\n }\r\n\r\n treeData.forEach(dfs);\r\n return {\r\n checked: [...checked],\r\n halfChecked: [...halfChecked]\r\n };\r\n}\r\n"],"names":[],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,WAAW,GAAG,UAAU,EAAE;AAC/E,IAAI,MAAM,MAAM,GAAG,EAAE,CAAC;AACtB,IAAI,SAAS,EAAE,CAAC,KAAK,EAAE;AACvB,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AACtD,YAAY,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK;AACpC,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,CAAC;AAClD,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC;AACxD;AACA,gBAAgB,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;AACtC,YAAY,CAAC,CAAC,CAAC;AACf,QAAQ,CAAC;AACT,IAAI,CAAC;AACL,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC;AACb,IAAI,OAAO,MAAM,CAAC;AAClB,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,2BAA2B,CAAC,KAAK,EAAE,QAAQ,GAAG,CAAC,EAAE,EAAE,KAAK,GAAG,IAAI,EAAE,SAAS,GAAG,UAAU,EAAE,WAAW,GAAG,UAAU,EAAE,GAAG,EAAE,EAAE;AAC1I,IAAI,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAC1B,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;AACrB;AACA;AACA,IAAI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G,CAAC,MAAM,CAAC,CAAC;AAChC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT,QAAQ,IAAI,WAAW,EAAE;AACzB,YAAY,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACpC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT,QAAQ,OAAO,CAAC,CAAC;AACjB,IAAI,CAAC;AACL;AACA,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC1B,IAAI,OAAO;AACX,QAAQ,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;AAC7B,QAAQ,WAAW,EAAE,CAAC,GAAG,WAAW,CAAC;AACrC,KAAK,CAAC;AACN;;;;;;;;"}
package/dist/tree.js CHANGED
@@ -65,24 +65,23 @@ function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = "id", parent
65
65
  }
66
66
 
67
67
  /**
68
- * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
68
+ * 在嵌套树中按 `id` 递归查找节点
69
69
  *
70
70
  * @template T extends Record<PropertyKey, any>
71
71
  * @param {string | number} id - 要查找的 id
72
72
  * @param {T[]} arr - 嵌套树森林
73
- * @param {string} [resultKey='name'] - 需要返回的字段
74
73
  * @param {string} [idKey='id'] - 主键字段
75
74
  * @param {string} [childrenKey='children'] - 子节点字段
76
- * @returns {any} 找到的值;未找到返回 `undefined`
75
+ * @returns {any} 找到的节点;未找到返回 `undefined`
77
76
  */
78
- const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
77
+ const findTreeNodeById = function findTreeNodeByIdFn(id, arr, idKey = "id", childrenKey = "children") {
79
78
  if (Array.isArray(arr) && arr.length > 0) {
80
79
  for (let i = 0; i < arr.length; i++) {
81
80
  const item = arr[i];
82
81
  if (item[idKey]?.toString() === id?.toString()) {
83
- return item[resultKey];
82
+ return item;
84
83
  } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {
85
- const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);
84
+ const result = findTreeNodeByIdFn(id, item[childrenKey], idKey, childrenKey);
86
85
  if (result) {
87
86
  return result;
88
87
  }
@@ -91,6 +90,21 @@ const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey
91
90
  }
92
91
  };
93
92
 
93
+ /**
94
+ * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
95
+ *
96
+ * @template T extends Record<PropertyKey, any>
97
+ * @param {string | number} id - 要查找的 id
98
+ * @param {T[]} arr - 嵌套树森林
99
+ * @param {string} [resultKey='name'] - 需要返回的字段
100
+ * @param {string} [idKey='id'] - 主键字段
101
+ * @param {string} [childrenKey='children'] - 子节点字段
102
+ * @returns {any} 找到的值;未找到返回 `undefined`
103
+ */
104
+ function findObjAttrValueById(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
105
+ return findTreeNodeById(id, arr, idKey, childrenKey)?.[resultKey];
106
+ }
107
+
94
108
  /**
95
109
  * 从服务端返回的已选 id 数组里,提取出
96
110
  * 1. 叶子节点
@@ -165,5 +179,5 @@ function extractFullyCheckedKeys(treeData, selectedKeys, idKey = "id", childrenK
165
179
  };
166
180
  }
167
181
 
168
- export { extractFullyCheckedKeys, findObjAttrValueById, flatCompleteTree2NestedTree, nestedTree2IdMap };
182
+ export { extractFullyCheckedKeys, findObjAttrValueById, findTreeNodeById, flatCompleteTree2NestedTree, nestedTree2IdMap };
169
183
  //# sourceMappingURL=tree.js.map
package/dist/tree.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"tree.js","sources":["../src/source/tree.js"],"sourcesContent":["/**\r\n * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} data - 嵌套树森林\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表\r\n */\r\nexport function nestedTree2IdMap(data, idKey = \"id\", childrenKey = \"children\") {\r\n const retObj = {};\r\n function fn(nodes) {\r\n if (Array.isArray(nodes) && nodes.length > 0) {\r\n nodes.forEach((node) => {\r\n retObj[node[idKey]] = { ...node };\r\n retObj[node[idKey]][childrenKey] = null;\r\n\r\n fn(node[childrenKey]);\r\n });\r\n }\r\n }\r\n fn(data);\r\n return retObj;\r\n}\r\n\r\n/**\r\n * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)\r\n * @param {number | string} [parentId=0] - 根节点标识值\r\n * @param {Object} [opts] - 字段映射配置\r\n * @param {string} [opts.idKey='id'] - 节点主键\r\n * @param {string} [opts.parentKey='parentId'] - 父节点外键\r\n * @param {string} [opts.childrenKey='children'] - 存放子节点的字段\r\n * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林\r\n */\r\nexport function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = \"id\", parentKey = \"parentId\", childrenKey = \"children\" } = {}) {\r\n const map = new Map(); // id -> node\r\n const items = []; // 多根森林\r\n\r\n // 1. 初始化:保证每个节点都有 children,并存入 map\r\n for (const item of nodes) {\r\n const node = { ...item, [childrenKey]: [] };\r\n map.set(item[idKey], node);\r\n }\r\n\r\n // 2. 建立父子关系\r\n for (const item of nodes) {\r\n const node = map.get(item[idKey]);\r\n const parentIdVal = item[parentKey];\r\n\r\n if (parentIdVal === parentId) {\r\n // 根层\r\n items.push(node);\r\n } else {\r\n // 非根层:找到父节点,把自己挂上去\r\n const parent = map.get(parentIdVal);\r\n if (parent) parent[childrenKey].push(node);\r\n // 如果 parent 不存在,说明数据不完整,可自定义处理\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\n/**\r\n * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {string | number} id - 要查找的 id\r\n * @param {T[]} arr - 嵌套树森林\r\n * @param {string} [resultKey='name'] - 需要返回的字段\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {any} 找到的值;未找到返回 `undefined`\r\n */\r\nexport const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey = \"name\", idKey = \"id\", childrenKey = \"children\") {\r\n if (Array.isArray(arr) && arr.length > 0) {\r\n for (let i = 0; i < arr.length; i++) {\r\n const item = arr[i];\r\n if (item[idKey]?.toString() === id?.toString()) {\r\n return item[resultKey];\r\n } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {\r\n const result = findObjAttrValueByIdFn(id, item[childrenKey], resultKey, idKey, childrenKey);\r\n if (result) {\r\n return result;\r\n }\r\n }\r\n }\r\n }\r\n};\r\n\r\n/**\r\n * 从服务端返回的已选 id 数组里,提取出\r\n * 1. 叶子节点\r\n * 2. 所有子节点都被选中的父节点\r\n * 其余父节点一律丢弃(由前端 Tree 自动算半选)\r\n *\r\n * @param {Array} treeData 完整树\r\n * @param {Array} selectedKeys 后端给的选中 id 数组\r\n * @param {String} idKey 节点唯一字段\r\n * @param {String} childrenKey 子节点字段\r\n * @returns {{checked: string[], halfChecked: string[]}}\r\n */\r\nexport function extractFullyCheckedKeys(treeData, selectedKeys, idKey = \"id\", childrenKey = \"children\") {\r\n const selectedSet = new Set(selectedKeys);\r\n const checked = new Set();\r\n const halfChecked = new Set();\r\n\r\n /* 返回值含义\r\n 0 - 未选中\r\n 1 - 半选\r\n 2 - 全选\r\n */\r\n function dfs(node) {\r\n const nodeId = node[idKey];\r\n const children = node[childrenKey] || [];\r\n\r\n // 叶子\r\n if (!children.length) {\r\n if (selectedSet.has(nodeId)) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n return 0;\r\n }\r\n\r\n // 非叶子\r\n let allChecked = true;\r\n let someChecked = false;\r\n\r\n children.forEach((child) => {\r\n const childState = dfs(child);\r\n if (childState !== 2) allChecked = false;\r\n if (childState >= 1) someChecked = true;\r\n });\r\n\r\n // 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选\r\n if (selectedSet.has(nodeId)) {\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n\r\n // 当前节点不在 selectedKeys 里,看子节点\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n if (someChecked) {\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n return 0;\r\n }\r\n\r\n treeData.forEach(dfs);\r\n return {\r\n checked: [...checked],\r\n halfChecked: [...halfChecked]\r\n };\r\n}\r\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,WAAW,GAAG,UAAU,EAAE;AAC/E,IAAI,MAAM,MAAM,GAAG,EAAE,CAAC;AACtB,IAAI,SAAS,EAAE,CAAC,KAAK,EAAE;AACvB,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AACtD,YAAY,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK;AACpC,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,CAAC;AAClD,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC;AACxD;AACA,gBAAgB,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;AACtC,YAAY,CAAC,CAAC,CAAC;AACf,QAAQ,CAAC;AACT,IAAI,CAAC;AACL,IAAI,EAAE,CAAC,IAAI,CAAC,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GAAG,CAAC,MAAM,CAAC,CAAC;AACpC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT;AACA;AACA,QAAQ,IAAI,UAAU,EAAE;AACxB,YAAY,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AAChC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT,QAAQ,IAAI,WAAW,EAAE;AACzB,YAAY,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACpC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT,QAAQ,OAAO,CAAC,CAAC;AACjB,IAAI,CAAC;AACL;AACA,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC1B,IAAI,OAAO;AACX,QAAQ,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;AAC7B,QAAQ,WAAW,EAAE,CAAC,GAAG,WAAW,CAAC;AACrC,KAAK,CAAC;AACN;;;;"}
1
+ {"version":3,"file":"tree.js","sources":["../src/source/tree.js"],"sourcesContent":["/**\r\n * 把嵌套树拍平成 `{ [id]: node }` 映射,同时把原 `children` 置为 `null`。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} data - 嵌套树森林\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {Record<string, T & { [k in typeof childrenKey]: null }>} id→节点的映射表\r\n */\r\nexport function nestedTree2IdMap(data, idKey = \"id\", childrenKey = \"children\") {\r\n const retObj = {};\r\n function fn(nodes) {\r\n if (Array.isArray(nodes) && nodes.length > 0) {\r\n nodes.forEach((node) => {\r\n retObj[node[idKey]] = { ...node };\r\n retObj[node[idKey]][childrenKey] = null;\r\n\r\n fn(node[childrenKey]);\r\n });\r\n }\r\n }\r\n fn(data);\r\n return retObj;\r\n}\r\n\r\n/**\r\n * 把**已包含完整父子关系**的扁平节点列表还原成嵌套树(森林)。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {T[]} nodes - 扁平节点列表(必须包含 id / parentId)\r\n * @param {number | string} [parentId=0] - 根节点标识值\r\n * @param {Object} [opts] - 字段映射配置\r\n * @param {string} [opts.idKey='id'] - 节点主键\r\n * @param {string} [opts.parentKey='parentId'] - 父节点外键\r\n * @param {string} [opts.childrenKey='children'] - 存放子节点的字段\r\n * @returns {(T & { [k in typeof childrenKey]: T[] })[]} 嵌套树森林\r\n */\r\nexport function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = \"id\", parentKey = \"parentId\", childrenKey = \"children\" } = {}) {\r\n const map = new Map(); // id -> node\r\n const items = []; // 多根森林\r\n\r\n // 1. 初始化:保证每个节点都有 children,并存入 map\r\n for (const item of nodes) {\r\n const node = { ...item, [childrenKey]: [] };\r\n map.set(item[idKey], node);\r\n }\r\n\r\n // 2. 建立父子关系\r\n for (const item of nodes) {\r\n const node = map.get(item[idKey]);\r\n const parentIdVal = item[parentKey];\r\n\r\n if (parentIdVal === parentId) {\r\n // 根层\r\n items.push(node);\r\n } else {\r\n // 非根层:找到父节点,把自己挂上去\r\n const parent = map.get(parentIdVal);\r\n if (parent) parent[childrenKey].push(node);\r\n // 如果 parent 不存在,说明数据不完整,可自定义处理\r\n }\r\n }\r\n\r\n return items;\r\n}\r\n\r\n/**\r\n * 在嵌套树中按 `id` 递归查找节点\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {string | number} id - 要查找的 id\r\n * @param {T[]} arr - 嵌套树森林\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {any} 找到的节点;未找到返回 `undefined`\r\n */\r\nexport const findTreeNodeById = function findTreeNodeByIdFn(id, arr, idKey = \"id\", childrenKey = \"children\") {\r\n if (Array.isArray(arr) && arr.length > 0) {\r\n for (let i = 0; i < arr.length; i++) {\r\n const item = arr[i];\r\n if (item[idKey]?.toString() === id?.toString()) {\r\n return item;\r\n } else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {\r\n const result = findTreeNodeByIdFn(id, item[childrenKey], idKey, childrenKey);\r\n if (result) {\r\n return result;\r\n }\r\n }\r\n }\r\n }\r\n};\r\n\r\n/**\r\n * 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。\r\n *\r\n * @template T extends Record<PropertyKey, any>\r\n * @param {string | number} id - 要查找的 id\r\n * @param {T[]} arr - 嵌套树森林\r\n * @param {string} [resultKey='name'] - 需要返回的字段\r\n * @param {string} [idKey='id'] - 主键字段\r\n * @param {string} [childrenKey='children'] - 子节点字段\r\n * @returns {any} 找到的值;未找到返回 `undefined`\r\n */\r\nexport function findObjAttrValueById(id, arr, resultKey = \"name\", idKey = \"id\", childrenKey = \"children\") {\r\n return findTreeNodeById(id, arr, idKey, childrenKey)?.[resultKey];\r\n}\r\n\r\n/**\r\n * 从服务端返回的已选 id 数组里,提取出\r\n * 1. 叶子节点\r\n * 2. 所有子节点都被选中的父节点\r\n * 其余父节点一律丢弃(由前端 Tree 自动算半选)\r\n *\r\n * @param {Array} treeData 完整树\r\n * @param {Array} selectedKeys 后端给的选中 id 数组\r\n * @param {String} idKey 节点唯一字段\r\n * @param {String} childrenKey 子节点字段\r\n * @returns {{checked: string[], halfChecked: string[]}}\r\n */\r\nexport function extractFullyCheckedKeys(treeData, selectedKeys, idKey = \"id\", childrenKey = \"children\") {\r\n const selectedSet = new Set(selectedKeys);\r\n const checked = new Set();\r\n const halfChecked = new Set();\r\n\r\n /* 返回值含义\r\n 0 - 未选中\r\n 1 - 半选\r\n 2 - 全选\r\n */\r\n function dfs(node) {\r\n const nodeId = node[idKey];\r\n const children = node[childrenKey] || [];\r\n\r\n // 叶子\r\n if (!children.length) {\r\n if (selectedSet.has(nodeId)) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n return 0;\r\n }\r\n\r\n // 非叶子\r\n let allChecked = true;\r\n let someChecked = false;\r\n\r\n children.forEach((child) => {\r\n const childState = dfs(child);\r\n if (childState !== 2) allChecked = false;\r\n if (childState >= 1) someChecked = true;\r\n });\r\n\r\n // 当前节点本身在 selectedKeys 里,但子节点未全选 → 只能算半选\r\n if (selectedSet.has(nodeId)) {\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n\r\n // 当前节点不在 selectedKeys 里,看子节点\r\n if (allChecked) {\r\n checked.add(nodeId);\r\n return 2;\r\n }\r\n if (someChecked) {\r\n halfChecked.add(nodeId);\r\n return 1;\r\n }\r\n return 0;\r\n }\r\n\r\n treeData.forEach(dfs);\r\n return {\r\n checked: [...checked],\r\n halfChecked: [...halfChecked]\r\n };\r\n}\r\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,EAAE,WAAW,GAAG,UAAU,EAAE;AAC/E,IAAI,MAAM,MAAM,GAAG,EAAE,CAAC;AACtB,IAAI,SAAS,EAAE,CAAC,KAAK,EAAE;AACvB,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;AACtD,YAAY,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK;AACpC,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,CAAC;AAClD,gBAAgB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC;AACxD;AACA,gBAAgB,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;AACtC,YAAY,CAAC,CAAC,CAAC;AACf,QAAQ,CAAC;AACT,IAAI,CAAC;AACL,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC;AACb,IAAI,OAAO,MAAM,CAAC;AAClB,CAAC;AACD;AACA;AACA;A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AAM,CAAC,EAAE;AACrC,YAAY,IAAI,UAAU,EAAE;AAC5B,gBAAgB,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACpC,gBAAgB,OAAO,CAAC,CAAC;AACzB,YAAY,CAAC;AACb,YAAY,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACpC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT;AACA;AACA,QAAQ,IAAI,UAAU,EAAE;AACxB,YAAY,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AAChC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT,QAAQ,IAAI,WAAW,EAAE;AACzB,YAAY,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AACpC,YAAY,OAAO,CAAC,CAAC;AACrB,QAAQ,CAAC;AACT,QAAQ,OAAO,CAAC,CAAC;AACjB,IAAI,CAAC;AACL;AACA,IAAI,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC1B,IAAI,OAAO;AACX,QAAQ,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;AAC7B,QAAQ,WAAW,EAAE,CAAC,GAAG,WAAW,CAAC;AACrC,KAAK,CAAC;AACN;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a2bei4-utils",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "🧰 零依赖、ESM 的 JS 业务工具箱:数组乱序/深浅克隆、防抖节流、URL/query 解析、Date/时长格式化、随机汉字/字母、文件下载、事件总线、UUID/分布式短 ID、树结构互转等 40+ 常用函数 & 类,Tree-Shaking 友好。",
5
5
  "private": false,
6
6
  "type": "module",
@@ -55,6 +55,11 @@
55
55
  "import": "./dist/id.js",
56
56
  "require": "./dist/id.cjs"
57
57
  },
58
+ "./menu": {
59
+ "types": "./types/menu.d.ts",
60
+ "import": "./dist/menu.js",
61
+ "require": "./dist/menu.cjs"
62
+ },
58
63
  "./timer": {
59
64
  "types": "./types/timer.d.ts",
60
65
  "import": "./dist/timer.js",
@@ -135,12 +140,12 @@
135
140
  },
136
141
  "homepage": "https://github.com/xiaodu114/a2bei4-utils",
137
142
  "devDependencies": {
138
- "@rollup/plugin-commonjs": "^28.0.6",
139
- "@rollup/plugin-node-resolve": "^16.0.2",
140
- "@rollup/plugin-terser": "^0.4.4",
141
- "prettier": "^3.6.2",
142
- "rollup": "^4.52.4",
143
- "rollup-plugin-dts": "^6.2.3",
143
+ "@rollup/plugin-commonjs": "^29.0.2",
144
+ "@rollup/plugin-node-resolve": "^16.0.3",
145
+ "@rollup/plugin-terser": "^1.0.0",
146
+ "prettier": "^3.8.1",
147
+ "rollup": "^4.60.0",
148
+ "rollup-plugin-dts": "^6.4.1",
144
149
  "rollup-plugin-filesize": "^10.0.0",
145
150
  "typescript": "^5.9.3"
146
151
  },