npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

format-hierarchy

v0.1.2

Published

A lightweight TypeScript / JavaScript utility to format and stringify objects and hierarchies with an emphasis on consuming less space when long lists of short values are present.

Readme

format-hierarchy

npm version npm license

A lightweight TypeScript / JavaScript utility to format and stringify objects and hierarchies with an emphasis on consuming less space when long lists of short values are present.

Examples

Useful for...

Wrapping long arrays of short values

{
 "team":{
  "name":"Support",
  "members":[
   "Ann", "Alice", "Bob", "Dan", "Charlie",
   "Eleanor", "Fiona", "Gina", "Harry", "Ivy",
   "Jack", "Kate", "Leo", "Mia", "Nick",
   "Olivia", "Pam", "Quinn", "Rose", "Sam",
   "Tina", "Uma", "Victor", "Wendy", "Xavier",
   "Yara", "Zack",
  ],
 },
}

Inlining and wrapping lists of short objects

{
 "team":{
  "name":"Support",
  "members":[
   {"name":"Ann","role":"admin"},
   {"name":"Bob","role":"developer"},
   {"name":"Charlie","role":"tester"},
   {
    "name":"Dan",
    "role":"project manager",
   },
   {"name":"Eve","role":"designer"},
   {
    "name":"Frank",
    "role":"assistant to the regional sales manager",
   },
   {
    "name":"Guy",
    "role":"deputy assistant to the regional sales manager",
   },
   {"name":"Hank","role":"intern"},
   {"name":"Inga","role":"intern"},
  ],
 },
}

Wraps to specified line length

formatHierarchy(data, { wrapAtWidth: 80 })

{
 "team":{
  "name":"Support",
  "members":[
   {"name":"Ann","role":"admin","aliases":["Anna","Annabel","Annie","Beth"]},
   {"name":"Bob","role":"developer"}, {"name":"Charlie","role":"tester"},
   {"name":"Dan","role":"project manager"}, {"name":"Eve","role":"designer"},
   {"name":"Frank","role":"assistant to the regional sales manager"},
   {"name":"Guy","role":"deputy assistant to the regional sales manager"},
   {"name":"Hank","role":"intern"}, {"name":"Inga","role":"intern"},
  ],
 },
}

formatHierarchy(data, { wrapAtWidth: 30 }) (note that long strings are not broken)

{
 "team":{
  "name":"Support",
  "members":[
   {
    "name":"Ann",
    "role":"admin",
    "aliases":[
     "Anna", "Annabel",
     "Annie", "Beth",
    ],
   },
   {
    "name":"Bob",
    "role":"developer",
   },
   {
    "name":"Charlie",
    "role":"tester",
   },
   {
    "name":"Dan",
    "role":"project manager",
   },
   {
    "name":"Eve",
    "role":"designer",
   },
   {
    "name":"Frank",
    "role":"assistant to the regional sales manager",
   },
   {
    "name":"Guy",
    "role":"deputy assistant to the regional sales manager",
   },
   {
    "name":"Hank",
    "role":"intern",
   },
   {
    "name":"Inga",
    "role":"intern",
   },
  ],
 },
}

Installation

npm install format-hierarchy

or with yarn / pnpm / bun:

pnpm add format-hierarchy

Features

  • Width-Aware Line Wrapping (wrapAtWidth): Target a maximum column width in characters; items and lines wrap automatically once the threshold is reached.
  • Context-Aware Object Inlining: Short objects and arrays stay compact on a single line if they fit within the remaining column width (accounting for indentation and property key length).
  • Safe Stringification: Safely catches circular references or serialization errors by embedding error messages instead of throwing.
  • Dual Package: Provides full TypeScript definitions (.d.ts), ES Modules (esm), and CommonJS (cjs) builds.

Usage

Basic Example

import { formatHierarchy } from "format-hierarchy";

const data = {
  id: "user-123",
  profile: {
    name: "Alice",
    settings: {
      theme: "dark",
      notifications: true,
    },
  },
  tags: ["admin", "developer"],
};

// Full multi-line formatting (default)
console.log(formatHierarchy(data));

Output:

{
 "id":"user-123",
 "profile":{
  "name":"Alice",
  "settings":{
   "theme":"dark",
   "notifications":true,
  },
 },
 "tags":[
  "admin", 
  "developer", 
 ],
}

Width-Aware Wrapping (wrapAtWidth)

// Keep compact structures inline and wrap lines at 40 characters
const result = formatHierarchy(data, { wrapAtWidth: 40 });

Output:

{
 "id":"user-123",
 "profile":{
  "name":"Alice",
  "settings":{"theme":"dark","notifications":true},
 },
 "tags":[
  "admin", "developer", 
 ],
}

Options

| Option | Type | Default | Description | | --- | --- | --- | --- | | wrapAtWidth | number | undefined | Target line/column width in characters. When specified, short objects/arrays stay inline if they fit on the line, and array rows wrap once exceeding this width. |

API

export interface FormatHierarchyOptions {
  wrapAtWidth?: number;
}

export function formatHierarchy(
  obj: any,
  options?: FormatHierarchyOptions,
): string | undefined;
  • obj: The object, array, or primitive value to format.
  • options (optional): Configuration object (FormatHierarchyOptions).

Development

# Install dependencies
npm install

# Run unit tests
npm test

# Type check
npm run typecheck

# Build library (ESM, CJS, and .d.ts)
npm run build

Background

Read more about why this utility was created and its origins in BACKGROUND.md.

License

MIT