DLL(doubly linked list) library for javascript projects
npm install @akashbabu/node-dllDLL schema.This library is written in typescript for robust APIs and type support.
Every aspect was desgined and thought carefully to minimize introducing new bugs.
> npm i @akashbabu/node-dll -S
``TS
import {DLL, DLLItem} from '@akashbabu/node-dll';
const dll = new DLL
const dllItem: DLLItem
console.log(dllItem.data) // => foo
console.log(dll.length) // => 1
dll.remove(dllItem)
console.log(dll.length) // => 0
dll.push('foo')
dll.unshift('bar')
const headItem = dll.head
console.log(headItem.data) // => bar
const headData = dll.shift()
console.log(headData) // => bar
console.log(dll.head.data) // => foo
dll.clear()
console.log(dll.length) // => 0
const firstItem = dll.push('first')
dll.push('third')
dll.appendAfter(firstItem, 'second')
dll.forEach((item, i) => {
console.log(${i + 1}) ${item});
})
// => 1) first
// => 2) second
// => 3) third
console.log(dll.map((item, i) => ${i + 1}) ${item}));
// => ["1) first", "2) second", "3) third"]
`
#### new DLL
This creates a new instance of DLL.
T -> Denotes the type of data being saved in DLL
##### .head: DLLItem
Returns the first item in the list
##### .tail: DLLItem
Returns the last item in the list
##### .length: number
Returns the length of the list
##### .shift(): T | undefined
Removes and returns the first item in the list. Returns undefined if the list is empty
##### .unshift(data: T): void
Adds the given item to the head of DLL (same as Array.unshift logic)
##### .forEach(cb: (data: T, i: number) => void): void
Iterates through the entire DLL
##### .map(cb: (data: T, i: number) => U): U[]
Iterates through the entire DLL and returns the resultant array.
U -> Denotes the return type of cb(callback)
##### .push(data: T): DLLItem
Adds the given item to the tail of DLL and returns the added item
##### .appendAfter(dllItem: DLLItem
Adds the given data after the given dllItem in DLL and returns the added item
##### .remove(dllItem: DLLItem
Removes the given item from DLL and returns true if the removal was successful
##### .clear()
Removes all the items in the DLL
and next` properties, this is to ensure that the users doesn't change them unintentionally (which can mess up with the entire DLL) and hence less surface for bugs.##### .data
Set / get data value on the DLLItem via this property
##### .prev
Get prev value on the DLLItem via this property
##### .next
Get next value on the DLLItem via this property
All contributions are welcome!!!
But before raising any issue, please check the issue-tracker in github if there is any matching issues already in the pipeline, if not then please go ahead and raise your own.
PR Guidelines:
- Make sure to include corresponding test cases
- Be generous on code comments
- Write documentation if necessary
- Wait for your approval 😜
MIT