UNPKG

@kaspersky/components

Version:

Kaspersky Design System UI Kit

59 lines (58 loc) 1.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateTreeData = generateTreeData; exports.getKeys = getKeys; function getKeys(nodes) { const out = []; for (const node of nodes) { out.push(node.key); if (node.children) { out.push(...getKeys(node.children)); } } return out; } function generateTreeData(num) { let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { depth: 3, width: 3 }; const nodes = []; let currentRootWidth = 0; let currentNum = 0; function isNumberOfNodesExceeded() { return currentNum >= num; } function flipCoin() { return Math.random() < 0.5; } function createNode(key, currentDepth) { const node = { key, isLeaf: true, title: key }; currentNum++; if (!isNumberOfNodesExceeded() && config.depth > currentDepth && flipCoin()) { node.isLeaf = false; node.children = []; let childrenNumber = 0; while (true) { node.children.push(createNode(`${key}-${childrenNumber++}`, currentDepth + 1)); if (isNumberOfNodesExceeded() || config.width === childrenNumber || flipCoin()) { break; } } } return node; } function createRootNode() { return createNode(`0-${currentRootWidth++}`, 1); } while (!isNumberOfNodesExceeded()) { nodes.push(createRootNode()); } return nodes; }