README
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_nodewithnew_node.old_nodemust be a child node of the calling object.addChild(child[, prev]) - Insert
childinto linked list. Default to end of list. If an optionalprevnode is passed, and that node is a child of the calling object, thenchildwill be inserted afterprev.removeChild(child) - Removes
childfrom linked list if it is the calling object is the parent ofchild.getNextChild([child]) - Returns the next child in linked list after the given
child. Ifchildis not present, returns null. Ifchildis the last child node in the list, then null is returned.getChildAtIndex(index[, node]) - Returns node at the given
indexoffset. If the optionalnodeargument is given, returns the node that isindexnodes away fromnode.nodemust 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.