Skip to content

Commit

Permalink
Refactor printNode helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
savq committed Jun 25, 2024
1 parent b4350d9 commit 1929fa3
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions bin/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ import process from "node:process";
import readline from "node:readline/promises";
import { parser } from "../dist/index.js";

function echo(s) {
process.stdout.write(s);
}

function printNode(input, n, depth) {
echo(`${" ".repeat(depth * 2)} ${n.name}`);
if (n.node.firstChild) {
echo(` ${n.from}..${n.to}`);
} else {
echo(`: ${input.slice(n.from, n.to)}`);
}
echo("\n");
function printNode(input, n, depth, indentUnit = 2) {
// If node is terminal, show content, else show range
const content = n.node.firstChild === null
? `: ${input.slice(n.from, n.to)}`
: ` ${n.from}..${n.to}`;
console.log(" ".repeat(depth * indentUnit) + n.name + content);
}

function printTree(input) {
Expand Down

0 comments on commit 1929fa3

Please sign in to comment.