@humanwhocodes/doubly-linked-list
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -395,3 +395,52 @@ /** | ||
} | ||
/** | ||
* Returns the first item that matches a given function. | ||
* @param {Function} matcher A function returning true when an item matches | ||
* and false when an item doesn't match. | ||
* @returns {*} The first item that returns true from the matcher, undefined | ||
* if no items match. | ||
*/ | ||
find(matcher) { | ||
/* | ||
* The `current` variable is used to iterate over the list nodes. | ||
* It starts out pointing to the head and is overwritten inside | ||
* of the loop below. | ||
*/ | ||
let current = this[head]; | ||
/* | ||
* The `index` variable is used to track how deep into the list we've | ||
* gone. This is important because this is the value that is returned | ||
* from this method. | ||
*/ | ||
let index = 0; | ||
/* | ||
* This loop checks each node in the list to see if it matches. | ||
* If a match is found, it returns the data immediately, exiting the | ||
* loop because there's no reason to keep searching. The search | ||
* continues until there are no more nodes to search (when `current` is `null`). | ||
*/ | ||
while (current !== null) { | ||
if (matcher(current.data)) { | ||
return current.data; | ||
} | ||
// traverse to the next node in the list | ||
current = current.next; | ||
// keep track of where we are | ||
index++; | ||
} | ||
/* | ||
* If execution gets to this point, it means we reached the end of the | ||
* list and didn't find `data`. Just return `undefined` as the | ||
* "not found" value. | ||
*/ | ||
return undefined; | ||
} | ||
/** | ||
@@ -398,0 +447,0 @@ * Removes the node from the given location in the list. |
{ | ||
"name": "@humanwhocodes/doubly-linked-list", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "A doubly linked list implementation in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "doubly-linked-list.js", |
@@ -14,4 +14,7 @@ # JavaScript Doubly Linked List Class | ||
1. Defining a `values()` generator method. | ||
1. Defining a `find()` method for searching the list. | ||
1. Returning `undefined` from `get()` when no such index exists. | ||
Read the [blog post](https://humanwhocodes.com/blog/2019/02/computer-science-in-javascript-doubly-linked-lists/) about the design of this class. | ||
## Usage | ||
@@ -46,2 +49,5 @@ | ||
// search for a value | ||
let result = list.find(value => value.length > 3); | ||
// convert to an array using iterators | ||
@@ -48,0 +54,0 @@ let array1 = [...list.values()]; |
24432
559
75