Need private packages and team management tools?Check out npm Teams »

@candlefw/ll

0.1.5 • Public • Published

CandleFW Linked List

This module provides essential method and property mixins to add linked list and basic tree traversal functionality to any object constructor. It utilize doubly linked circular lists underneath the hood.

Install

NPM

npm install --save @candlefw/ll

Usage

note: This script uses ES2015 module syntax, and has the extension .mjs. To include this script in a project, you may need to use the node flag --experimental-modules; or, use a bundler that supports ES modules, such as rollup.

To add linked list properties to an object constructor:

import ll from "@candlefw/ll"
 
class Foo{};
 
ll.mixin(Foo);
 
let fooA = new Foo();
let fooB = new Foo();
 
fooA.next = fooB
 
fooB.previous //=> fooA

Or with additional parent/child properties and methods.

import ll from "@candlefw/ll"
 
function Bar();
 
ll.mixinTree(Bar);
 
let barA = new Bar();
let barB = new Bar();
 
barB.parent = barB;
 
barB.children // => [barB];
 

Added Components

Properties

Provided by ll.mixin

  • nxt - Points to next node or the object itself.

  • prv - Points to previous node or the object itself.

Provided by ll.mixinTree (includes above properties)

  • noc - The number of child nodes.

  • fch - Points to first child node or null.

  • par - Points to parent node or null.

Methods:

Provided by ll.mixin

  • insertBefore(node) - Inserts given node before the calling node in the linked list.

  • insertAfter(node) - Inserts given node after the calling node in the linked list.

Provided by ll.mixinTree (includes above methods)

  • eve() - Returns the root node of the tree.

  • unshift(node) - Inserts node as the head of the list.

  • push(node) - Inserts node at the tail of the list.

  • replace(old_node, new_node) - Replaces old_node with new_node. old_node must be a child node of the calling object.

  • addChild(child[, prev]) - Insert child into linked list. Default to end of list. If an optional prev node is passed, and that node is a child of the calling object, then child will be inserted after prev.

  • removeChild(child) - Removes child from linked list if it is the calling object is the parent of child.

  • getNextChild([child]) - Returns the next child in linked list after the given child. If child is not present, returns null. If child is the last child node in the list, then null is returned.

  • getChildAtIndex(index[, node]) - Returns node at the given index offset. If the optional node argument is given, returns the node that is index nodes away from node. node must be a child of the calling object, otherwise it is ignored.

Virtual Properties (setters and getters):

Provided by ll.mixin

  • next - Returns nxt. Assigning a node to this property will insert that node into the list after the assignee node.

  • previous - Returns prv. Assigning a node to this property will insert that node into the list before the assignee node.

Provided by ll.mixinTree (includes above virtual properties)

  • parent - Returns the value of par. Assigning a node to this property will cause the assignee node to become a child of the assigned node.

  • children (Read-Only) - Returns an array of child nodes.

Keywords

Install

npm i @candlefw/ll

DownloadsWeekly Downloads

1

Version

0.1.5

License

MIT

Unpacked Size

20 kB

Total Files

5

Last publish

Collaborators

  • avatar