Skip to content

Commit

Permalink
feat(index): add mergeSearchParameters function (#3917)
Browse files Browse the repository at this point in the history
* feat(index): add mergeSearchParameters function

```js
mergeSearchParameters(a, b, c) // new search parameters
```

- overrides left-to-right on uncontrolled parameters
- merges controlled parameters
- overrides left-to-right on duplicate attributes

Co-authored-by: samouss <samouss@users.noreply.github.com>

* fix(support): add polyfill for findIndex

This is the same one as in helper v3

* chore: fix lint

* chore: remove compose

* change test titles

* chore(merge): use in index widget

* chore: reorder

* fix typo
  • Loading branch information
Haroenv committed Oct 23, 2019
1 parent 632e06b commit c0fe7bb
Show file tree
Hide file tree
Showing 4 changed files with 520 additions and 11 deletions.
370 changes: 370 additions & 0 deletions src/lib/utils/__tests__/mergeSearchParameters-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
import algoliasearchHelper from 'algoliasearch-helper';
import merge from '../mergeSearchParameters';

describe('mergeSearchParameters', () => {
it('overrides non-managed parameters', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
// Inherit
hitsPerPage: 2,
attributesToSnippet: ['description'],
// Overridden
query: 'Samsung',
attributesToHighlight: ['name'],
}),
algoliasearchHelper.SearchParameters.make({
// hitsPerPage: 2,
// attributesToSnippet: ['description'],
query: 'Apple',
attributesToHighlight: ['name', 'author'],
}),
algoliasearchHelper.SearchParameters.make({
// hitsPerPage: 2,
// attributesToSnippet: ['description'],
// attributesToHighlight: ['name', 'author'],
query: 'Apple iPhone',
distinct: true,
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
hitsPerPage: 2,
attributesToSnippet: ['description'],
attributesToHighlight: ['name', 'author'],
query: 'Apple iPhone',
distinct: true,
})
);
});

it('merges `facets` parameters', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
facets: ['brand'],
}),
algoliasearchHelper.SearchParameters.make({
facets: ['categories'],
}),
algoliasearchHelper.SearchParameters.make({
facets: ['brand', 'colors'],
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
facets: ['brand', 'categories', 'colors'],
})
);
});

it('merges `disjunctiveFacets` parameters', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['brand'],
}),
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['categories'],
}),
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['brand', 'colors'],
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['brand', 'categories', 'colors'],
})
);
});

it('merges `facetsRefinements` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
facets: ['brand'],
facetsRefinements: {
brand: ['Samsung'],
},
}),
algoliasearchHelper.SearchParameters.make({
facets: ['categories'],
facetsRefinements: {
categories: ['TVs'],
},
}),
algoliasearchHelper.SearchParameters.make({
facets: ['brand', 'colors'],
facetsRefinements: {
brand: ['Apple'],
colors: ['Red'],
},
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
facets: ['brand', 'categories', 'colors'],
facetsRefinements: {
brand: ['Apple'],
categories: ['TVs'],
colors: ['Red'],
},
})
);
});

it('merges `facetsExcludes` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
facets: ['brand'],
facetsExcludes: {
brand: ['Samsung'],
},
}),
algoliasearchHelper.SearchParameters.make({
facets: ['categories'],
facetsExcludes: {
categories: ['TVs'],
},
}),
algoliasearchHelper.SearchParameters.make({
facets: ['brand', 'colors'],
facetsExcludes: {
brand: ['Apple'],
colors: ['Red'],
},
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
facets: ['brand', 'categories', 'colors'],
facetsExcludes: {
brand: ['Apple'],
categories: ['TVs'],
colors: ['Red'],
},
})
);
});

