DEV Community

Cover image for I made a library to print object as a tree structure like the unix tree command
Muhammad Sifat Hossain
Muhammad Sifat Hossain

Posted on


I made a library to print object as a tree structure like the unix tree command

I'm currently working on a project where I have some hierarchical data that need's to be printed in the console as a tree structure.

At first, I thought let me see if it's available on npm. But before going to npm I told myself: "Why don't I try it myself first?". So I started working on it and by the time I finished it I realized that it's 2 am in the morning πŸ˜…. Today I've published the library on npm. It's called flexible-tree-printer.


import { printTree } from "flexible-tree-printer";

const categories = {
  study: {
    academic: { Math: null, English: null },
    programming: {
      DSA: null,
      "Number Theory": {},
      Backend: {
        "Node.Js": {},
        Sqlite: {},
  work: {
    personal_projects: null,
    job: {},

  parentNode: categories,
  printRootNode: () => console.log("categories"),
Enter fullscreen mode Exit fullscreen mode

Running the above snippet produces the following result:

β”œβ”€β”€ study
β”‚   β”œβ”€β”€ academic
β”‚   β”‚   β”œβ”€β”€ Math
β”‚   β”‚   └── English
β”‚   └── programming
β”‚       β”œβ”€β”€ DSA
β”‚       β”œβ”€β”€ Number Theory
β”‚       └── Backend
β”‚           β”œβ”€β”€ Node.Js
β”‚           └── Sqlite
└── work
    β”œβ”€β”€ personal_projects
    └── job
Enter fullscreen mode Exit fullscreen mode

Almost every behavior of printing is customizable and the library has a very flexible API.

I would love to hear your feedback and kindly give it a ⭐ GitHub if you find it interesting.

Top comments (0)


11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!