UNPKG

drip-table

Version:

A tiny and powerful enterprise-class solution for building tables.

143 lines (111 loc) 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapRecursive = exports.forEachRecursive = exports.flattenRecursive = exports.findRecursive = exports.findIndexRecursive = exports.filterRecursive = void 0; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * This file is part of the drip-table project. * @link : https://drip-table.jd.com/ * @author : Emil Zhai (root@derzh.com) * @modifier : Emil Zhai (root@derzh.com) * @copyright: Copyright (c) 2021 JD Network Technology Co., Ltd. */ var forEachRecursive = function forEachRecursive(columns, callbackfn) { var index = -1; var iter = function iter(cs) { cs.forEach(function (c) { var _c$children; if ('children' in c && (_c$children = c.children) !== null && _c$children !== void 0 && _c$children.length) { iter(c.children); } else { index += 1; callbackfn(c, index); } }); }; return iter(columns); }; exports.forEachRecursive = forEachRecursive; var flattenRecursive = function flattenRecursive(columns) { var res = []; forEachRecursive(columns, function (c) { res.push(c); }); return res; }; exports.flattenRecursive = flattenRecursive; var mapRecursive = function mapRecursive(columns, callbackfn) { var index = -1; var iter = function iter(cs) { return cs.map(function (c) { var _c$children2; if ('children' in c && (_c$children2 = c.children) !== null && _c$children2 !== void 0 && _c$children2.length) { return _objectSpread(_objectSpread({}, c), {}, { children: iter(c.children) }); } index += 1; return callbackfn(c, index); }); }; return iter(columns); }; exports.mapRecursive = mapRecursive; var filterRecursive = function filterRecursive(columns, callbackfn) { var index = -1; var iter = function iter(cs) { var res = []; cs.forEach(function (c) { var _c$children3; if ('children' in c && (_c$children3 = c.children) !== null && _c$children3 !== void 0 && _c$children3.length) { var children = iter(c.children); if (children.length > 0) { res.push(_objectSpread(_objectSpread({}, c), {}, { children: children })); } } else { index += 1; if (callbackfn(c, index)) { res.push(c); } } }); return res; }; return iter(columns); }; exports.filterRecursive = filterRecursive; var findRecursive = function findRecursive(columns, callbackfn) { var index = -1; var found = void 0; forEachRecursive(columns, function (c) { if (found) { return; } index += 1; if (callbackfn(c, index)) { found = c; } }); return found; }; exports.findRecursive = findRecursive; var findIndexRecursive = function findIndexRecursive(columns, callbackfn) { var index = -1; var found = false; forEachRecursive(columns, function (c) { if (found) { return; } index += 1; if (callbackfn(c, index)) { found = true; } }); return found ? index : -1; }; exports.findIndexRecursive = findIndexRecursive;