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

h_sifat

Muhammad Sifat Hossain

Posted on October 15, 2022

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.

Example:

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: {},
  },
};

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

Running the above snippet produces the following result:

categories
ā”œā”€ā”€ 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.

šŸ’– šŸ’Ŗ šŸ™… šŸš©
h_sifat
Muhammad Sifat Hossain

Posted on October 15, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related