Skip to content

Commit

Permalink
feat: add LinkedList find / toString convenience methods (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Apr 9, 2024
1 parent 84bea0a commit 98efeea
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/utils/datastructures/LinkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class LinkedList<T> {
this.linkToTail(entity);
}

find(predicate: (entity: T) => boolean): T | null {
for (const entity of this) {
if (predicate(entity)) {
return entity;
}
}
return null;
}

isLinked(entity: T) {
return this.linkFor(entity).isLinked;
}
Expand Down Expand Up @@ -98,6 +107,14 @@ class LinkedList<T> {
}
}

toString(callback: (entity: T) => string) {
const result = [];
for (const entity of this) {
result.push(callback(entity));
}
return `[${result.join(', ')}]`;
}

unlink(entity: T) {
return this.linkFor(entity).unlink();
}
Expand Down

0 comments on commit 98efeea

Please sign in to comment.