From 5684da156f375b78d09d6c95cdde86bb9f8818b6 Mon Sep 17 00:00:00 2001 From: indaco Date: Sat, 12 Mar 2022 18:17:35 +0100 Subject: [PATCH] refactor: groupedByOne and groupedByMany refactored Both functions have been refactored to reflect the changes introduced on sveltin v.0.6.0 with page endpoints --- src/lib/utils/collections.js | 56 +++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/lib/utils/collections.js b/src/lib/utils/collections.js index b0a9e36..a0ba92d 100644 --- a/src/lib/utils/collections.js +++ b/src/lib/utils/collections.js @@ -3,15 +3,17 @@ import reduce from 'lodash-es/reduce.js'; /** * @param {string} on - * @param {any[]} collection - * @param {string[]} giveBack + * @param {Array.} collection + * @param {Array.} giveBack + * + * @returns {Array.} */ export const groupedByOne = (on, collection, giveBack) => { const res = []; const obj = {}; forEach(collection, (curr) => { - const property = curr[on]; + const property = curr.meta[on]; if (property != undefined) { if (!(property in obj)) { obj[property] = { name: property, items: [] }; @@ -20,7 +22,7 @@ export const groupedByOne = (on, collection, giveBack) => { let result = {}; forEach(giveBack, (value) => { - result[value] = curr[value]; + result[value] = curr.meta[value]; }); obj[property].items.push(result); } @@ -31,30 +33,30 @@ export const groupedByOne = (on, collection, giveBack) => { /** * @param {string} on - * @param {any[]} collection - * @param {string[]} giveBack + * @param {Array.} collection + * @param {Array.} giveBack + * + * @returns {Array.} */ export const groupedByMany = (on, collection, giveBack) => { - return [ - Object.entries( - reduce( - collection, - (acc, curr) => { - forEach(curr[on], function (item) { - let result = {}; - forEach(giveBack, (element) => { - result[element] = curr[element]; - }); - if (acc[item]) { - acc[item].push(result); - } else { - acc[item] = [result]; - } + return Object.entries( + reduce( + collection, + (acc, curr) => { + forEach(curr.meta[on], function (item) { + let result = {}; + forEach(giveBack, (element) => { + result[element] = curr.meta[element]; }); - return acc; - }, - {} - ) - ).map(([name, items]) => ({ name, items })) - ]; + if (acc[item]) { + acc[item].push(result); + } else { + acc[item] = [result]; + } + }); + return acc; + }, + {} + ) + ).map(([name, items]) => ({ name, items })); };