Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find method #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ Default export, the class that holds and manages a list.
Call it with either a forEach-able (like an array) or a set of
arguments, to initialize the list.

The Array-ish methods all act like you'd expect. No magic length,
The Array-ish methods all act like you'd expect. No magic length,
though, so if you change that it won't automatically prune or add
empty spots.

### Yallist.create(..)

Alias for Yallist function. Some people like factories.
Alias for Yallist function. Some people like factories.

#### yallist.head

Expand All @@ -73,7 +73,7 @@ The last node in the list

#### yallist.length

The number of nodes in the list. (Change this at your peril. It is
The number of nodes in the list. (Change this at your peril. It is
not magic like Array length.)

#### yallist.toArray()
Expand All @@ -90,7 +90,7 @@ Call a function on each item in the list, in reverse order.

#### yallist.get(n)

Get the data at position `n` in the list. If you use this a lot,
Get the data at position `n` in the list. If you use this a lot,
probably better off just using an Array.

#### yallist.getReverse(n)
Expand Down Expand Up @@ -156,15 +156,15 @@ Insert one or more items to the head of the list.

#### yallist.unshiftNode(node)

Move a Node object to the front of the list. (That is, pull it out of
Move a Node object to the front of the list. (That is, pull it out of
wherever it lives, and make it the new head.)

If the node belongs to a different list, then that list will remove it
first.

#### yallist.pushNode(node)

Move a Node object to the end of the list. (That is, pull it out of
Move a Node object to the end of the list. (That is, pull it out of
wherever it lives, and make it the new tail.)

If the node belongs to a list already, then that list will remove it
Expand All @@ -178,14 +178,18 @@ and tail and other nodes.
Will throw an error if you try to have a list remove a node that
doesn't belong to it.

#### yallist.find(predicate)

Like Array.find.

### Yallist.Node

The class that holds the data and is actually the list.

Call with `const n = new Node(value, previousNode, nextNode)`

Note that if you do direct operations on Nodes themselves, it's very
easy to get into weird states where the list is broken. Be careful :)
easy to get into weird states where the list is broken. Be careful :)

#### node.next

Expand All @@ -201,5 +205,5 @@ The data the node contains.

#### node.list

The list to which this node belongs. (Null if it does not belong to
The list to which this node belongs. (Null if it does not belong to
any list.)
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yallist",
"version": "5.0.0",
"version": "5.1.0",
"description": "Yet Another Linked List",
"files": [
"dist"
Expand Down
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ export class Yallist<T = unknown> {
this.tail = head
return this
}

find(predicate: (value: T, index: number) => boolean) {
let walker = this.head
let index = 0

while (walker !== undefined) {
if (predicate(walker.value, index)) {
return walker
}

walker = walker.next
index++
}

return undefined
}
}

// insertAfter undefined means "make the node the new head of list"
Expand Down
12 changes: 12 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,15 @@ t.test('splice bug', t => {
t.same(myList.toArray(), [1, 'should be second item', 2, 3])
t.end()
})

t.test('find', t => {
const ll = new Yallist([1, 2, 3, 4, 5])

const foundNode = ll.find(value => value === 3)
t.equal(foundNode?.value, 3)

const notFoundNode = ll.find(value => value === 6)
t.equal(notFoundNode, undefined)

t.end()
})