@humanwhocodes/doubly-linked-list
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -411,4 +411,43 @@ /** | ||
let current = this[head]; | ||
/* | ||
* 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; | ||
} | ||
/* | ||
* 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; | ||
} | ||
/** | ||
* Returns the index of 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 {int} The index of the first item that matches a given function | ||
* or -1 if there are no matching items. | ||
*/ | ||
findIndex(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 | ||
@@ -422,3 +461,3 @@ * gone. This is important because this is the value that is returned | ||
* 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 | ||
* If a match is found, it returns the index immediately, exiting the | ||
* loop because there's no reason to keep searching. The search | ||
@@ -429,3 +468,3 @@ * continues until there are no more nodes to search (when `current` is `null`). | ||
if (matcher(current.data)) { | ||
return current.data; | ||
return index; | ||
} | ||
@@ -442,6 +481,6 @@ | ||
* 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 | ||
* list and didn't find `data`. Just return -1 as the | ||
* "not found" value. | ||
*/ | ||
return undefined; | ||
return -1; | ||
} | ||
@@ -448,0 +487,0 @@ |
{ | ||
"name": "@humanwhocodes/doubly-linked-list", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A doubly linked list implementation in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "doubly-linked-list.js", |
@@ -14,3 +14,4 @@ # JavaScript Doubly Linked List Class | ||
1. Defining a `values()` generator method. | ||
1. Defining a `find()` method for searching the list. | ||
1. Defining a `find()` method for searching the list to return data. | ||
1. Defining a `findIndex()` method for searching the list to return an index. | ||
1. Returning `undefined` from `get()` when no such index exists. | ||
@@ -51,2 +52,3 @@ | ||
let result = list.find(value => value.length > 3); | ||
let foundIndex = list.findIndex(value => value.length > 3); | ||
@@ -53,0 +55,0 @@ // convert to an array using iterators |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25987
593
77