it('merges `disjunctiveFacetsRefinements` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['brand'],
disjunctiveFacetsRefinements: {
brand: ['Samsung'],
},
}),
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['categories'],
disjunctiveFacetsRefinements: {
categories: ['TVs'],
},
}),
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['brand', 'colors'],
disjunctiveFacetsRefinements: {
brand: ['Apple'],
colors: ['Red'],
},
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
disjunctiveFacets: ['brand', 'categories', 'colors'],
disjunctiveFacetsRefinements: {
brand: ['Apple'],
categories: ['TVs'],
colors: ['Red'],
},
})
);
});

it('merges `numericRefinements` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
numericRefinements: {
price: {
'>=': [10],
'<=': [100],
},
},
}),
algoliasearchHelper.SearchParameters.make({
numericRefinements: {
rating: {
'>=': [3],
},
},
}),
algoliasearchHelper.SearchParameters.make({
numericRefinements: {
price: {
'>': [100],
},
vote: {
'>=': [50],
},
},
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
numericRefinements: {
price: {
'>': [100],
},
rating: {
'>=': [3],
},
vote: {
'>=': [50],
},
},
})
);
});

it('merges `tagRefinements` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
tagRefinements: ['brand'],
}),
algoliasearchHelper.SearchParameters.make({
tagRefinements: ['categories'],
}),
algoliasearchHelper.SearchParameters.make({
tagRefinements: ['brand', 'colors'],
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
tagRefinements: ['brand', 'categories', 'colors'],
})
);
});

it('merges `hierarchicalFacets` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'categories',
attributes: ['categories.lvl0', 'categories.lvl1'],
},
],
}),
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'department',
attributes: ['department.lvl0', 'department.lvl1'],
},
],
}),
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'categories',
attributes: ['topLevelCategories', 'subLevelCategories'],
},
{
name: 'folders',
attributes: ['folders.lvl0', 'folders.lvl1'],
},
],
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'categories',
attributes: ['topLevelCategories', 'subLevelCategories'],
},
{
name: 'department',
attributes: ['department.lvl0', 'department.lvl1'],
},
{
name: 'folders',
attributes: ['folders.lvl0', 'folders.lvl1'],
},
],
})
);
});

it('merges `hierarchicalFacetsRefinements` parameters, overrides conflicts', () => {
const actual = merge(
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'categories',
attributes: ['categories.lvl0', 'categories.lvl1'],
},
],
hierarchicalFacetsRefinements: {
categories: ['Appliances > Fans'],
},
}),
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'department',
attributes: ['department.lvl0', 'department.lvl1'],
},
],
hierarchicalFacetsRefinements: {
department: ['Engineering > Squad'],
},
}),
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'categories',
attributes: ['topLevelCategories', 'subLevelCategories'],
},
{
name: 'folders',
attributes: ['folders.lvl0', 'folders.lvl1'],
},
],
hierarchicalFacetsRefinements: {
categories: ['Cell Phones > Prepaid Phones'],
folders: ['Music > Artist > Mike Miller'],
},
})
);

expect(actual).toEqual(
algoliasearchHelper.SearchParameters.make({
hierarchicalFacets: [
{
name: 'categories',
attributes: ['topLevelCategories', 'subLevelCategories'],
},
{
name: 'department',
attributes: ['department.lvl0', 'department.lvl1'],
},
{
name: 'folders',
attributes: ['folders.lvl0', 'folders.lvl1'],
},
],
hierarchicalFacetsRefinements: {
categories: ['Cell Phones > Prepaid Phones'],
department: ['Engineering > Squad'],
folders: ['Music > Artist > Mike Miller'],
},
})
);
});
});
1 change: 1 addition & 0 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { default as escape } from './escape';
export { default as find } from './find';
export { default as findIndex } from './findIndex';
export { default as mergeDeep } from './mergeDeep';
export { default as mergeSearchParameters } from './mergeSearchParameters';
export { default as resolveSearchParameters } from './resolveSearchParameters';
export { warning, deprecate } from './logger';
export {
Expand Down
Loading

0 comments on commit c0fe7bb

Please sign in to comment.