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

nestedsetsjs

v1.0.5

Published

Nested sets structure with separated items and nodes

Downloads

17

Readme

nestedsetsjs

NPM Version GitHub file size in bytes npm License

Nested sets structure with separated items and nodes.

Source here Armasheko Artem / nestedsetsjs.

:evergreen_tree:

Instalation

npm install nestedsetsjs

Usage

var ns        = require('nestedsetsjs')
var NestedSet = ns();

NestedSet.setItem(1, {title: "Root"})
NestedSet.setItem(2, {title: "Type"})
NestedSet.setItem(3, {title: "Type2"})
NestedSet.setItem(4, {title: "Group"})
NestedSet.setItem(5, {title: "Group2"})
NestedSet.setItem(6, {title: "SubGroup"})

var root_node_id = NestedSet.addRoot(1)
var type_node_id = NestedSet.addNode(root_node_id, 2)
var type_node2_id = NestedSet.addNode(root_node_id, 3)
var group_node_id = NestedSet.addNode(type_node_id, 4)
var group_node2_id = NestedSet.addNode(type_node2_id, 5)
var subgroup_node_id = NestedSet.addNode(group_node_id, 6)
var subgroup_node_id2 = NestedSet.addNode(group_node2_id, 6)

console.log(NestedSet.debug())

Methods

setItem(id, data)

Sets data for an item

  • id - ID item
  • data - Stored data
    Returns Stored data in Data propert
NestedSet.setItem(1, { title: "Root"})

removeItem(id)

Removes an item with its dependent nodes

  • id - ID item
    Returns TRUE if item exists or FALSE if not exists
NestedSet.removeItem(1)

addRoot(item_id)

Initializes the Nested Sets structure. Adds a root node and sets an item for it

  • item_id - item ID
    Returns node ID = 1
NestedSet.addRoot(1)

addNode(target_node_id, item_id)

Adds a new node to the specified parent node and sets an item for it

  • target_node_id - parent node ID
  • item_id - item ID
    Returns node ID
NestedSet.addNode(1, 1)

getNode(node_id, asCopy)

Gets a node by ID

  • node_id - node ID
  • asCopy - true (will return a copy of the object) / false or undefined (will return the object itself)
    Returns node object
NestedSet.getNode(1)

removeNode(node_id)

Removes a node by its ID

  • node_id - node ID
    Returns new Structure
NestedSet.removeNode(1)

getNodes ()

Returns all nodes (Structure property)

removeNodes()

Removes all nodes (items are not destroyed)

getParent(node_id)

Returns the first parent for the node

  • node_id - node ID
NestedSet.getParent(5)

getParents(node_id)

Returns all parents of the node

  • node_id - node ID
NestedSet.getParents(5)

getChilds(node_id, depth)

Returns nested nodes in a node

  • node_id - node ID
  • depth - nesting depth
NestedSet.getChilds(1, 1)

getBranch(node_id)

Returns the entire branch that contains the node

  • node_id - node ID
NestedSet.getBranch(5)

getTree()

Returns the entire structure (nodes and items)

clearAll()

Removes all items and all nodes

isRoot(node_id)

Checks if a node is root

  • node_id - node ID

isBranch(node_id)

Checks if a node is a parent

  • node_id - node ID

isLeaf(node_id)

Checks if a node is the end of a branch

  • node_id - node ID

getMaxRightKey

Returns MAX right key in the tree

getMaxLeftKey

Returns MAX left key in the tree

getCountNodes

Returns count nodes of tree

checkTree()

Checks a tree according to three rules

  1. The left node key is always less than the right key
  2. The remainder of the division of the difference between the right and left keys must be 0
  3. The remainder of dividing the difference between the left key and the nesting depth + 2 must be 0

debug()

Displays information about nodes and items

Properties

  • Data (Items)
  • Structure (Nodes)

Structure

[
  {
    _id: 1, // ID node
    lkey: 1, // left key
    rkey: 14, // right key
    depth: 0, // depth
    childs: 2, // count childs
    parent_id: 0, // ID parent node
    item_id: 1, // ID item from property Data
    data: { title: 'Root' } // item with ID item_id from property Data
  },
  ...
]

Returns this scheme for methods

  • getParent
  • getParents
  • getChilds
  • getBranch
  • getTree

Tests

npm install
npm test

ESLint

npm install
npx eslint ./src/nestedsets.js
npx eslint ./test/test.js