3.0.1 • Published 2 years ago

@adamdoescoding/treeify v3.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Treeify

Treeify is an incy-wincy JS library (with TS support) AdamJ-Dev created to determine the render order of comments on his blog. He thought: this logic is not particularly the blog's concern: I'll just put it in it's own library.

Installation:

npm install @adamdoescoding/treeify

Usage:

Given a flattened list of objects, each extending

type Item = {
  id: string;
  // key idea: if item has no parent, the following should be falsy:
  parentId?: string | null; 
}

i.e.

const list: Item[] = [...]

You may call

makeTree(list);

to produce a Tree which exhibits the relevant ancestral relationships in list.

A Tree is a class that has roots. roots are Tree Nodes, which bear an item on their item proprety, as well as a set of child nodes on their children property.

You may also call

orderByTree(list)

which produces the selfsame tree, but returns the (pre-order) depth-first-traversed array produced from it.

For example, assume that you pass orderByTree a flat list of comments that are already ordered by date. Then orderByTree will return those comments in correct render order -- how they should be ordered in a comment section.

3.0.1

2 years ago

2.0.1

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago