diff --git a/src/lib/database.ts b/src/lib/database.ts index 22a2293..6e6aacb 100644 --- a/src/lib/database.ts +++ b/src/lib/database.ts @@ -1,7 +1,11 @@ /** - * Retrieve an arbitrary object with a matching ID from an Array. - * Supposed to be used on collections returned by the PocketBase API. + * Select an element from an [objects] array where [key] matches [value]. + * Supposed to be used on collections returned by the [PocketBase] client. */ -export const get_by_id = (objects: T[], id: string): T | undefined => { - return objects.find((o: T) => ("id" in o ? o.id === id : false)); +export const get_by_value = ( + objects: T[], + key: keyof T, + value: string, +): T | undefined => { + return objects.find((o: T) => (key in o ? o[key] === value : false)); };