Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(index): merge ruleContexts search parameter #3944

Merged
merged 13 commits into from
Jul 15, 2019
27 changes: 27 additions & 0 deletions src/lib/utils/__tests__/mergeSearchParameters-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,31 @@ describe('mergeSearchParameters', () => {
})
);
});

it('merges and dedupes `ruleContexts` parameters', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
ruleContexts: ['ais-genre-comedy'],
samouss marked this conversation as resolved.
Show resolved Hide resolved
}),
algoliasearchHelper.SearchParameters.make({
ruleContexts: ['ais-genre-thriller', 'ais-genre-comedy'],
}),
algoliasearchHelper.SearchParameters.make({
ruleContexts: ['ais-rating-4'],
}),
algoliasearchHelper.SearchParameters.make({
ruleContexts: [],
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
ruleContexts: [
'ais-genre-comedy',
'ais-genre-thriller',
'ais-rating-4',
],
})
);
});
});
25 changes: 24 additions & 1 deletion src/lib/utils/mergeSearchParameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SearchParameters } from 'algoliasearch-helper';
import findIndex from './findIndex';
import uniq from './uniq';

type Merger = (
left: SearchParameters,
Expand All @@ -17,6 +18,7 @@ const mergeWithRest: Merger = (left, right) => {
tagRefinements,
hierarchicalFacets,
hierarchicalFacetsRefinements,
ruleContexts,
...rest
} = right;

Expand Down Expand Up @@ -103,6 +105,23 @@ const mergeHierarchicalFacetsRefinements: Merger = (left, right) =>
},
});

const mergeRuleContexts: Merger = (left, right) => {
const ruleContexts: string[] = uniq(
([] as any)
.concat(left.ruleContexts)
.concat(right.ruleContexts)
.filter(Boolean)
samouss marked this conversation as resolved.
Show resolved Hide resolved
);

if (ruleContexts.length > 0) {
samouss marked this conversation as resolved.
Show resolved Hide resolved
return left.setQueryParameters({
ruleContexts,
});
}

return left;
};

const merge = (...parameters: SearchParameters[]): SearchParameters =>
parameters.reduce((left, right) => {
const hierarchicalFacetsRefinementsMerged = mergeHierarchicalFacetsRefinements(
Expand Down Expand Up @@ -137,7 +156,11 @@ const merge = (...parameters: SearchParameters[]): SearchParameters =>
facetRefinementsMerged,
right
);
const facetsMerged = mergeFacets(disjunctiveFacetsMerged, right);
const ruleContextsMerged = mergeRuleContexts(
disjunctiveFacetsMerged,
right
);
const facetsMerged = mergeFacets(ruleContextsMerged, right);

return mergeWithRest(facetsMerged, right);
});
Expand Down