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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tabula/tree-utils

v0.1.0

Published

Utilities for working with simple tree representation

Downloads

67

Readme

@tabula/tree-utils

Utilities for working with simple tree representation.

Installation

Use the package manager pnpm to install @tabula/tree-utils.

pnpm add @tabula/tree-utils

You can use npm or yarn too.

API

Tree

This package works with trees, which defined in the simple format.

type Leaf = {
  id: number | string;
};

type Branch = {
  id: number | string;

  children: Leaf[];
};

type Node = Leaf | Branch;

type Tree = Node[];

Of course, real types allows to extend properties of nodes.

Example

We use the following tree for examples:

const tree = [
  { id: '1' },
  {
    id: '2',
    children: [
      { id: '2-1' },
      {
        id: '2-2',
        children: [
          { id: '2-2-1' },
          {
            id: '2-2-2',
            children: [{ id: '2-2-2-1' }, { id: '2-2-2-2' }],
          },
          { id: '2-2-3' },
        ],
      },
    ],
  },
  { id: '3' },
  {
    id: '4',
    children: [
      { id: '4-1' },
      { id: '4-2' },
      {
        id: '4-3',
        children: [{ id: '4-3-1' }, { id: '4-3-2' }],
      },
    ],
  },
];

Traverse

There are breadth and depth functions available to traverse by the tree in iterator style. Each of them returns traverse item on each step, which can be specific for leaf or branch.

Each item has the following properties:

  • node: is node object itself;
  • isBranch: boolean value which true if node is branch;
  • isLeaf: boolean value which true if node is leaf;
  • level: nesting level starting from 0;
  • parentId: optional id of direct parent node;
  • parentIds: set of all parent ids from each nesting level.

We mark a node as branch if node has children property (even if it's an empty array and hasn't any children).

breadth

This method returns iterator for traverse by tree using breadth-first search.

import { breadth } from "@tabula/tree-utils";

for (const { node } of breadth(tree)) {
  console.log(node.id);
}

// 1, 2, 3, 4, 2-1, 2-2, 4-1, 4-2, 4-3, 2-2-1, 2-2-2, 2-2-3, 4-3-1, 4-3-2, 2-2-2-1, 2-2-2-2

filter

You can pass filter for nodes.

import { breadth } from "@tabula/tree-utils";

const filter = (item) => item.level < 3 && item.node.id !== '4';

for (const { node } of breadth(tree, { filter })) {
  console.log(node.id);
}

// 1, 2, 3, 2-1, 2-2, 2-2-1, 2-2-2, 2-2-3

subTree

You can pass id for subtree, to iterate over that subtree.

import { breadth } from "@tabula/tree-utils";

for (const { node } of breadth(tree, { subTree: '2-2' })) {
  console.log(node.id);
}

// 2-2, 2-2-1, 2-2-2, 2-2-3, 2-2-2-1, 2-2-2-2

filter and subTree

You can combine the subTree and filter options. There are items of subtree will be filtered.

import { breadth } from "@tabula/tree-utils";

const filter = (item) => item.level < 3;

for (const { node } of breadth(tree, { filter, subTree: '2-2' })) {
  console.log(node.id);
}

// 2-2, 2-2-1, 2-2-2, 2-2-3

depth

This method returns iterator for traverse by tree using depth-first search.

import { depth } from "@tabula/tree-utils";

for (const { node } of depth(tree)) {
  console.log(node.id);
}

// 1, 2, 2-1, 2-2, 2-2-1, 2-2-2, 2-2-2-1, 2-2-2-2, 2-2-3, 3, 4, 4-1, 4-2, 4-3, 4-3-1, 4-3-2

filter

You can pass filter for nodes.

import { depth } from "@tabula/tree-utils";

const filter = (item) => item.level < 3 && item.node.id !== '4';

for (const { node } of depth(tree, { filter })) {
  console.log(node.id);
}

// 1, 2, 2-1, 2-2, 2-2-1, 2-2-2, 2-2-3, 3

subTree

You can pass id for subtree, to iterate over that subtree.

import { depth } from "@tabula/tree-utils";

for (const { node } of depth(tree, { subTree: '2-2' })) {
  console.log(node.id);
}

// 2-2, 2-2-1, 2-2-2, 2-2-2-1, 2-2-2-2, 2-2-3

filter and subTree

You can combine the subTree and filter options. There are items of subtree will be filtered.

import { depth } from "@tabula/tree-utils";

const filter = (item) => item.level < 3;

for (const { node } of depth(tree, { filter, subTree: '2-2' })) {
  console.log(node.id);
}

// 2-2, 2-2-1, 2-2-2, 2-2-3

License

This project is ISC licensed.