Skip to content

Commit a67bcb2

Browse files
OttoAllmendingerllm-git
andcommitted
feat(utxo-bin): add formatObjAsTree helper
Adds a utility function that creates a formatted tree output from any arbitrary object by first parsing it and then applying the regular tree formatter. Issue: BTC-2170 Co-authored-by: llm-git <[email protected]>
1 parent badbb14 commit a67bcb2

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

modules/utxo-bin/src/format.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import { Chalk, Instance } from 'chalk';
22
import archy from 'archy';
33

4-
import { ParserNode, ParserNodeValue } from './Parser';
4+
import { Parser, ParserNode, ParserNodeValue } from './Parser';
5+
import { parseUnknown } from './parseUnknown';
56

67
const hideDefault = ['pubkeys', 'sequence', 'locktime', 'scriptSig', 'witness'];
78

89
export function formatSat(v: number | bigint): string {
910
return (Number(v) / 1e8).toFixed(8);
1011
}
1112

13+
type FormatOptions = {
14+
hide?: string[];
15+
chalk?: Chalk;
16+
};
17+
18+
function getDefaultChalk(): Chalk {
19+
if (process.env.NO_COLOR) {
20+
return new Instance({ level: 0 });
21+
}
22+
return new Instance();
23+
}
24+
1225
export function formatTree(
1326
n: ParserNode,
14-
{ hide = hideDefault, chalk = new Instance() }: { hide?: string[]; chalk?: Chalk } = {}
27+
{ hide = hideDefault, chalk = getDefaultChalk() }: FormatOptions = {}
1528
): string {
1629
function getLabel(
1730
label: string | number,
@@ -61,3 +74,13 @@ export function formatTree(
6174

6275
return archy(toArchy(n));
6376
}
77+
78+
export function formatObjAsTree(
79+
label: string | number,
80+
obj: unknown,
81+
{ hide = hideDefault, chalk = getDefaultChalk() }: FormatOptions = {}
82+
): string {
83+
const p = new Parser({ parseError: 'continue' });
84+
const node = parseUnknown(p, label, obj, { omit: hide });
85+
return formatTree(node, { hide, chalk });
86+
}

0 commit comments

Comments
 (0)