From 7385ca9a02948e404bdb4a2607d0bebf1d79d329 Mon Sep 17 00:00:00 2001 From: Mateusz Rzepa Date: Fri, 13 Sep 2024 16:18:02 +0200 Subject: [PATCH] remove mapValues --- src/helpers.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 6a53120..650848d 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -270,27 +270,35 @@ export const filters_ids = function(facets_data) { }; /** - * calculates ids for facets - * if there is no facet input then return null to not save resources for OR calculation - * null means facets haven't matched searched items + * Calculates identifiers for facets + * If there are no filters, returns null to save resources during OR calculations + * null means that the facets did not match any of the searched items */ export const facets_ids = function(facets_data, filters) { - let output = new FastBitSet([]); - let i = 0; - - mapValues(filters, function(filters, field) { - filters.forEach((filter) => { - ++i; - output = output.new_union( - facets_data[field][filter] || new FastBitSet([]) - ); - }); - }); + if (!facets_data || typeof facets_data !== 'object') { + throw new Error('Invalid facets_data provided.'); + } - if (i === 0) { + if (!filters || typeof filters !== 'object') { return null; } + const allFilters = Object.entries(filters).flatMap( + ([field, filterArray]) => + Array.isArray(filterArray) + ? filterArray.map(filter => ({ field, filter })) + : [] + ); + + if (allFilters.length === 0) { + return null; + } + + const output = allFilters.reduce((acc, { field, filter }) => { + const bitset = facets_data[field]?.[filter] || new FastBitSet([]); + return acc.new_union(bitset); + }, new FastBitSet([])); + return output; };