GithubHelp home page GithubHelp logo

betaweb / nestedjs Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 13.06 MB

Deal with deep nested arrays in JavaScript

JavaScript 50.68% CSS 21.36% HTML 3.03% Vue 24.93%
javascript javascript-library nodejs nested nested-structures array-manipulations javascript-array collectr

nestedjs's Introduction

NestedJS

Build Status

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')

nestedjs's People

Contributors

betaweb avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

c57fr

nestedjs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.