From 95a2d086eecbb1fb5fa4ce31b309cef7598e9ab5 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sat, 14 Dec 2024 15:52:20 +0100 Subject: [PATCH] Lib: Replace get_by_id helper with more general get_by_value (key can be chosen) --- src/lib/database.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) 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)); };