@@ -270,27 +270,35 @@ export const filters_ids = function(facets_data) {
270
270
} ;
271
271
272
272
/**
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
276
276
*/
277
277
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
+ }
289
281
290
- if ( i === 0 ) {
282
+ if ( ! filters || typeof filters !== 'object' ) {
291
283
return null ;
292
284
}
293
285
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
+
294
302
return output ;
295
303
} ;
296
304
0 commit comments