UNPKG

@orca-fe/tools

Version:
292 lines (287 loc) 9.9 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /* eslint-disable @typescript-eslint/ban-types */ /** * 遍历树状结构 * @param data 树状数据 * @param callback 返回值处理 * @param options 配置 */ export function revMap(data, callback, options = {}) { var childFirst = options.childFirst, _options$childKey = options.childKey, childKey = _options$childKey === void 0 ? 'children' : _options$childKey, parent = options.parent, replaceChildren = options.replaceChildren; return data.map((datum, index) => { var _newDatum2; if (!datum) return datum; // error object var children = datum[childKey]; var newDatum; if (childFirst) { var newChildren = []; if (Array.isArray(children)) { newChildren = revMap(children, callback, _objectSpread(_objectSpread({}, options), {}, { parent: datum })); } newDatum = callback(_objectSpread(_objectSpread({}, datum), {}, { [childKey]: newChildren }), parent, index); } else { var _newDatum; newDatum = callback(_objectSpread({}, datum), parent, index); var c = replaceChildren ? (_newDatum = newDatum) === null || _newDatum === void 0 ? void 0 : _newDatum[childKey] : children; if (newDatum && Array.isArray(c)) { newDatum[childKey] = revMap(c, callback, _objectSpread(_objectSpread({}, options), {}, { parent: newDatum })); } } return (_newDatum2 = newDatum) !== null && _newDatum2 !== void 0 ? _newDatum2 : datum; }); } /** * 遍历树状结构,并扁平化 * @param data 树状数据 * @param callback 返回值处理 * @param options 配置 */ export function revFlatMap(data, callback, options = {}) { var childFirst = options.childFirst, _options$childKey2 = options.childKey, childKey = _options$childKey2 === void 0 ? 'children' : _options$childKey2, parent = options.parent; var res = []; data.forEach((datum, index, arr) => { // error object if (!datum) { return; } var children = datum[childKey]; var newDatum; if (childFirst) { if (Array.isArray(datum[childKey])) { children = revFlatMap(datum[childKey], callback, _objectSpread(_objectSpread({}, options), {}, { parent: datum })); } newDatum = callback(_objectSpread(_objectSpread({}, datum), {}, { [childKey]: children }), parent, index, arr); if (children) { res.push(...children); } res.push(newDatum); } else { newDatum = callback(_objectSpread({}, datum), parent, index, arr); if (newDatum && Array.isArray(children)) { children = revFlatMap(datum[childKey], callback, _objectSpread(_objectSpread({}, options), {}, { parent: datum })); newDatum['children'] = children; } res.push(newDatum); if (children) { res.push(...children); } } }); return res; } /** * 将数组转换为树状结构 */ export function list2tree(list, idField = 'id', parentIdField = 'parentId', childKey = 'children') { var root = []; var cache = {}; // create cache list.forEach(item => { var id = item[idField]; cache[id] = item; }); // build tree list.forEach(item => { var parentId = item[parentIdField]; var parent = cache[parentId]; if (!parent) { // root root.push(item); } else { if (!parent[childKey]) { parent[childKey] = []; } parent[childKey].push(item); } }); return root; } export function getByPath(data, path, getByIndex) { var root = data; var p = path.slice(); while (p.length > 0) { var _index = p.shift(); if (_index != null) { var _item = root[_index]; if (p.length === 0) { return _item; } if (typeof getByIndex === 'function') { var res = getByIndex(_item); if (!res) return undefined; root = res; } else if (Array.isArray(_item)) { root = _item; } } } return undefined; } export function treeFind(data, callback, getChildren) { function rev(data, path = []) { for (var i = 0; i < data.length; i++) { var currentPath = [...path, i]; var datum = data[i]; var res = callback(datum, currentPath); if (res) { return datum; } var children = typeof getChildren === 'function' ? getChildren(datum) : datum; if (Array.isArray(children)) { var childrenRes = rev(children, currentPath); if (childrenRes) { return childrenRes; } } } return undefined; } return rev(data); } /** * 递归遍历树 * @param arr * @param callback * @param options */ export function treeFor(arr, callback, options = {}) { var _options$childrenKey = options.childrenKey, childrenKey = _options$childrenKey === void 0 ? 'children' : _options$childrenKey, _options$parentPath = options.parentPath, parentPath = _options$parentPath === void 0 ? [] : _options$parentPath, parent = options.parent, childFirst = options.childFirst; arr.forEach((item, index) => { var path = [...parentPath, index]; function triggerCallback() { callback(item, parent, path); } function revChildren() { if (item) { var children = item[childrenKey]; if (Array.isArray(children)) { treeFor(children, callback, _objectSpread(_objectSpread({}, options), {}, { parentPath: path, parent: item })); } } } if (childFirst) { revChildren(); triggerCallback(); } else { triggerCallback(); revChildren(); } }); } /** * 递归遍历树,并依次替换节点获得全新的树 * @param arr * @param callback * @param options */ export function treeMap(arr, callback, options = {}) { var _options$childrenKey2 = options.childrenKey, childrenKey = _options$childrenKey2 === void 0 ? 'children' : _options$childrenKey2, _options$parentPath2 = options.parentPath, parentPath = _options$parentPath2 === void 0 ? [] : _options$parentPath2, parent = options.parent, childFirst = options.childFirst; return arr.map((item, index) => { var path = [...parentPath, index]; var newItem = null; function triggerCallback() { newItem = callback(item, parent, path); } var newChildren = []; function revChildren() { var children = item[childrenKey]; if (Array.isArray(children)) { newChildren = treeMap(children, callback, _objectSpread(_objectSpread({}, options), {}, { parentPath: path, parent: item })); } } if (childFirst) { revChildren(); triggerCallback(); } else { triggerCallback(); revChildren(); } if (newItem) { // @ts-expect-error newItem[childrenKey] = newChildren; } return newItem; }); } var removeFlag = {}; /** * 树过滤器,对树状结构的所有节点进行遍历(子节点优先)。 * 对于需要保留的节点,请返回 true。 * 当保留子节点时,父节点则不会被遍历,必须存在。 * @version 0.10.0 * @param arr * @param callback * @param options */ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type export function treeFilter(arr, callback, options = {}) { var _options$childrenKey3 = options.childrenKey, childrenKey = _options$childrenKey3 === void 0 ? 'children' : _options$childrenKey3; function rev(list, path = []) { return list.map(item => { // 对象类型,取出子节点判断 if (item && typeof item === 'object') { // 先取出 children var children = item[childrenKey]; if (Array.isArray(children)) { // 存在 children,则先遍历 叶子 var newChildren = rev(children, [...path, item]); // 如果子节点存在内容,则本节点必须存在 if (newChildren.length > 0) { return _objectSpread(_objectSpread({}, item), {}, { [childrenKey]: newChildren }); } } } // 判断当前节点是否需要保留 var res = callback(item, [...path, item]); if (res) { // 保留 return item; } // 其余内容不保留,后续过滤掉所有 removeFlag return removeFlag; }).filter(item => item !== removeFlag); } return rev(arr); }