tav-ui
Version:
188 lines (186 loc) • 5.34 kB
JavaScript
const DEFAULT_CONFIG = {
id: "id",
children: "children",
pid: "pid"
};
const getConfig = (config) => Object.assign({}, DEFAULT_CONFIG, config);
function listToTree(list, config = {}) {
const conf = getConfig(config);
const nodeMap = /* @__PURE__ */ new Map();
const result = [];
const { id, children, pid } = conf;
for (const node of list) {
node[children] = node[children] || [];
nodeMap.set(node[id], node);
}
for (const node of list) {
const parent = nodeMap.get(node[pid]);
(parent ? parent.children : result).push(node);
}
return result;
}
function treeToList(tree, config = {}, clearParentChildren = false) {
config = getConfig(config);
const { children } = config;
const result = [...tree];
for (let i = 0; i < result.length; i++) {
if (!result[i][children])
continue;
result.splice(i + 1, 0, ...result[i][children]);
if (clearParentChildren)
result[i][children] = [];
}
return result;
}
function findNode(tree, func, config = {}) {
config = getConfig(config);
const { children } = config;
const list = [...tree];
for (const node of list) {
if (func(node))
return node;
node[children] && list.push(...node[children]);
}
return null;
}
function findNodeAll(tree, func, config = {}) {
config = getConfig(config);
const { children } = config;
const list = [...tree];
const result = [];
for (const node of list) {
func(node) && result.push(node);
node[children] && list.push(...node[children]);
}
return result;
}
function findPath(tree, func, config = {}) {
config = getConfig(config);
const path = [];
const list = [...tree];
const visitedSet = /* @__PURE__ */ new Set();
const { children } = config;
while (list.length) {
const node = list[0];
if (visitedSet.has(node)) {
path.pop();
list.shift();
} else {
visitedSet.add(node);
node[children] && list.unshift(...node[children]);
path.push(node);
if (func(node))
return path;
}
}
return null;
}
function findPathAll(tree, func, config = {}) {
config = getConfig(config);
const path = [];
const list = [...tree];
const result = [];
const visitedSet = /* @__PURE__ */ new Set();
const { children } = config;
while (list.length) {
const node = list[0];
if (visitedSet.has(node)) {
path.pop();
list.shift();
} else {
visitedSet.add(node);
node[children] && list.unshift(...node[children]);
path.push(node);
func(node) && result.push([...path]);
}
}
return result;
}
function filter(tree, func, config = {}) {
config = getConfig(config);
const children = config.children;
function listFilter(list) {
return list.map((node) => ({ ...node })).filter((node) => {
node[children] = node[children] && listFilter(node[children]);
return func(node) || node[children] && node[children].length;
});
}
return listFilter(tree);
}
function forEach(tree, func, config = {}) {
config = getConfig(config);
const list = [...tree];
const { children } = config;
for (let i = 0; i < list.length; i++) {
if (func(list[i]))
return;
children && list[i][children] && list.splice(i + 1, 0, ...list[i][children]);
}
}
function treeMap(treeData, opt) {
return treeData.map((item) => treeMapEach(item, opt));
}
function treeMapEach(data, { children = "children", conversion }) {
const haveChildren = Array.isArray(data[children]) && data[children].length > 0;
const conversionData = conversion(data) || {};
if (haveChildren) {
return {
...conversionData,
[children]: data[children].map((i) => treeMapEach(i, {
children,
conversion
}))
};
} else {
return {
...conversionData
};
}
}
function findChildrens(tree, childrenId, func, config = {}) {
config = getConfig(config);
const childrens = [];
const list = [...tree];
const { children, id, pid } = config;
function traverse(_list, _childrenId, _parentId) {
for (let i = 0; i < _list.length; i++) {
const item = _list[i];
if (item[id] === _childrenId || item[pid] === _parentId) {
childrens.push(func(item) ?? item);
if (item[children] && item[children].length) {
traverse(item[children], _childrenId, item[id]);
}
} else {
if (item[children] && item[children].length) {
traverse(item[children], _childrenId);
}
}
}
}
traverse(list, childrenId);
return childrens;
}
function findParents(tree, parentId, func, config = {}) {
config = getConfig(config);
const parents = [];
const list = [...tree];
const { children, pid, id } = config;
function traverse(_list, _parentId) {
for (let i = 0; i < _list.length; i++) {
const item = _list[i];
if (item[id] === _parentId) {
parents.push(func(item) ?? item);
traverse(list, item[pid]);
break;
} else {
if (item[children] && item[children].length) {
traverse(item[children], _parentId);
}
}
}
}
traverse(list, parentId);
return parents;
}
export { filter, findChildrens, findNode, findNodeAll, findParents, findPath, findPathAll, forEach, listToTree, treeMap, treeMapEach, treeToList };
//# sourceMappingURL=treeHelper2.mjs.map