diff --git a/README.md b/README.md index 68f6162..3358a4c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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() @@ -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) @@ -156,7 +156,7 @@ 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 @@ -164,7 +164,7 @@ 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 @@ -178,6 +178,10 @@ 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. @@ -185,7 +189,7 @@ 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 @@ -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.) diff --git a/package-lock.json b/package-lock.json index d76b908..50e4ce6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "yallist", - "version": "5.0.0", + "version": "5.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "yallist", - "version": "5.0.0", + "version": "5.1.0", "license": "BlueOak-1.0.0", "devDependencies": { "prettier": "^3.2.5", diff --git a/package.json b/package.json index 2f52478..6c79b9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yallist", - "version": "5.0.0", + "version": "5.1.0", "description": "Yet Another Linked List", "files": [ "dist" diff --git a/src/index.ts b/src/index.ts index 0152c5e..438dcda 100644 --- a/src/index.ts +++ b/src/index.ts @@ -397,6 +397,22 @@ export class Yallist { 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" diff --git a/test/basic.js b/test/basic.js index 5ee3864..8430629 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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() +})