knt-shared 1.5.2 → 1.5.3

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.
@@ -16,6 +16,30 @@ export interface ArrayToMapOptions {
16
16
  /** 作为 value 的字段名,不传则返回整个对象 */
17
17
  valueField?: string;
18
18
  }
19
+ /**
20
+ * 树形数据转换上下文
21
+ */
22
+ export interface TransformContext<T = any> {
23
+ /** 当前节点层级(从 0 开始) */
24
+ level: number;
25
+ /** 在同级节点中的索引位置 */
26
+ index: number;
27
+ /** 父节点引用(根节点为 null) */
28
+ parent: T | null;
29
+ /** 节点路径(由各层级索引组成的数组) */
30
+ path: number[];
31
+ /** 所有祖先节点数组(从根节点到父节点,不包含当前节点) */
32
+ ancestors: T[];
33
+ /** 是否为叶子节点(无子节点) */
34
+ isLeaf: boolean;
35
+ }
36
+ /**
37
+ * 树形数据转换选项
38
+ */
39
+ export interface TransformTreeOptions {
40
+ /** 子节点字段名,默认 'children' */
41
+ childrenField?: string;
42
+ }
19
43
  /**
20
44
  * 将树形结构扁平化为一维数组
21
45
  * @param tree 树形数据数组
@@ -76,4 +100,120 @@ export declare function flattenTree<T extends Record<string, any>>(tree: T[], op
76
100
  * ```
77
101
  */
78
102
  export declare function arrayToMap<T extends Record<string, any>>(array: T[], options?: ArrayToMapOptions): Map<any, any>;
103
+ /**
104
+ * 转换树形数据结构,支持字段映射、添加元数据、条件转换等操作
105
+ * @param tree 树形数据数组
106
+ * @param transform 转换函数,接收节点和上下文信息,返回转换后的节点
107
+ * @param options 配置选项
108
+ * @returns 转换后的新树形数据数组
109
+ * @example
110
+ * ```ts
111
+ * // 示例 1:字段映射(后端数据转 Arco Tree 格式)
112
+ * const backendData = [
113
+ * { id: 1, name: '节点1', items: [
114
+ * { id: 2, name: '节点1-1' },
115
+ * { id: 3, name: '节点1-2' }
116
+ * ]}
117
+ * ];
118
+ *
119
+ * const arcoTreeData = transformTree(
120
+ * backendData,
121
+ * (node) => ({
122
+ * key: node.id,
123
+ * title: node.name,
124
+ * }),
125
+ * { childrenField: 'items' }
126
+ * );
127
+ * // 结果: [{ key: 1, title: '节点1', children: [
128
+ * // { key: 2, title: '节点1-1' },
129
+ * // { key: 3, title: '节点1-2' }
130
+ * // ]}]
131
+ *
132
+ * // 示例 2:添加元数据字段
133
+ * const tree = [
134
+ * { id: 1, title: '根节点', children: [
135
+ * { id: 2, title: '子节点1' },
136
+ * { id: 3, title: '子节点2', children: [
137
+ * { id: 4, title: '孙节点' }
138
+ * ]}
139
+ * ]}
140
+ * ];
141
+ *
142
+ * const enrichedTree = transformTree(
143
+ * tree,
144
+ * (node, ctx) => ({
145
+ * ...node,
146
+ * level: ctx.level,
147
+ * parentId: ctx.parent?.id,
148
+ * isLeaf: ctx.isLeaf,
149
+ * pathStr: ctx.path.join('-'),
150
+ * })
151
+ * );
152
+ * // 结果: 每个节点都会添加 level、parentId、isLeaf、pathStr 字段
153
+ *
154
+ * // 示例 3:条件转换
155
+ * const menuTree = [
156
+ * { id: 1, name: '首页', status: 'active', children: [
157
+ * { id: 2, name: '用户管理', status: 'inactive' },
158
+ * { id: 3, name: '系统设置', status: 'active' }
159
+ * ]}
160
+ * ];
161
+ *
162
+ * const transformedMenu = transformTree(
163
+ * menuTree,
164
+ * (node, ctx) => ({
165
+ * ...node,
166
+ * title: ctx.level === 0 ? `【根】${node.name}` : node.name,
167
+ * disabled: node.status === 'inactive',
168
+ * icon: ctx.isLeaf ? 'icon-file' : 'icon-folder',
169
+ * })
170
+ * );
171
+ * // 结果: 根节点标题添加前缀,inactive 状态的节点被禁用,根据是否为叶子节点设置不同图标
172
+ *
173
+ * // 示例 4:使用 ancestors 构建完整路径
174
+ * const categoryTree = [
175
+ * { id: '767488425906949', name: 'HSS_专用勿动', children: [
176
+ * { id: '767489603769093', name: '医疗保健', children: [
177
+ * { id: '767489603769094', name: '医疗器械' }
178
+ * ]}
179
+ * ]}
180
+ * ];
181
+ *
182
+ * const treeWithFullPath = transformTree(
183
+ * categoryTree,
184
+ * (node, ctx) => ({
185
+ * ...node,
186
+ * fullPath: [...ctx.ancestors.map(a => a.id), node.id].join('/'),
187
+ * fullPathText: [...ctx.ancestors.map(a => a.name), node.name].join('/'),
188
+ * })
189
+ * );
190
+ * // 结果:
191
+ * // 根节点: { fullPath: '767488425906949', fullPathText: 'HSS_专用勿动' }
192
+ * // 子节点: { fullPath: '767488425906949/767489603769093', fullPathText: 'HSS_专用勿动/医疗保健' }
193
+ * // 孙节点: { fullPath: '767488425906949/767489603769093/767489603769094', fullPathText: 'HSS_专用勿动/医疗保健/医疗器械' }
194
+ *
195
+ * // 示例 5:复杂嵌套结构转换
196
+ * const orgData = [
197
+ * {
198
+ * deptId: 1,
199
+ * deptName: '技术部',
200
+ * subDepts: [
201
+ * { deptId: 2, deptName: '前端组' },
202
+ * { deptId: 3, deptName: '后端组' }
203
+ * ]
204
+ * }
205
+ * ];
206
+ *
207
+ * const treeSelectData = transformTree(
208
+ * orgData,
209
+ * (node, ctx) => ({
210
+ * value: node.deptId,
211
+ * label: node.deptName,
212
+ * depth: ctx.level,
213
+ * }),
214
+ * { childrenField: 'subDepts' }
215
+ * );
216
+ * ```
217
+ */
218
+ export declare function transformTree<T extends Record<string, any>, R = T>(tree: T[], transform: (node: T, context: TransformContext<T>) => R, options?: TransformTreeOptions): R[];
79
219
  //# sourceMappingURL=tree.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../src/utils/tree.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,2BAA2B;IAC3B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,0BAA0B;IAC1B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,0BAA0B;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8BAA8B;IAC9B,UAAU,CAAC,EAAE,MAAM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IAC3B,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACvD,IAAI,EAAE,CAAC,EAAE,EACT,OAAO,CAAC,EAAE,kBAAkB,GAC3B,CAAC,EAAE,CA2CL;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACtD,KAAK,EAAE,CAAC,EAAE,EACV,OAAO,CAAC,EAAE,iBAAiB,GAC1B,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CA0Bf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkHG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAChE,IAAI,EAAE,CAAC,EAAE,EACT,SAAS,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,EACvD,OAAO,CAAC,EAAE,oBAAoB,GAC7B,CAAC,EAAE,CAuEL"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knt-shared",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "KNT共享组件库和工具函数",
5
5
  "author": "hss",
6
6
  "main": "dist/index.cjs.js",