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

entriestree

v0.4.0

Published

Work with recursive array of data in Javascript

Readme

EntriesTree.js

This library is intended to work with recursive array of data in Javascript.

Installation

⬆️ Top ➡️ Guide

This package is available on NPM.

npm i entriestree

Guide

⬆️ Top ➡️ API

Example data

This kind of data looks familiar? This is the sample data used below. You'll find this sample data in fixtures/readmeCollection.js.

- {id: 1, ...}
	- {id: 10, ...}
	- {id: 11, ...}
		- {id: 110, ...}
		- {id: 111, ...}
			- {id: 1110, ...}
			- {id: 1111, ...}
		- {id: 112, ...}
	- {id: 12, ...}
- {id: 2, ...}
	- {id: 20, ...}
		- {id: 200, ...}
	- {id: 21, ...}
	- {id: 22, ...}
		- {id: 221, ...}
- {id: 3, ...}
- {id: 4, ...}
	- {id: 40, ...}
		- {id: 400, ...}
		- {id: 401, ...}
			- {id: 4010, ...}
	- {id: 41, ...}
	- {id: 42, ...}

Initialization

In this example, collection is our structure and each item has an unique identifier called id (customizable). Child items are stored in a property called children (customizable too).

import collection from '...' // above sample data
import EntriesTree from 'entriestree'

const tree = new EntriesTree(collection, 'id', 'children')

Find element

Properties starting with _ are added by the library.

tree.find(11) // returns {id: 11, children: [{id: 110, ...}, ...]}
tree.find(3) // returns {id: 3, ...}
tree.find(-1) // returns null

Also, you can find the nearest ancestor.

tree.findAncestor(110) // returns {id: 11, ...}
tree.findAncestor(1) // returns {id: 1, ...}
tree.findAncestor(-1) // returns null

Find element's parent

tree.findParent(110) // returns {id: 1, children: [...], ...}
tree.findParent(4) // returns null because {id: 4} is a root element

Find element's siblings

tree.findSiblings(1)
// returns {prevItem: null, nextItem: {id: 2, ...}}
tree.findSiblings(111)
// returns {prevItem: {id: 110, ...}, nextItem: {id: 112, ...}}
tree.findSiblings(-1)
// returns {prevItem: null, nextItem: null}

Count elements

It's super easy to get the collection length (recursively).

tree.count() // returns 23
tree.countFrom(11) // returns 5

Update element

tree.find(221) // returns {id: 221, ...}

tree.update(221, (item) => {
  item.foo = 'bar';
  return item
})

tree.find(221) // returns {id: 221, foo: 'bar', ...}

Update element recursively

tree.updateUp(110, (item) => {
  item.foo = 'bar'
  return item
}) // updates 110, 11 and 1 

tree.updateDown(111, (item) => {
  item.bar = 'baz'
  return item
}) // updates 111, 1110 and 1111 

⚠️ Don't forget to return the updated element in your updater function!

Delete element

tree.delete(111) // returns the deleted item
tree.count() // returns 20 since {id: 111} as two children
tree.delete(-1) // returns null

Insert before or after element

tree.insertAfter(1, toInsert) // returns EntriesTree's instance
tree.insertBefore(111, toInsert) // same as above
tree.insertAfter(-1, toInsert) // can't insert after an unexisting element

Loop over flattened elements

tree.iterable()
// returns [{id: 1, ...}, {id: 2, ...}, {id: 3, ...}, ...]

Deep clone a collection

Sometimes it's useful to work on a copy of your elements without noising your original collection.

const tree = new EntriesTree().setCollection(collection, true)
// collection will be untouched
const tree = new EntriesTree().clone(collection)
// same as above

tree.update(1, (item) => {
  item.foo = 'bar'
  return item
})

collection[0].foo !== tree.find(1).foo
// returns false

API

⬆️ Top ➡️ Tests

Methods meant to be private are not listed. Eg: isTheOne, isNode, isRoot, etc

| Method | Params | Return | |--------|--------|--------| | constructor | {Object[]} collection = [], {string} id = 'id', {string} childkey | {EntriesTree} | | setCollection | {Object[]} collection = [], {boolean} clone | {EntriesTree} | | getCollection | | {Object[]} | | clone | {?Object[]} collection = null | {Object[]} | | iterable | {Object[]} | | | count | | @returns {number} | | countFrom | {Object,string,number} toFind | {number} | | find | {Object,string,number} toFind | {?Object} | | findParent | {Object,string,number} toFind | {?Object} | | findSiblings | {Object,string,number} toFind | {?Object} | | findAncestor | {Object,string,number} toFind | {?Object} | | update | {Object,string,number} toFind, {function} updater | {EntriesTree} | | updateUp | {Object,string,number} toFind, {function} updater | {EntriesTree} | | updateDown | {Object,string,number} toFind, {function} updater | {EntriesTree} | | delete | {Object,string,number} toFind | {?Object} | | insertAfter | {Object,string,number} toFind, {Object} toInsert | {EntriesTree} | | insertBefore | {Object,string,number} toFind, {Object} toInsert | {EntriesTree} |

Tests

⬆️ Top ➡️ Licence

Tests are made with ava

npm run test

Licence

⬆️ Top

MIT