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

immutable-flat-tree

v0.0.3

Published

Typescript flat tree structures with immutability as core concept

Downloads

8

Readme

Immutable Flat Tree

Long Story Short

Tree<T> {
  rootId: Id
  nodemap: { [Id]: Node<T> }
}

Node<T> {
  id: Id
  ...
  value: T
}

Description

There are many powerful libraries that deal with trees in a super efficient way, as they store tree data in Map structures.

But, some potential consumers, like React, don't get this effiency benefit, as they need data structures that react to changes in nodes.

This library comes to the rescue. It offers a simple way to structure trees. Objectively, it won't be as efficient as others, but it's oriented to serve these potential consumers in the way they need.

Install

Using npm:

$ npm install immutable-flat-tree

Using yarn:

$ yarn add immutable-flat-tree

Usage

Create a tree

import { createTree } from 'immutable-flat-tree';

const tree = createTree('some value', 'a');

// Tree:
// a -> 'some value'
//

Create a tree node

import { ... , createTreeNode } from 'immutable-flat-tree';

...

const updatedTree1 = createTreeNode('some other value b', 'b', 'a')(tree);
const updatedTree2 = createTreeNode('some other value c', 'c', 'a')(updatedTree1);
const updatedTree3 = createTreeNode('some other value d', 'd', 'a')(updatedTree2);
const updatedTree4 = createTreeNode('some other value e', 'e', 'b')(updatedTree3);
const updatedTree5 = createTreeNode('some other value f', 'f', 'd')(updatedTree4);

// Updated Tree 5:
// a -> 'some value'
// ├─ b -> 'some other value b'
// │  └─ e -> 'some other value e'
// ├─ c -> 'some other value c'
// └─ d -> 'some other value d'
//    └─ f -> 'some other value f'

Delete a tree node

import { ... , deleteTreeNode } from 'immutable-flat-tree';

...

const updatedTree6 = deleteTreeNode('c')(updatedTree5);

// Updated Tree 6:
// a -> 'some value'
// ├─ b -> 'some other value b'
// │  └─ e -> 'some other value e'
// └─ d -> 'some other value d'
//    └─ f -> 'some other value f'

Delete children from a tree node

import { ... , deleteTreeNodeChildren } from 'immutable-flat-tree';

...

const updatedTree7 = deleteTreeNodeChildren('d')(updatedTree6);

// Updated Tree 7:
// a -> 'some value'
// ├─ b -> 'some other value b'
// │  └─ e -> 'some other value e'
// └─ d -> 'some other value d'

Update parentship of a tree node

import { ... , updateTreeNodeParent } from 'immutable-flat-tree';

...

const updatedTree8 = updateTreeNodeParent('e', 'd')(updatedTree7);

// Updated Tree 8:
// a -> 'some value'
// ├─ b -> 'some other value b'
// └─ d -> 'some other value d'
//    └─ e -> 'some other value e'

Update a tree node value

import { ... , updateTreeNodeValue } from 'immutable-flat-tree';

...

const updatedTree9 = updateTreeNodeValue('b', 'foo')(updatedTree8);

// Updated Tree 9:
// a -> 'some value'
// ├─ b -> 'foo'
// └─ d -> 'some other value d'
//    └─ e -> 'some other value e'

Convert tree to ordered array

import { ... , getOrderedTreeNodes } from 'immutable-flat-tree';

...

const defaultOrderedNodes = getOrderedTreeNodes(updatedTree9);

// Ordered Nodes:
// [a, b, d, e]

const inverseAlphabeticallyOrderedNodes = getOrderedTreeNodes(updatedTree9, (a, b) => b.value - a.value);

// Inverse Alphabetically Ordered Nodes
// [a, d, e, b]

Types

In the usage examples above, a primitive string has been used. But you can use any other type with more complexity like:

import { createTree } from 'immutable-flat-tree';

const tree = createTree({
  name: 'some name value',
  description: 'some description value',
}, 'a')

const orderedByName = getOrderedTreeNodes(tree, (a, b) => a.value.name - b.value.name);