Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jessitron/stringify-tree
make a tree structure into a nice ascii printout
https://github.com/jessitron/stringify-tree
Last synced: about 1 month ago
JSON representation
make a tree structure into a nice ascii printout
- Host: GitHub
- URL: https://github.com/jessitron/stringify-tree
- Owner: jessitron
- Created: 2018-12-21T18:14:02.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-30T02:54:08.000Z (over 1 year ago)
- Last Synced: 2024-12-02T04:46:15.912Z (about 2 months ago)
- Language: TypeScript
- Size: 7.02 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-ascii - stringify-tree
README
# stringify-tree
Convert a tree structure to an ASCII tree, in approximately the style of `npm ls`
This is in TypeScript, which helps since you pass it some functions.
## install
`npm install stringify-tree`
## trivial example
```typescript
import { stringifyTree } from "stringify-tree";const tree = {
name: "Grandmarti", children: [
{
name: "Cyndi", children: [
{
name: "Jess", children: [
{ name: "Evelyn", children: [] },
{ name: "Linda", children: [] },
],
},
],
},
{ name: "Celia", children: [] },
],
};
console.log(stringifyTree(tree, t => t.name, t => t.children));
``````
┬ Grandmarti
├─┬ Cyndi
│ └─┬ Jess
│ ├── Evelyn
│ └── Linda
└── Celia
```## real-life example
```typescript
import { stringifyTree } from "stringify-tree";function treeNodeName(tn: TreeNode) {
const endOffset = tn.$value ? ", " + (tn.$offset + tn.$value.length) : "";
return `[${tn.$offset}${endOffset}] ${tn.$name}`;
}
function treeNodeChildren(tn: TreeNode): TreeNode[] {
return tn.$children || [];
}// parser stuff from Atomist. trust me, it makes a TreeNode with fields like the above
const ast = await Java9FileParser.toAst(p.findFileSync(path));
console.log(stringifyTree(ast, treeNodeName, treeNodeChildren));
``````
```