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

collectr

v1.0.4

Published

Deal with nested collections in JavaScript with NestedJS

Downloads

54

Readme

NestedJS

This library allows you to deal with deep nested arrays in JavaScript. Navigate into nested collections can be tricky, so NestedJS try to get it easier.

You can find a live demo of NestedJS here.

Getting started

Installation

Browser :

Download the latest version of NestedJS and load the file Nested.min.js (on build folder) on your HTML page :

    <script src="/path/to/Nested.min.js"></script>

NodeJS :

You just have to install ...

    # with NPM
    $ npm install nestedjs

    # with Yarn
    $ yarn add nestedjs

... and load the package on your script

const NestedJS = require('nestedjs')
// OR
import NestedJS from 'nestedjs'

Usage

Instanciation

Once package loaded, NestedJS must ne instanciate with a nested collection.

const collection = [...] // Nested data collection
const tree = new NestedJS(collection)

IMPORTANT : The property of every node's children if there are, is named children by default. You can change the name with the option children_key.

The created instance transform the collection and add several properties and methods to each collection's node :

Properties

  • __nodeid : this property is unique and allows you to retrieve a specific node
  • __parentid : tag the parent node id of a node if exists, null otherwise
  • __rootid : tag the root node id of a node if exists, null otherwise
  • __previd : tag the previous node id of a node if exists, null otherwise
  • __nextid : tag the next node id of a node if exists, null otherwise
  • __depth : tag the node depth (0 to n)

To know the added properties on each node you must debug your collection, or you can simply browse it with a recursive loop.

Methods

You can retrieve a node by his unique id :

    const node = tree.retrieveNode(/* __nodeid node unique id */)

This will returns a node which is an instance of the NestedJS's Node class. This class has several methods :

  • returns original properties of the node :
    node.properties
  • returns node unique id :
    node.getId()
  • returns node parent unique id :
    node.getParentId()
  • returns next node unique id :
    node.getNextId()
  • returns previous node unique id :
    node.getPreviousId()
  • check if node property exists :
    node.hasProperty(key)
  • returns node property if exists, defaultValue otherwise :
    node.getProperty(key, defaultValue)
  • set node's property :
    node.setProperty(key, value)
  • returns an array of previous nodes if exists, null otherwise :
    node.previousNodes()
  • returns previous node if exists, null otherwise :
    node.previousNode()
  • returns true if the node has a predecessor, false otherwise :
    node.hasPreviousNode()
  • returns an array of next nodes if exists, null otherwise :
    node.nextNodes()
  • returns next node if exists, null otherwise :
    node.nextNode()
  • returns true if the node has a successor, false otherwise :
    node.hasNextNode()
  • returns an array of siblings nodes if exists, null otherwise
    node.siblingsNodes()
  • returns an array of child nodes :
    node.childNodes()
  • returns node's first child if it has, null otherwise :
    node.firstChild()
  • returns node's last child if is has, null otherwise :
    node.lastChild()
  • returns node's child by index if it has, null otherwise :
    node.nthChild()
  • return true if the node has children, false otherwise :
    node.hasChildNodes()
  • returns parent node if exists, null otherwise :
    node.parentNode()
  • returns true if the node has an ancestor, false otherwise :
    node.hasParentNode()
  • returns root node if exists, null otherwise :
    node.rootNode()
  • returns true if the node has a root node, false otherwise :
    node.hasRootNode()
  • returns current node breadcrumb :
    node.breadcrumb()
  • returns NestedJS instance :
    node.getTree()
  • returns node depth :
    node.depth()

Each node original properties are preserved and are transferred as properties of the NestedJS's Node class.

You also retrieve one or several nodes by a key/value couple search :

const nodes = tree.retrieveNodesBy('name', 'lorem')