UNPKG

@triviality/core

Version:
114 lines 4.74 kB
"use strict"; /** * Refactored version of https://gist.github.com/antonmedv/2d2d478a41d7fcbc1bcb Anton Medvedev * * SELECT_________________ * / \ \ * .___ FROM JOIN * / \ | / \ * a city_name people address ON * | * =___________ * / \ * .____ . * / \ / \ * p address_id a id * */ Object.defineProperty(exports, "__esModule", { value: true }); var repeat = function (txt, times) { var output = ''; // tslint:disable-next-line:no-increment-decrement for (var i = 0; i < times; i++) { output += txt; } return output; }; function drawTree(initial, getTitle, getNodes) { var whitespace = ' '; var output = []; var checkEmpty = function (onLine) { if (output[onLine] === undefined) { output[onLine] = ''; } }; var findPadding = function (_txt, onLine, _position, margin) { if (margin === void 0) { margin = 2; } checkEmpty(onLine); var padding = 0; var position = _position; var length = output[onLine].length; if (position < 0) { padding = -position; position = 0; } if (length >= position) { padding += length - position + margin; } return padding; }; var insert = function (txt, onLine, position) { checkEmpty(onLine); var length = output[onLine].length; if (position < 0) { throw new Error("Trying to insert \"" + txt + "\" at negative position(" + position + ")."); } if (position < length) { throw new Error("Trying to insert \"" + txt + "\" at position(" + position + ") less then length(" + length + ")."); } output[onLine] += repeat(whitespace, position - length) + txt; }; var drawNode = function (tree, onLine, position) { var padding = 0; var foundedPadding = 0; var nodePadding = 0; var node; var title; var offset = 0; var currentTitle = getTitle(tree); var nodes = getNodes(tree); if (nodes && nodes.length !== 0) { var at = position; if (nodes.length === 1) { node = nodes[0]; title = getTitle(node); var halfOfCurrentTitle = Math.floor(currentTitle.length / 2); offset = Math.floor(title.length / 2) - halfOfCurrentTitle; foundedPadding = findPadding(title, onLine + 2, position - offset); nodePadding = drawNode(node, onLine + 2, position - offset + foundedPadding); insert('|', onLine + 1, position + halfOfCurrentTitle + foundedPadding + nodePadding); padding = foundedPadding + nodePadding; } else { // tslint:disable-next-line:no-increment-decrement for (var i = 0; i < nodes.length; i++) { node = nodes[i]; title = getTitle(node); if (i === 0) { offset = title.length === 1 ? 2 : Math.floor(title.length / 2) + 1; foundedPadding = findPadding(title, onLine + 2, position - offset); nodePadding = drawNode(node, onLine + 2, position - offset + foundedPadding); insert('/', onLine + 1, position - 1 + foundedPadding + nodePadding); insert(repeat(whitespace, currentTitle.length), onLine + 1, position + foundedPadding + nodePadding); padding = foundedPadding + nodePadding; at += padding + currentTitle.length; } else { offset = title.length === 1 ? -1 : Math.floor(title.length / 2) - 1; foundedPadding = findPadding(title, onLine + 2, at - offset); nodePadding = drawNode(node, onLine + 2, at - offset + foundedPadding); at += foundedPadding + nodePadding; insert('\\', onLine + 1, at); currentTitle += repeat('_', foundedPadding + nodePadding); } } } } insert(currentTitle, onLine, position + padding); return padding; }; drawNode(initial, 0, 0); return output.join('\n'); } exports.drawTree = drawTree; //# sourceMappingURL=asciitree.js.map