From 2e83ef89b4a79e37cafe41aa2b8ce1c871f2b433 Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Sun, 22 Sep 2024 03:57:55 +0300 Subject: [PATCH] Add function for fixing and improving the API responses --- src/utils.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/utils.js b/src/utils.js index 2908d74..cf372a2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -41,3 +41,15 @@ export function calculateProgress(...elements) { isNotStarted: completeCount === 0, }; } + +export const addIdAndRelations = (items, relations = []) => + Object.fromEntries(Object.entries(items).map(([id, item]) => + ([id, { + id, + ...item, + ...Object.fromEntries(relations.map(([field, collection, idField]) => ([ + field, + Array.isArray(item[idField]) ? item[idField].map(id => collection[id]) : collection[item[idField]], + ]))), + }]) + ));