@zcrkey/js-utils 0.0.11 → 0.0.15

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.
package/dist/cjs/tree.js DELETED
@@ -1,179 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
-
29
- // src/tree.ts
30
- var tree_exports = {};
31
- __export(tree_exports, {
32
- default: () => CrTreeUtil
33
- });
34
- module.exports = __toCommonJS(tree_exports);
35
- var import_lodash = require("lodash");
36
- var import_util = __toESM(require("./util"));
37
- var CrTreeUtil = class {
38
- /**
39
- * 列表数据转树形数据
40
- * @param list
41
- * @param options
42
- * @param options.idField 默认值 id
43
- * @param options.parentIdField 默认值 parentId
44
- * @param options.childrenField 默认值 children
45
- * @param options.isCloneDeep 是否需要深拷贝, 默认值为 true
46
- * @param options.getData 处理数据
47
- * @returns
48
- */
49
- static listToTree(list, options) {
50
- const {
51
- idField = "id",
52
- parentIdField = "parentId",
53
- childrenField = "children",
54
- isCloneDeep = true,
55
- getData = (item) => item
56
- } = options || {};
57
- const listData = isCloneDeep ? (0, import_lodash.cloneDeep)(list) : list;
58
- const treeData = [];
59
- const nodeMap = /* @__PURE__ */ new Map();
60
- for (const item of listData) {
61
- const id = item[idField];
62
- const node = {
63
- ...getData(item),
64
- __origin_id: id,
65
- __origin_pid: item[parentIdField]
66
- };
67
- nodeMap.set(id, node);
68
- }
69
- for (const node of nodeMap.values()) {
70
- const parentId = node.__origin_pid;
71
- if (parentId === void 0 || parentId === null || parentId === 0 || !nodeMap.has(parentId)) {
72
- treeData.push(node);
73
- } else {
74
- const parentNode = nodeMap.get(parentId);
75
- if (!parentNode[childrenField]) {
76
- parentNode[childrenField] = [];
77
- }
78
- parentNode[childrenField].push(node);
79
- }
80
- }
81
- const clean = (nodes) => {
82
- for (const node of nodes) {
83
- delete node.__origin_id;
84
- delete node.__origin_pid;
85
- if (node[childrenField]) {
86
- clean(node[childrenField]);
87
- }
88
- }
89
- };
90
- clean(treeData);
91
- return treeData;
92
- }
93
- /**
94
- * 树形数据转列表数据
95
- * @param tree
96
- * @param options
97
- * @param options.idField 默认值 id
98
- * @param options.parentIdField 默认值 parentId
99
- * @param options.childrenField 默认值 children
100
- * @param options.rootParentValue 根节点的父值,默认值 null
101
- * @returns
102
- */
103
- static treeToList(tree, options) {
104
- const {
105
- idField = "id",
106
- parentIdField = "parentId",
107
- childrenField = "children",
108
- rootParentValue = null
109
- } = options || {};
110
- const result = [];
111
- const originQueue = (0, import_lodash.cloneDeep)([...tree]);
112
- const queue = originQueue.map((node) => ({
113
- node,
114
- parentId: rootParentValue
115
- }));
116
- while (queue.length) {
117
- const { node, parentId } = queue.shift();
118
- if (!node || !import_util.default.isObject(node)) {
119
- continue;
120
- }
121
- const children = node[childrenField];
122
- const nodeId = node[idField];
123
- const { [childrenField]: _, ...rest } = node;
124
- const flatNode = {
125
- ...rest,
126
- [parentIdField]: parentId
127
- };
128
- result.push(flatNode);
129
- if (children && import_util.default.isArray(children) && children.length > 0) {
130
- const childQueue = children.map((childNode) => ({
131
- node: childNode,
132
- parentId: nodeId
133
- }));
134
- queue.push(...childQueue);
135
- }
136
- }
137
- return result;
138
- }
139
- /**
140
- * 获取扁平化父数据(包含自身)
141
- * @param listData
142
- * @param value
143
- * @param settings
144
- * @param settings.valueField 默认值 value
145
- * @param settings.idField 默认值 id
146
- * @param settings.parentIdField 默认值 parentId
147
- * @param settings.getData 过滤数据
148
- */
149
- static getFlatParentDatas(listData, value, settings) {
150
- if (!Array.isArray(listData) || listData.length === 0 || value == null) {
151
- return [];
152
- }
153
- const options = Object.assign(
154
- {
155
- valueField: "value",
156
- idField: "id",
157
- parentIdField: "parentId",
158
- getData: (item) => {
159
- return item;
160
- }
161
- },
162
- settings
163
- );
164
- const nodeMap = (0, import_lodash.keyBy)(listData, options.valueField);
165
- const result = [];
166
- const visited = /* @__PURE__ */ new Set();
167
- let current = nodeMap[value];
168
- while (current && !visited.has((0, import_lodash.get)(current, options.idField))) {
169
- visited.add((0, import_lodash.get)(current, options.idField));
170
- result.unshift(options.getData(current));
171
- const parentId = (0, import_lodash.get)(
172
- current,
173
- options.parentIdField
174
- );
175
- current = (0, import_lodash.find)(listData, [options.valueField, parentId]);
176
- }
177
- return result;
178
- }
179
- };