Skip to content

Commit

Permalink
FIX algolia/algoliasearch-helper-js#72 : set multiple conjunctive ref…
Browse files Browse the repository at this point in the history
…inement for the same attribute
  • Loading branch information
Alexandre Stanislawski committed May 12, 2015
1 parent 4af9be8 commit 5ab55ae
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
34 changes: 28 additions & 6 deletions packages/algoliasearch-helper/src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,16 @@ SearchParameters.prototype = {
* @return {SearchParameters}
*/
addFacetRefinement : function addFacetRefinement( facet, value ) {
if( this.isRefined( facet, value ) ) {
return this;
}

return this.mutateMe( function( m ) {
m.page = 0;
m.facetsRefinements[ facet ] = value;
if( !m.facetsRefinements[ facet ] ) {
m.facetsRefinements[ facet ] = [];
}
m.facetsRefinements[ facet ].push( value );
} );
},
/**
Expand Down Expand Up @@ -403,15 +410,29 @@ SearchParameters.prototype = {
} );
},
/**
* Remove a refinement set on facet.
* Remove a refinement set on facet. If a value is provided, it will clear the
* refinement for the given value, otherwise it will clear all the refinement
* values for the facetted attribute.
* @method
* @param {string} facet
* @param {string} value
* @return {SearchParameters}
*/
removeFacetRefinement : function removeFacetRefinement( facet ) {
removeFacetRefinement : function removeFacetRefinement( facet, value ) {
return this.mutateMe( function( m ) {
m.page = 0;
m._clearFacetRefinements( facet );
if( value ) {
var idx = m.facetsRefinements[ facet ].indexOf( value );
if( idx > -1 ) {
m.facetsRefinements[ facet ].splice( idx, 1 );
if( m.facetsRefinements[ facet ].length === 0 ) {
delete m.facetsRefinements[ facet ];
}
}
}
else {
m._clearFacetRefinements( facet );
}
} );
},
/**
Expand Down Expand Up @@ -519,7 +540,7 @@ SearchParameters.prototype = {
*/
toggleFacetRefinement : function toggleFacetRefinement( facet, value ) {
if( this.isFacetRefined( facet, value ) ) {
return this.removeFacetRefinement( facet );
return this.removeFacetRefinement( facet, value );
}
else {
return this.addFacetRefinement( facet, value );
Expand Down Expand Up @@ -563,7 +584,8 @@ SearchParameters.prototype = {
* @return {boolean}
*/
isFacetRefined : function isFacetRefined( facet, value ) {
return this.facetsRefinements[ facet ] === value;
return this.facetsRefinements[ facet ] &&
this.facetsRefinements[ facet ].indexOf( value ) !== -1;
},
/**
* Returns true if the couple (facet, value) is excluded
Expand Down
6 changes: 4 additions & 2 deletions packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,10 @@ AlgoliaSearchHelper.prototype._hasDisjunctiveRefinements = function( facet ) {
AlgoliaSearchHelper.prototype._getFacetFilters = function( facet ) {
var facetFilters = [];

forEach( this.state.facetsRefinements, function( facetValue, facetName ) {
facetFilters.push( facetName + ":" + facetValue );
forEach( this.state.facetsRefinements, function( facetValues, facetName ) {
forEach( facetValues, function( facetValue ) {
facetFilters.push( facetName + ":" + facetValue );
} );
} );

forEach( this.state.facetsExcludes, function( facetValues, facetName ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";

var test = require( "tape" );
//var _ = require( "lodash" );
var SearchParameters = require( "../../src/SearchParameters" );
Expand Down
9 changes: 4 additions & 5 deletions packages/algoliasearch-helper/test/spec/helper.clears.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";

var test = require( "tape" ),
algoliasearchHelper = require( "../../index" ),
isUndefined = require( "lodash/lang/isUndefined" );
Expand All @@ -25,7 +24,7 @@ var init = function init() {
test( "Check that the state objects match how we test them", function( t ) {
var helper = init();

t.deepEqual( helper.state.facetsRefinements, { "facet1" : "0", "facet2" : "0" } );
t.deepEqual( helper.state.facetsRefinements, { "facet1" : [ "0" ], "facet2" : [ "0" ] } );
t.deepEqual( helper.state.disjunctiveFacetsRefinements, { "disjunctiveFacet1" : [ "0" ], "disjunctiveFacet2" : [ "0" ] } );
t.deepEqual( helper.state.facetsExcludes, { "excluded1" : [ "0" ], "excluded2" : [ "0" ] } );
t.deepEqual( helper.state.numericRefinements, { "numeric1" : { ">=" : "0", "<" : "10" }, "numeric2" : { ">=" : 0, "<" : 10 } } );
Expand All @@ -37,7 +36,7 @@ test( "Clear with a name should work on every type and not remove others than ta
var helper = init();

helper.clearRefinements( "facet1" );
t.deepEqual( helper.state.facetsRefinements, { "facet2" : "0" } );
t.deepEqual( helper.state.facetsRefinements, { "facet2" : [ "0" ] } );

helper.clearRefinements( "disjunctiveFacet1" );
t.deepEqual( helper.state.disjunctiveFacetsRefinements, { "disjunctiveFacet2" : [ "0" ] } );
Expand All @@ -57,7 +56,7 @@ test( "Clearing the same field from multiple elements should remove it everywher

helper.addNumericRefinement( "facet1", ">=", "10" ).toggleExclude( "facet1", "value" );

t.equal( helper.state.facetsRefinements.facet1, "0" );
t.deepEqual( helper.state.facetsRefinements.facet1, [ "0" ] );
t.deepEqual( helper.state.numericRefinements.facet1, { ">=" : "10" } );
t.deepEqual( helper.state.facetsExcludes.facet1, [ "value" ] );

Expand All @@ -72,7 +71,7 @@ test( "Clearing the same field from multiple elements should remove it everywher
test( "Clearing twice the same attribute should be not problem", function( t ) {
var helper = init();

t.equal( helper.state.facetsRefinements.facet1, "0" );
t.deepEqual( helper.state.facetsRefinements.facet1, [ "0" ] );
helper.clearRefinements( "facet1" );
t.assert( isUndefined( helper.state.facetsRefinements.facet1 ) );
t.doesNotThrow( function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use strict";
var test = require("tape");
var _ = require( "lodash" );
var sinon = require("sinon");
Expand Down
7 changes: 3 additions & 4 deletions packages/algoliasearch-helper/test/spec/refinements.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ test( "Adding refinments should add an entry to the refinments attribute", funct
var facetValue = "42";

t.ok( _.isEmpty( helper.state.facetsRefinements ), "should be empty at first" );
helper.addRefine( facetName, "42" );
t.ok( _.size( helper.state.facetsRefinements ) === 1 &&
helper.state.facetsRefinements.facet1 === facetValue,
"when adding a refinment, should have one" );
helper.addRefine( facetName, facetValue );
t.ok( _.size( helper.state.facetsRefinements ) === 1, "when adding a refinment, should have one" );
t.deepEqual( helper.state.facetsRefinements.facet1, [ facetValue ] );
helper.addRefine( facetName, facetValue );
t.ok( _.size( helper.state.facetsRefinements ) === 1, "when adding the same, should still be one" );
helper.removeRefine( facetName, facetValue );
Expand Down
1 change: 0 additions & 1 deletion packages/algoliasearch-helper/test/spec/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";

var test = require("tape");
var sinon = require("sinon");
var algoliaSearch = require( "algoliasearch" );
Expand Down

0 comments on commit 5ab55ae

Please sign in to comment.