Skip to content

Commit 7385ca9

Browse files
committed
remove mapValues
1 parent b7133c5 commit 7385ca9

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/helpers.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,27 +270,35 @@ export const filters_ids = function(facets_data) {
270270
};
271271

272272
/**
273-
* calculates ids for facets
274-
* if there is no facet input then return null to not save resources for OR calculation
275-
* null means facets haven't matched searched items
273+
* Calculates identifiers for facets
274+
* If there are no filters, returns null to save resources during OR calculations
275+
* null means that the facets did not match any of the searched items
276276
*/
277277
export const facets_ids = function(facets_data, filters) {
278-
let output = new FastBitSet([]);
279-
let i = 0;
280-
281-
mapValues(filters, function(filters, field) {
282-
filters.forEach((filter) => {
283-
++i;
284-
output = output.new_union(
285-
facets_data[field][filter] || new FastBitSet([])
286-
);
287-
});
288-
});
278+
if (!facets_data || typeof facets_data !== 'object') {
279+
throw new Error('Invalid facets_data provided.');
280+
}
289281

290-
if (i === 0) {
282+
if (!filters || typeof filters !== 'object') {
291283
return null;
292284
}
293285

286+
const allFilters = Object.entries(filters).flatMap(
287+
([field, filterArray]) =>
288+
Array.isArray(filterArray)
289+
? filterArray.map(filter => ({ field, filter }))
290+
: []
291+
);
292+
293+
if (allFilters.length === 0) {
294+
return null;
295+
}
296+
297+
const output = allFilters.reduce((acc, { field, filter }) => {
298+
const bitset = facets_data[field]?.[filter] || new FastBitSet([]);
299+
return acc.new_union(bitset);
300+
}, new FastBitSet([]));
301+
294302
return output;
295303
};
296304

0 commit comments

Comments
 (0)