@orca-fe/tools
Version:
front-end tools
318 lines (312 loc) • 12.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getByPath = getByPath;
exports.list2tree = list2tree;
exports.revFlatMap = revFlatMap;
exports.revMap = revMap;
exports.treeFilter = treeFilter;
exports.treeFind = treeFind;
exports.treeFor = treeFor;
exports.treeMap = treeMap;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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 配置
*/
function revMap(data, callback) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
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(function (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), {}, _defineProperty({}, 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 配置
*/
function revFlatMap(data, callback) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var childFirst = options.childFirst,
_options$childKey2 = options.childKey,
childKey = _options$childKey2 === void 0 ? 'children' : _options$childKey2,
parent = options.parent;
var res = [];
data.forEach(function (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), {}, _defineProperty({}, childKey, children)), parent, index, arr);
if (children) {
res.push.apply(res, _toConsumableArray(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.apply(res, _toConsumableArray(children));
}
}
});
return res;
}
/**
* 将数组转换为树状结构
*/
function list2tree(list) {
var idField = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id';
var parentIdField = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'parentId';
var childKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'children';
var root = [];
var cache = {};
// create cache
list.forEach(function (item) {
var id = item[idField];
cache[id] = item;
});
// build tree
list.forEach(function (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;
}
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;
}
function treeFind(data, callback, getChildren) {
function rev(data) {
var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
for (var i = 0; i < data.length; i++) {
var currentPath = [].concat(_toConsumableArray(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
*/
function treeFor(arr, callback) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
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(function (item, index) {
var path = [].concat(_toConsumableArray(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
*/
function treeMap(arr, callback) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
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(function (item, index) {
var path = [].concat(_toConsumableArray(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
function treeFilter(arr, callback) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var _options$childrenKey3 = options.childrenKey,
childrenKey = _options$childrenKey3 === void 0 ? 'children' : _options$childrenKey3;
function rev(list) {
var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
return list.map(function (item) {
// 对象类型,取出子节点判断
if (item && _typeof(item) === 'object') {
// 先取出 children
var children = item[childrenKey];
if (Array.isArray(children)) {
// 存在 children,则先遍历 叶子
var newChildren = rev(children, [].concat(_toConsumableArray(path), [item]));
// 如果子节点存在内容,则本节点必须存在
if (newChildren.length > 0) {
return _objectSpread(_objectSpread({}, item), {}, _defineProperty({}, childrenKey, newChildren));
}
}
}
// 判断当前节点是否需要保留
var res = callback(item, [].concat(_toConsumableArray(path), [item]));
if (res) {
// 保留
return item;
}
// 其余内容不保留,后续过滤掉所有 removeFlag
return removeFlag;
}).filter(function (item) {
return item !== removeFlag;
});
}
return rev(arr);
}