Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
feat(getState): remove "filter" option (#707)
Browse files Browse the repository at this point in the history
* feat(getState): remove "filter" option

This also removes the `.filter` function on `SearchParameters` which isn't used.

Another slight reduction in size: 11.2  -> 10.9 (after lodash.merge)

BREAKING CHANGE: getState(filters) is replaced my manually filtering the returned object
BREAKING CHANGE: SearchParameters.filter is removed

* doc(filter): remove reference
  • Loading branch information
Haroenv committed Nov 18, 2019
1 parent aa128fc commit ac52791
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 133 deletions.
1 change: 0 additions & 1 deletion documentation-src/metalsmith/content/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,6 @@ All the attributes specific to the helper are described below:
{{> jsdoc jsdoc/state/addTagRefinement}}
{{> jsdoc jsdoc/state/clearRefinements}}
{{> jsdoc jsdoc/state/clearTags}}
{{> jsdoc jsdoc/state/filter}}
{{> jsdoc jsdoc/state/getConjunctiveRefinements}}
{{> jsdoc jsdoc/state/getDisjunctiveRefinements}}
{{> jsdoc jsdoc/state/getExcludeRefinements}}
Expand Down
60 changes: 0 additions & 60 deletions src/SearchParameters/filterState.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ var valToNumber = require('../functions/valToNumber');
var omit = require('../functions/omit');
var objectHasKeys = require('../functions/objectHasKeys');

var filterState = require('./filterState');

var RefinementList = require('./RefinementList');

/**
Expand Down Expand Up @@ -1403,17 +1401,6 @@ SearchParameters.prototype = {
return this.setPage(0);
},

/**
* Returns an object with only the selected attributes.
* @param {string[]} filters filters to retrieve only a subset of the state. It
* accepts strings that can be either attributes of the SearchParameters (e.g. hitsPerPage)
* or attributes of the index with the notation 'attribute:nameOfMyAttribute'
* @return {object}
*/
filter: function(filters) {
return filterState(this, filters);
},

/**
* Helper function to get the hierarchicalFacet separator or the default one (`>`)
* @param {object} hierarchicalFacet
Expand Down
9 changes: 2 additions & 7 deletions src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -912,20 +912,15 @@ AlgoliaSearchHelper.prototype.setState = function(newState) {

/**
* Get the current search state stored in the helper. This object is immutable.
* @param {string[]} [filters] optional filters to retrieve only a subset of the state
* @return {SearchParameters|object} if filters is specified a plain object is
* returned containing only the requested fields, otherwise return the unfiltered
* state
* @example
* // Get the complete state as stored in the helper
* helper.getState();
* @example
* // Get a part of the state with all the refinements on attributes and the query
* helper.getState(['query', 'attribute:category']);
*/
AlgoliaSearchHelper.prototype.getState = function(filters) {
if (filters === undefined) return this.state;
return this.state.filter(filters);
AlgoliaSearchHelper.prototype.getState = function() {
return this.state;
};

/**
Expand Down
52 changes: 0 additions & 52 deletions test/spec/algoliasearch.helper/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,6 @@ test('getState should return the current state of the helper', function() {
expect(helper.getState()).toEqual(helper.state);
});

test('getState should return an object according to the specified filters', function() {
var initialState = {
query: 'a query',
facets: ['facetA', 'facetWeDontCareAbout'],
disjunctiveFacets: ['facetB'],
hierarchicalFacets: [{
name: 'facetC',
attributes: ['facetC']
}],
minWordSizefor1Typo: 1
};
var index = 'indexNameInTheHelper';
var helper = algoliasearchHelper(fakeClient, index, initialState);

helper.toggleRefine('facetA', 'a');
helper.toggleRefine('facetWeDontCareAbout', 'v');
helper.toggleRefine('facetB', 'd');
helper.toggleRefine('facetC', 'menu');
helper.addNumericRefinement('numerical', '=', 3);
helper.addNumericRefinement('numerical2', '<=', 3);

var stateFinalWithSpecificAttribute = {
index: index,
query: initialState.query,
facetsRefinements: {facetA: ['a']},
disjunctiveFacetsRefinements: {facetB: ['d']},
numericRefinements: {numerical: {'=': [3]}}
};

var stateFinalWithoutSpecificAttributes = {
index: index,
query: initialState.query,
facetsRefinements: {facetA: ['a'], facetWeDontCareAbout: ['v']},
disjunctiveFacetsRefinements: {facetB: ['d']},
hierarchicalFacetsRefinements: {facetC: ['menu']},
numericRefinements: {numerical2: {'<=': [3]}, numerical: {'=': [3]}}
};

var stateWithHierarchicalAttribute = {
hierarchicalFacetsRefinements: {facetC: ['menu']}
};

expect(helper.getState([])).toEqual({});
expect(
helper.getState(['index', 'query', 'attribute:facetA', 'attribute:facetB', 'attribute:numerical'])
).toEqual(stateFinalWithSpecificAttribute);

expect(helper.getState(['attribute:facetC'])).toEqual(stateWithHierarchicalAttribute);

expect(helper.getState(['index', 'query', 'attribute:*'])).toEqual(stateFinalWithoutSpecificAttributes);
});

test('setState should set a default hierarchicalFacetRefinement when a rootPath is defined', function() {
var searchParameters = {hierarchicalFacets: [
{
Expand Down

0 comments on commit ac52791

Please sign in to comment.