Skip to content

Commit

Permalink
add clear function
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Apr 21, 2024
1 parent 8502b12 commit a56ed06
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# Upcoming

- Performance: Better handling of wait timeouts. Instead of waiting in intervals of 50ms, we check the result whenever new results are
added to the bin.
- Feature: "clear" method to remove all objects

# v0.2.1

Date: 2024-04-21T21:47:25.224Z
Expand Down
2 changes: 2 additions & 0 deletions src/QueryBin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export class QueryBin<T, Q extends QueryDefinitions<T> = QueryDefinitions<T>> {

addAll(values: T[]): void;

clear(): void;

all(): T[];

queryAll: QueryAll<T, Q>;
Expand Down
4 changes: 4 additions & 0 deletions src/QueryBin.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export class QueryBin {
this.latch.next();
}

clear() {
this.values = [];
}

all() {
return this.values;
}
Expand Down
8 changes: 8 additions & 0 deletions src/QueryBin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ describe("QueryBin", () => {
expect(numbers.all()).toEqual([1, 2, 3, 1, 2]);
});

it("can clear all values", () => {
const numbers = new QueryBin({});
numbers.addAll([1, 2, 3]);
expect(numbers.all()).toEqual([1, 2, 3]);
numbers.clear();
expect(numbers.all()).toEqual([]);
});

it("queryAll returns all results matching a query", () => {
const bin = new QueryBin({ dividableBy });
bin.addAll([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
Expand Down

0 comments on commit a56ed06

Please sign in to comment.