Skip to content

Commit

Permalink
feat: Add utility 'find' functions for improved typing and more speci…
Browse files Browse the repository at this point in the history
…fic functionality for class 'find' methods.
  • Loading branch information
adam-coster committed Jul 2, 2021
1 parent e427ba2 commit 344f288
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ As environment variables:
- Search cards by title
- Create a card
- Delete a card
- Make all tests _self-contained_ (e.g. create a board, _then_ add widgets and cards, etc)

## Usage

Expand Down
30 changes: 30 additions & 0 deletions src/lib/utility.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ArrayMatchFunction } from '$/types/Utility.js';
import { assertBravoClaim } from './errors.js';

export function stringsMatchIgnoringCase(string1: string, string2: string) {
Expand All @@ -9,6 +10,35 @@ export function stringsMatchIgnoringCase(string1: string, string2: string) {
);
}

export function stringsMatch(
string1: string,
string2: string,
options?: { ignoreCase?: boolean },
) {
if (options?.ignoreCase) {
return stringsMatchIgnoringCase(string1, string2);
}
return string1 === string2;
}

/**
* Async find function, allowing
* for async match functions. (Synchronous funcs
* will also work fine.)
*/
export async function find<Item>(
items: Item[],
matchFunction: ArrayMatchFunction<Item>,
) {
let idx = 0;
for await (const item of items) {
if (await matchFunction(item, idx, items)) {
return item;
}
idx++;
}
}

export function findByField<
Item extends Record<string, any>,
Field extends keyof Item,
Expand Down

0 comments on commit 344f288

Please sign in to comment.