Skip to content
betaWebPublic

About

Deal with deep nested arrays in goddamn JavaScript

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Latest commit

 

History

60 Commits

Folders and files

Repository files navigation

NestedJS

Build Status

Listen up: 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 :

Listen up: Download the latest version of NestedJS and load the muthaf*ckin' 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 f*ckin' script

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

Usage

Instanciation

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

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

IMPORTANT : The property of every f*ckin' 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 goddamn collection and add several properties and methods to each goddamn collection's node :

Properties

  • __nodeid : this property is unique and allows you to retrieve a goddamn specific node
  • __parentid : tag the parent node id of a node if exists, null otherwise
  • __rootid : tag the f*ckin' root node id of a node if exists, null otherwise
  • __previd : tag the previous node id of a goddamn 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 motherf*ckin' recursive loop. That's some bullsh*t.

Methods

You can retrieve a muthaf*ckin' node by his unique id :

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

This will returns a motherf*ckin' node which is an instance of the bad-ass NestedJS's Node class. This motherf*ckin' class has several methods :

  • returns original properties of the muthaf*ckin' 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 f*ckin' next nodes if exists, null otherwise :
    node.nextNodes()
  • returns next node if exists, null otherwise :
    node.nextNode()
  • returns true if the damn node has a successor, false otherwise :
    node.hasNextNode()
  • returns an array of muthaf*ckin' siblings nodes if exists, null otherwise
    node.siblingsNodes()
  • returns an array of goddamn 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 f*ckin' 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 motherf*ckin' 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')

About

Deal with deep nested arrays in goddamn JavaScript

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages