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.

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.

Top comments (0)