Lib: Replace get_by_id helper with more general get_by_value (key can be chosen)

This commit is contained in:
2024-12-14 15:52:20 +01:00
parent 16a315c1a5
commit 95a2d086ee

View File

@ -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 = <T extends object>(objects: T[], id: string): T | undefined => {
return objects.find((o: T) => ("id" in o ? o.id === id : false));
export const get_by_value = <T extends object>(
objects: T[],
key: keyof T,
value: string,
): T | undefined => {
return objects.find((o: T) => (key in o ? o[key] === value : false));
};