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

@fsdk/tree-utils

v0.0.1

Published

Utilities for tree-like structures

Downloads

6

Readme

Tree Utilities

A set of utility functions frequently used while working with tree-like structures

API

Tree Generator

Thare are 2 tree generation utility functions here:

  • generateTree - Helper function. Provided with options, it returns flat array of tree items. Works on top of the generator.
  • treeGenerator - Generator function. Provided with options, allows to generate random tree, yielding each tree item.

Usage Example:

itemCreator - Both generateTree and treeGenerator rely on creator function used to generate particular tree item.

import { defaultItemCreatorFactory, TreeItemMeta } from '@fsdk/tree-utils';
import { nanoid } from 'nanoid'

type FlatTreeItem = {
  id: string;
  parentId?: string;
  name: string;
  depth: number;
}

const flatItemCreator = (parent: FlatTreeItem | undefined, meta: TreeItemMeta) => ({
  id: nanoid(),
  parentId: parent?.id,
  name: `Item ${id}`,
  depth: meta.level,
});

type NestedTreeItem = {
  id: string;
  children: NestedTreeItem[];
}

const nestedItemCreator = (parent: NestedTreeItem | undefined, meta: TreeItemMeta) => {
  const item: NestedTreeItem = {
    id: nanoid(),
    children: [],
  };

  parent?.children.push(item);

  return item;
};

// alternatively you can use built-in item creator factory to produce `flatItemCreator` above
const itemCreator = defaultItemCreatorFactory();

generateTree:

import { generateTree } from '@fsdk/tree-utils';

const treeItems = generateTree({
  itemCreator,     // Required. Provides function used to create tree item object
  maxDepth: 5,     // Optional. Provides maximum depth of the randomly generated tree. Defults to 10
  minSiblings: 2,  // Optional. Provides minimum nested items count used by randomzier. Defaults to 3
  maxSiblings: 15, // Optional. Provides maximum nested items count used by randomizer. Defaults to 7
  maxItems: 1000,  // Optional. Limits total items generated in case generation exceeds specified distribution. Defaults to 1000
});

// if you use nested tree object structure it is handy to get `root` as all other items are accessible via children property
const [root] = generateTree({
  itemCreator: nestedItemCreator,
});

treeGenerator:

import { treeGenerator } from '@fsdk/tree-utils';

const generator = treeGenerator({
  itemCreator,    // Required. Provides function used to create tree item object
  maxDepth: 10,   // Required. Provides maximum depth of the randomly generated tree.
  minSiblings: 3, // Required. Provides minimum nested items count used by randomzier.
  maxSiblings: 7, // Required. Provides maximum nested items count used by randomizer.
});

// Start generate items by calling `next`
const treeItem = generator.next().value;