unist utility to inspect trees
This is a tiny, but useful, package that pretty prints any unist tree.
This utility pretty prints the tree in a format custom made for unist trees. This is useful for spotting bugs, as well as getting an idea of whats going on in the tree. Both trees and node lists can be printed.
This package is ESM only.
In Node.js (18+) with yarn:
yarn add @flex-development/unist-util-inspectSee Git - Protocols | Yarn Β for info regarding installing from Git.
In Deno with esm.sh:
import { inspect } from 'https://esm.sh/@flex-development/unist-util-inspect'In browsers with esm.sh:
<script type="module">
import { inspect } from 'https://esm.sh/@flex-development/unist-util-inspect'
</script>With bun:
bun add @flex-development/unist-util-inspectSuppose we have the file dbl-linear.mts, and the following module, example.mts:
import type * as docast from '@flex-development/docast'
import { fromDocs } from '@flex-development/docast-util-from-docs'
import { inspect } from '@flex-development/unist-util-inspect'
import { read } from 'to-vfile'
import type { VFile } from 'vfile'
const file: VFile = await read('dbl-linear.mts')
const tree: docast.Root = fromDocs(file)
console.log(inspect(tree))...running node --experimental-strip-types --experimental-transform-types ./example.mts yields:
root[7]
ββ0 comment[3] (1:1-5:4, 0-106)
β ββ0 blockTag<@file>[1] (2:4-2:19, 7-22)
β β ββ0 text "dblLinear" (2:10-2:19, 13-22)
β ββ1 blockTag<@module>[1] (3:4-3:21, 26-43)
β β ββ0 text "dblLinear" (3:12-3:21, 34-43)
β ββ2 blockTag<@see>[1] (4:4-4:59, 47-102)
β ββ0 text "https://codewars.com/kata/5672682212c8ecf83e000050" (4:9-4:59, 52-102)
ββ1 comment[7] (7:1-27:4, 108-684)
β ββ0 description[7] (8:4-14:68, 115-416)
β β ββ0 paragraph[5] (8:4-8:60, 115-171)
β β β ββ0 text "Consider a sequence " (8:4-8:24, 115-135)
β β β ββ1 inlineCode "u" (8:24-8:27, 135-138)
β β β ββ2 text " where " (8:27-8:34, 138-145)
β β β ββ3 inlineCode "u" (8:34-8:37, 145-148)
β β β ββ4 text " is defined as follows:" (8:37-8:60, 148-171)
β β ββ1 break (8:60-9:1, 171-172)
β β ββ2 break (9:3-10:1, 174-175)
β β β data: {"blank":true}
β β ββ3 list[3] (1:1-1:1)
β β β β ordered: true
β β β β start: 1
β β β β spread: false
β β β ββ0 listItem[4] (10:5-10:53, 179-227)
β β β β β spread: false
β β β β β checked: null
β β β β ββ0 text "The number " (10:8-10:19, 182-193)
β β β β ββ1 inlineCode "u(0) = 1" (10:19-10:29, 193-203)
β β β β ββ2 text " is the first one in " (10:29-10:50, 203-224)
β β β β ββ3 inlineCode "u" (10:50-10:53, 224-227)
β β β ββ1 listItem[11] (11:5-11:77, 232-304)
β β β β β spread: false
β β β β β checked: null
β β β β ββ0 text "For each " (11:8-11:17, 235-244)
β β β β ββ1 inlineCode "x" (11:17-11:20, 244-247)
β β β β ββ2 text " in " (11:20-11:24, 247-251)
β β β β ββ3 inlineCode "u" (11:24-11:27, 251-254)
β β β β ββ4 text ", " (11:27-11:29, 254-256)
β β β β ββ5 inlineCode "y = 2x + 1" (11:29-11:41, 256-268)
β β β β ββ6 text " and " (11:41-11:46, 268-273)
β β β β ββ7 inlineCode "z = 3x + 1" (11:46-11:58, 273-285)
β β β β ββ8 text " must be in " (11:58-11:70, 285-297)
β β β β ββ9 inlineCode "u" (11:70-11:73, 297-300)
β β β β ββ10 text " too" (11:73-11:77, 300-304)
β β β ββ2 listItem[2] (12:5-12:41, 309-345)
β β β β spread: false
β β β β checked: null
β β β ββ0 text "There are no other numbers in " (12:8-12:38, 312-342)
β β β ββ1 inlineCode "u" (12:38-12:41, 342-345)
β β ββ4 break (12:41-13:1, 345-346)
β β ββ5 break (13:3-14:1, 348-349)
β β β data: {"blank":true}
β β ββ6 paragraph[5] (14:4-14:68, 352-416)
β β ββ0 text "Given an index, " (14:4-14:20, 352-368)
β β ββ1 inlineCode "n" (14:20-14:23, 368-371)
β β ββ2 text ", the function returns the element at " (14:23-14:61, 371-409)
β β ββ3 inlineCode "u(n)" (14:61-14:67, 409-415)
β β ββ4 text "." (14:67-14:68, 415-416)
β ββ1 blockTag<@example>[1] (16:4-17:28, 423-459)
β β ββ0 code "await dblLinear(0) // 1" (17:5-17:28, 436-459)
β β lang: null
β β meta: null
β ββ2 blockTag<@example>[1] (18:4-19:30, 463-501)
β β ββ0 code "await dblLinear(10) // 22" (19:5-19:30, 476-501)
β β lang: null
β β meta: null
β ββ3 blockTag<@example>[1] (20:4-21:32, 505-545)
β β ββ0 code "await dblLinear(100) // 447" (21:5-21:32, 518-545)
β β lang: null
β β meta: null
β ββ4 blockTag<@example>[1] (22:4-23:36, 549-593)
β β ββ0 code "await dblLinear(7687) // 111718" (23:5-23:36, 562-593)
β β lang: null
β β meta: null
β ββ5 blockTag<@param>[2] (25:4-25:46, 600-642)
β β ββ0 typeExpression "number" (25:11-25:19, 607-615)
β β ββ1 text "n - Sequence element index" (25:20-25:46, 616-642)
β ββ6 blockTag<@return>[3] (26:4-26:38, 646-680)
β ββ0 typeExpression "number" (26:12-26:20, 654-662)
β ββ1 text "Element at " (26:21-26:32, 663-674)
β ββ2 inlineCode "u(n)" (26:32-26:38, 674-680)
ββ2 comment[2] (29:3-33:6, 727-781)
β ββ0 description[1] (30:6-30:15, 736-745)
β β ββ0 paragraph[1] (30:6-30:15, 736-745)
β β ββ0 text "Sequence." (30:6-30:15, 736-745)
β ββ1 blockTag<@const>[2] (32:6-33:1, 756-776)
β ββ0 typeExpression "number[]" (32:13-32:23, 763-773)
β ββ1 text "u" (32:24-32:25, 774-775)
ββ3 comment[2] (36:3-40:6, 811-900)
β ββ0 description[1] (37:6-37:54, 820-868)
β β ββ0 paragraph[3] (37:6-37:54, 820-868)
β β ββ0 text "Index of x in " (37:6-37:20, 820-834)
β β ββ1 inlineTag<@linkcode> "u" (37:20-37:33, 834-847)
β β ββ2 text " used to calculate y." (37:33-37:54, 847-868)
β ββ1 blockTag<@var>[2] (39:6-40:1, 879-895)
β ββ0 typeExpression "number" (39:11-39:19, 884-892)
β ββ1 text "j" (39:20-39:21, 893-894)
ββ4 comment[2] (43:3-47:6, 924-1013)
β ββ0 description[1] (44:6-44:54, 933-981)
β β ββ0 paragraph[3] (44:6-44:54, 933-981)
β β ββ0 text "Index of x in " (44:6-44:20, 933-947)
β β ββ1 inlineTag<@linkcode> "u" (44:20-44:33, 947-960)
β β ββ2 text " used to calculate z." (44:33-44:54, 960-981)
β ββ1 blockTag<@var>[2] (46:6-47:1, 992-1008)
β ββ0 typeExpression "number" (46:11-46:19, 997-1005)
β ββ1 text "j" (46:20-46:21, 1006-1007)
ββ5 comment[2] (52:5-56:8, 1118-1173)
β ββ0 description[1] (53:8-53:12, 1129-1133)
β β ββ0 paragraph[2] (53:8-53:12, 1129-1133)
β β ββ0 inlineCode "y" (53:8-53:11, 1129-1132)
β β ββ1 text "." (53:11-53:12, 1132-1133)
β ββ1 blockTag<@const>[2] (55:8-56:1, 1148-1166)
β ββ0 typeExpression "number" (55:15-55:23, 1155-1163)
β ββ1 text "y" (55:24-55:25, 1164-1165)
ββ6 comment[2] (59:5-63:8, 1215-1270)
ββ0 description[1] (60:8-60:12, 1226-1230)
β ββ0 paragraph[2] (60:8-60:12, 1226-1230)
β ββ0 inlineCode "z" (60:8-60:11, 1226-1229)
β ββ1 text "." (60:11-60:12, 1229-1230)
ββ1 blockTag<@const>[2] (62:8-63:1, 1245-1263)
ββ0 typeExpression "number" (62:15-62:23, 1252-1260)
ββ1 text "z" (62:24-62:25, 1261-1262)This package exports the identifiers
inspect,
inspectColor,
and inspectNoColor.
There is no default export.
Inspect a tree with color in supported environments or without color in environments that do not support color.
tree(unknown) β the tree to inspectoptions(Options|null|undefined) β configuration options
(string) Pretty printed tree
Inspect a tree with color.
Otherwise the same as inspect.
Inspect a tree without color.
Otherwise the same as inspect.
This package is fully typed with TypeScript.
Inspect a tree (TypeScript type).
type Inspect = (tree: unknown, options?: Options | null | undefined) => stringtree(unknown) β the tree to inspectoptions(Options|null|undefined) β configuration options
(string) Pretty printed tree
Configuration options (TypeScript interface).
positions?(boolean | null | undefined) β whether to include positional information- default:
true
- default:
See CONTRIBUTING.md.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.