ts-linked-list
Advanced tools
Comparing version 1.0.0 to 1.0.1
import LinkedListNode from './LinkedListNode'; | ||
/** Type used for filter and find methods, returning a boolean */ | ||
declare type TTestFunction<NodeData> = (data: NodeData, index: number, list: LinkedList<NodeData>) => boolean; | ||
/** Type used for map and forEach methods, returning anything */ | ||
declare type TMapFunction<NodeData> = (data: any, index: number, list: LinkedList<NodeData>) => any; | ||
/** | ||
* A doubly linked list | ||
* ```javascript | ||
* const list = new LinkedList(1, 2); | ||
* list.append(3); | ||
* list.prepend(0); | ||
* list.forEach(data => console.log(data)); | ||
* // 0 1 2 3 | ||
* list.head.remove(); | ||
* console.log(list.toArray()); | ||
* // [1, 2, 3] | ||
* ```ts | ||
* const list = new LinkedList(1, 2, 3); | ||
* const listFromArray = LinkedList.from([1, 2, 3]); | ||
* ``` | ||
@@ -20,11 +16,11 @@ */ | ||
* The length of the list | ||
* @returns The length of the list | ||
*/ | ||
readonly length: number; | ||
/** | ||
* Convert an array to a new linked list | ||
* Convert any iterable to a new linked list | ||
* ```javascript | ||
* const list = LinkedList.from([1, 2, 3]); | ||
* const array = [1, 2, 3]; | ||
* const list = LinkedList.from(array); | ||
* ``` | ||
* @param iterable Any iterable datatype like an Array or a Map | ||
* @param iterable Any iterable datatype like Array or Map | ||
*/ | ||
@@ -42,7 +38,5 @@ static from<T>(iterable: Iterable<T>): LinkedList<T>; | ||
* ```ts | ||
* const list = new LinkedList(1, 2, 3); | ||
* list.get(2); // 3 | ||
* new LinkedList(1, 2, 3).get(0); // 1 | ||
* ``` | ||
* @param index to retrieve data at | ||
* @returns Data or undefined | ||
*/ | ||
@@ -52,4 +46,6 @@ get(index: number): NodeData | undefined; | ||
* Get the node at index, zero based | ||
* @param index to retrieve the node at | ||
* @returns The node or undefined | ||
* ```ts | ||
* new LinkedList(1, 2, 3).getNode(0); | ||
* // { prev: null, data: 1, next: LinkedListNode } | ||
* ``` | ||
*/ | ||
@@ -60,4 +56,7 @@ getNode(index: number): LinkedListNode<NodeData> | undefined; | ||
* satisfies the testing function | ||
* @param f A function to be applied to each nodes data | ||
* @returns A new object containing the node and the index or undefined | ||
* ```ts | ||
* new LinkedList(1, 2, 3).findNodeIndex(data => data === 1); | ||
* // { node: LinkedListNode, index: 0 } | ||
* ``` | ||
* @param f A function to be applied to the data of each node | ||
*/ | ||
@@ -71,4 +70,7 @@ findNodeIndex(f: TTestFunction<NodeData>): ({ | ||
* satisfies the provided testing function. Otherwise undefined is returned. | ||
* ```ts | ||
* new LinkedList(1, 2, 3).findNode(data => data === 1); | ||
* // { prev: null, data: 1, next: LinkedListNode } | ||
* ``` | ||
* @param f Function to test data against | ||
* @returns The matching node or undefined | ||
*/ | ||
@@ -79,4 +81,6 @@ findNode(f: TTestFunction<NodeData>): LinkedListNode<NodeData> | undefined; | ||
* satisfies the provided testing function. Otherwise undefined is returned. | ||
* ```ts | ||
* new LinkedList(1, 2, 3).find(data => data === 1); // 1 | ||
* ``` | ||
* @param f Function to test data against | ||
* @returns The matching index or undefined | ||
*/ | ||
@@ -87,10 +91,16 @@ find(f: TTestFunction<NodeData>): NodeData | undefined; | ||
* satisfies the provided testing function. Ohterwise -1 is returned. | ||
* ```ts | ||
* new LinkedList(1, 2, 3).findIndex(data => data === 3); // 2 | ||
* ``` | ||
* @param f Function to test data against | ||
* @returns The matching index or -1 | ||
*/ | ||
findIndex(f: TTestFunction<NodeData>): number; | ||
/** | ||
* Append a node to the end of the list | ||
* Append one or any number of nodes to the end of the list. | ||
* This modifies the list in place and returns the list itself | ||
* to make this method chainable. | ||
* ```ts | ||
* new LinkedList(1).append(2).append(3, 4); // 1 <=> 2 <=> 3 <=> 4 | ||
* ``` | ||
* @param data Data to be stored in the node, takes any number of arguments | ||
* @returns The list which was appended to | ||
*/ | ||
@@ -100,4 +110,6 @@ append(...args: NodeData[]): LinkedList<NodeData>; | ||
* Synonym for append | ||
* ```ts | ||
* new LinkedList(1).push(2).push(3, 4); // 1 <=> 2 <=> 3 <=> 4 | ||
* ``` | ||
* @param data Data to be stored, takes any number of arguments | ||
* @returns The list which was appended to | ||
*/ | ||
@@ -107,9 +119,7 @@ push(...args: NodeData[]): number; | ||
* Prepend any number of data arguments to the list. The | ||
* argument list is prepended in reverse order to make it more logical: | ||
* argument list is prepended as a block to reduce confusion: | ||
* ```javascript | ||
* const list = new LinkedList(3, 4); | ||
* list.prepend(0, 1, 2).toArray(); // => [0, 1, 2, 3, 4] | ||
* new LinkedList(3, 4).prepend(0, 1, 2); // [0, 1, 2, 3, 4] | ||
* ``` | ||
* @param data Data to be stored in the node, accepts any number of arguments | ||
* @returns The list which was prepended to | ||
*/ | ||
@@ -121,11 +131,17 @@ prepend(...args: NodeData[]): LinkedList<NodeData>; | ||
* or 0, it will be prepended. | ||
* ```ts | ||
* new LinkedList(1, 3).insertAt(1, 2); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param index The index to insert the new node at | ||
* @param data Data to be stored on the new node | ||
* @returns The list which was inserted to | ||
*/ | ||
insertAt(index: number, data: NodeData): LinkedList<NodeData>; | ||
/** | ||
* Remove the specified node from the list | ||
* Remove the specified node from the list and return the removed | ||
* node afterwards. | ||
* ```ts | ||
* const list = new LinkedList(1, 2, 3); | ||
* list.removeNode(list.tail); // { prev: null, data: 3, next: null, list: null } | ||
* ``` | ||
* @param node The node to be removed | ||
* @returns The removed node | ||
*/ | ||
@@ -135,4 +151,6 @@ removeNode(node: LinkedListNode<NodeData>): LinkedListNode<NodeData>; | ||
* Remove the node at the specified index | ||
* ```ts | ||
* new LinkedList(1, 2, 3).removeAt(2); // { prev: null, data: 3, next: null, list: null } | ||
* ``` | ||
* @param index Index at which to remove | ||
* @returns The removed node or undefined | ||
*/ | ||
@@ -142,31 +160,65 @@ removeAt(index: number): LinkedListNode<NodeData> | undefined; | ||
* Insert a new node before the reference node | ||
* ```ts | ||
* const list = new LinkedList(1, 3); | ||
* list.insertBefore(list.tail, 2); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param referenceNode The node reference | ||
* @param data Data to save in the node | ||
* @returns The list which was inserted to | ||
*/ | ||
insertBefore(referenceNode: LinkedListNode<NodeData>, data: NodeData): LinkedList<NodeData>; | ||
/** | ||
* Sorts the linked list using the provided compare function | ||
* @param compare A function used to compare the data of two nodes. It should return | ||
* a boolean. True will insert a before b, false will insert b before a. | ||
* (a, b) => a < b or (1, 2) => 1 < 2 === true, 2 will be inserted after 1, | ||
* the sort order will be ascending. | ||
*/ | ||
sort(compare: (a: NodeData, b: NodeData) => boolean): LinkedList<NodeData>; | ||
/** | ||
* Insert a new node after this one | ||
* ```ts | ||
* const list = new LinkedList(2, 3); | ||
* list.insertAfter(list.head, 1); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param referenceNode The reference node | ||
* @param data Data to be saved in the node | ||
* @returns This list | ||
*/ | ||
insertAfter(referenceNode: LinkedListNode<NodeData>, data: NodeData): LinkedList<NodeData>; | ||
/** | ||
* Remove the first node from the list | ||
* @returns The data of the removed node or undefined if the list was empty | ||
* Remove the first node from the list and return the data of the removed node | ||
* or undefined | ||
* ```ts | ||
* new LinkedList(1, 2, 3).shift(); // 1 | ||
* ``` | ||
*/ | ||
shift(): NodeData | undefined; | ||
/** | ||
* Remove the last node from the list | ||
* @returns The data of the removed node or undefined if the list was empty | ||
* Remove the last node from the list and return the data of the removed node | ||
* or undefined if the list was empty | ||
* ```ts | ||
* new LinkedList(1, 2, 3).pop(); // 3 | ||
* ``` | ||
*/ | ||
pop(): NodeData | undefined; | ||
/** | ||
* Concatenate the current list with another | ||
* @param list The list to be linked | ||
* @returns This list | ||
* Merge the current list with another. Both lists will be | ||
* equal after merging. | ||
* ```ts | ||
* const list = new LinkedList(1, 2); | ||
* const otherList = new LinkedList(3); | ||
* list.merge(otherList); | ||
* (list === otherList); // true | ||
* ``` | ||
* @param list The list to be merged | ||
*/ | ||
concat(list: LinkedList<NodeData>): LinkedList<NodeData>; | ||
merge(list: LinkedList<NodeData>): void; | ||
/** | ||
* Removes all nodes from a list | ||
* | ||
* ```ts | ||
* list.clear(); | ||
* ``` | ||
*/ | ||
clear(): this; | ||
/** | ||
* The slice() method returns a shallow copy of a | ||
@@ -176,5 +228,8 @@ * portion of a list into a new list object selected | ||
* The original list will not be modified. | ||
* ```ts | ||
* const list = new LinkedList(1, 2, 3, 4, 5); | ||
* const newList = list.slice(0, 3); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param start Start index | ||
* @param end End index, optional | ||
* @returns The newly sliced list | ||
*/ | ||
@@ -185,3 +240,5 @@ slice(start: number, end?: number): LinkedList<NodeData | {}>; | ||
* itself. | ||
* @returns The reversed list | ||
* ```ts | ||
* new LinkedList(1, 2, 3).reverse(); // 3 <=> 2 <=> 1 | ||
* ``` | ||
*/ | ||
@@ -191,2 +248,5 @@ reverse(): LinkedList<NodeData>; | ||
* The forEach() method executes a provided function once for each list node. | ||
* ```ts | ||
* new LinkedList(1, 2, 3).forEach(data => log(data)); // 1 2 3 | ||
* ``` | ||
* @param f Function to execute for each element, taking up to three arguments. | ||
@@ -199,5 +259,7 @@ * @param reverse Indicates if the list should be walked in reverse order, default is false | ||
* calling a provided function on every node in the calling list. | ||
* ```ts | ||
* new LinkedList(1, 2, 3).map(data => data + 10); // 11 <=> 12 <=> 13 | ||
* ``` | ||
* @param f Function that produces an node of the new list, taking up to three arguments | ||
* @param reverse Indicates if the list should be mapped in reverse order, default is false | ||
* @returns A new LinkedList | ||
*/ | ||
@@ -208,5 +270,7 @@ map(f: TMapFunction<NodeData>, reverse?: boolean): LinkedList<NodeData | {}>; | ||
* that pass the test implemented by the provided function. | ||
* ```ts | ||
* new LinkedList(1, 2, 3, 4, 5).filter(data => data < 4); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param f Function to test each node data in the list. Return true to keep the node | ||
* @param reverse Indicates if the list should be filtered in reverse order, default is false | ||
* @returns A new linked list | ||
*/ | ||
@@ -216,2 +280,5 @@ filter(f: TTestFunction<NodeData>, reverse?: boolean): LinkedList<NodeData | {}>; | ||
* Reduce over each node in the list | ||
* ```ts | ||
* new LinkedList(1, 2, 3).reduce(n => n += 1, 0); // 3 | ||
* ``` | ||
* @param f A reducer function | ||
@@ -224,3 +291,5 @@ * @param start An initial value | ||
* Convert the linked list to an array | ||
* @returns An array containing all the data from the LinkedList | ||
* ```ts | ||
* new LinkedList(1, 2, 3).toArray(); // [1, 2, 3] | ||
* ``` | ||
*/ | ||
@@ -238,2 +307,6 @@ toArray(): NodeData[]; | ||
* The iterator implementation | ||
* ```ts | ||
* const list = new LinkedList(1, 2, 3); | ||
* for (const data of list) { log(data); } // 1 2 3 | ||
* ``` | ||
*/ | ||
@@ -240,0 +313,0 @@ [Symbol.iterator](): IterableIterator<NodeData>; |
import LinkedList from './LinkedList'; | ||
/** | ||
* The class which represents one link or node in a linked list | ||
* ```ts | ||
* const node = new LinkedListNode(1, null, null, null); | ||
* ``` | ||
*/ | ||
export default class LinkedListNode<NodeData = any> { | ||
/** Data stored on the node */ | ||
data: NodeData; | ||
/** The previous node in the list */ | ||
prev: LinkedListNode<NodeData> | null; | ||
/** The next link in the list */ | ||
next: LinkedListNode<NodeData> | null; | ||
/** The list this node belongs to */ | ||
list: LinkedList<NodeData> | null; | ||
constructor(data: NodeData, prev: LinkedListNode<NodeData> | null, next: LinkedListNode<NodeData> | null, list: LinkedList<NodeData> | null); | ||
constructor( | ||
/** Data stored on the node */ | ||
data: NodeData, | ||
/** The previous node in the list */ | ||
prev: LinkedListNode<NodeData> | null, | ||
/** The next link in the list */ | ||
next: LinkedListNode<NodeData> | null, | ||
/** The list this node belongs to */ | ||
list: LinkedList<NodeData> | null); | ||
/** | ||
* Alias to .data | ||
* ```ts | ||
* new LinkedList(1, 2, 3).head.value; // 1 | ||
* ``` | ||
*/ | ||
readonly value: NodeData; | ||
/** | ||
* Get the index of this node | ||
* ```ts | ||
* new LinkedList(1, 2, 3).head.index; // 0 | ||
* ``` | ||
*/ | ||
readonly index: number | undefined; | ||
/** | ||
* Insert a new node before this one | ||
* ```ts | ||
* new LinkedList(2, 3).head.insertBefore(1); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param data Data to save in the node | ||
* @returns The list the data was inserted to | ||
*/ | ||
insertBefore(data: any): LinkedList<NodeData>; | ||
insertBefore(data: NodeData): LinkedList<NodeData>; | ||
/** | ||
* Insert a new node after this one | ||
* Insert new data after this node | ||
* ```ts | ||
* new LinkedList(1, 2).tail.insertAfter(3); // 1 <=> 2 <=> 3 | ||
* ``` | ||
* @param data Data to be saved in the node | ||
* @returns The list the data was inserted to | ||
*/ | ||
insertAfter(data: any): LinkedList<NodeData>; | ||
insertAfter(data: NodeData): LinkedList<NodeData>; | ||
/** | ||
* Remove this node | ||
* @returns {LinkedList} the list without this node | ||
* ```ts | ||
* new LinkedList(1, 2, 3, 4).tail.remove(); // 1 <=> 2 <=> 3 | ||
* ``` | ||
*/ | ||
remove(): LinkedListNode<NodeData>; | ||
} |
{ | ||
"name": "ts-linked-list", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A doubly linked list written in TypeScript", | ||
@@ -18,4 +18,3 @@ "keywords": [ | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.es.js", | ||
"main": "dist/LinkedList.js", | ||
"files": [ | ||
@@ -29,8 +28,6 @@ "dist" | ||
"scripts": { | ||
"build": "rollup -c", | ||
"dev": "rollup -cw", | ||
"build": "tsc", | ||
"lint": "tslint --project \".\"", | ||
"test": "jest --coverage", | ||
"test:watch": "jest --watchAll", | ||
"doc": "typedoc --theme minimal --target es6 --mode file --out ./doc ./src", | ||
"doc": "typedoc --theme minimal --target es5 --mode file --out ./doc ./src", | ||
"preversion": "yarn test", | ||
@@ -41,16 +38,9 @@ "postversion": "git push --tags && npm publish", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.11", | ||
"camelcase": "^5.0.0", | ||
"jest": "^23.6.0", | ||
"rollup": "^1.0.0", | ||
"rollup-plugin-commonjs": "^9.2.0", | ||
"rollup-plugin-node-resolve": "^4.0.0", | ||
"rollup-plugin-sourcemaps": "^0.4.2", | ||
"rollup-plugin-typescript2": "^0.19.0", | ||
"rollup-plugin-uglify": "^6.0.1", | ||
"ts-jest": "^23.10.5", | ||
"tslint": "^5.12.0", | ||
"typedoc": "^0.14.1", | ||
"typescript": "^3.2.2" | ||
"@types/jest": "^24.0.13", | ||
"jest": "^24.8.0", | ||
"ts-jest": "^24.0.2", | ||
"tslint": "^5.17.0", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.5.1" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
6
0
48181
1148
1