Skip to content

Commit fb7c192

Browse files
committed
Improve filters implementation
1 parent 88ea5e6 commit fb7c192

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/adapter/search-request-adapter/filters.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const adaptFilterSyntax = (filter: string) => {
1515
const [_, filterName, value] = matches
1616
return [{ filterName, value }]
1717
}
18-
return [undefined]
18+
return []
1919
}
2020

2121
/**
@@ -35,7 +35,7 @@ function extractFilters(filters?: Filter): Array<ParsedFilter | undefined> {
3535
})
3636
.flat(2)
3737
}
38-
return [undefined]
38+
return []
3939
}
4040

4141
/**
@@ -70,21 +70,23 @@ export function cacheFilters(filters?: Filter): FilterCache {
7070
* @returns {FacetsDistribution}
7171
*/
7272
export function assignMissingFilters(
73-
cache?: FilterCache,
73+
cachedFilters?: FilterCache,
7474
distribution?: FacetsDistribution
7575
): FacetsDistribution {
7676
distribution = distribution || {}
77-
if (cache && Object.keys(cache).length > 0) {
78-
for (const cachedFacet in cache) {
79-
for (const cachedField of cache[cachedFacet]) {
80-
// if cached field is not present in the returned distribution
8177

82-
if (
83-
!distribution[cachedFacet] ||
84-
!Object.keys(distribution[cachedFacet]).includes(cachedField)
85-
) {
78+
// If cachedFilters contains something
79+
if (cachedFilters && Object.keys(cachedFilters).length > 0) {
80+
// for all filters in cached filters
81+
for (const cachedFacet in cachedFilters) {
82+
// if facet does not exist on returned distribution, add an empty object
83+
if (!distribution[cachedFacet]) distribution[cachedFacet] = {}
84+
// for all fields in every filter
85+
for (const cachedField of cachedFilters[cachedFacet]) {
86+
// if the field is not present in the returned distribution
87+
// set it at 0
88+
if (!Object.keys(distribution[cachedFacet]).includes(cachedField)) {
8689
// add 0 value
87-
distribution[cachedFacet] = distribution[cachedFacet] || {}
8890
distribution[cachedFacet][cachedField] = 0
8991
}
9092
}

0 commit comments

Comments
 (0)