@sha1n/dagraph
Version:
Directed acyclic graph utility in TypeScript
43 lines (42 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTreeAsciiFormatter = exports.createIndentFormatter = void 0;
/**
* Creates a visitor that accumulates a string representation of the graph structure using indentation.
*
* @param labelFn optional function to generate a label for each node. Defaults to node.id.
* @param indent optional string to use for indentation. Defaults to 2 spaces.
* @returns a DAGVisitor that pushes lines to the context array.
*/
function createIndentFormatter(labelFn = n => n.id, indent = ' ') {
return (node, { depth }, lines) => {
lines.push(`${indent.repeat(depth)}${labelFn(node)}`);
};
}
exports.createIndentFormatter = createIndentFormatter;
/**
* Creates a visitor that accumulates a tree-like string representation of the graph structure
* using unicode box-drawing characters (├──, └──, │).
*
* @param labelFn optional function to generate a label for each node. Defaults to node.id.
* @returns a DAGVisitor that pushes lines to the context array.
*/
function createTreeAsciiFormatter(labelFn = n => n.id) {
const isLastChild = [];
return (node, { depth, index, total }, lines) => {
const isLast = index === total - 1;
isLastChild[depth] = isLast;
let prefix = '';
if (depth > 0) {
for (let i = 1; i < depth; i++) {
prefix += isLastChild[i] ? ' ' : '│ ';
}
const connector = isLast ? '└── ' : '├── ';
lines.push(`${prefix}${connector}${labelFn(node)}`);
}
else {
lines.push(labelFn(node));
}
};
}
exports.createTreeAsciiFormatter = createTreeAsciiFormatter;