From 6aedb1d6cf616ba4cc9d516d906232bf14fc2225 Mon Sep 17 00:00:00 2001 From: Alexandre Stanislawski Date: Tue, 2 Jun 2015 14:38:37 +0200 Subject: [PATCH] Fix algolia/algoliasearch-helper-js#84 : Add a more friendly way to update a single parameter on SearchParameters --- .../src/SearchParameters/index.js | 23 +++++++- .../SearchParameters.setQueryParameter.js | 57 +++++++++++++++++++ ...=> SearchParameters.setQueryParameters.js} | 0 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParameter.js rename packages/algoliasearch-helper/test/spec/{SearchParameters.setQueryParams.js => SearchParameters.setQueryParameters.js} (100%) diff --git a/packages/algoliasearch-helper/src/SearchParameters/index.js b/packages/algoliasearch-helper/src/SearchParameters/index.js index 82c82e6007..a85026fc6b 100644 --- a/packages/algoliasearch-helper/src/SearchParameters/index.js +++ b/packages/algoliasearch-helper/src/SearchParameters/index.js @@ -706,13 +706,32 @@ SearchParameters.prototype = { return Object.freeze( newState ); }, /** - * Function that let the user set any of the parameters with a plain object. + * Let the user set a specific value for a given parameter. Will return the + * same instance if the parameter is invalid or if the value is the same as the + * previous one. + * @method + * @param {string} parameter the parameter name + * @param {any} value the value to be set, must be compliant with the definition of the attribute on the object + * @return {SearchParameters} the updated state + */ + setQueryParameter : function setParameter( parameter, value ) { + var k = keys( this ); + if( k.indexOf( parameter ) === -1 ) return this; + if( this[ parameter ] === value ) return this; + + return this.mutateMe( function updateParameter( newState ) { + newState[ parameter ] = value; + return newState; + } ); + }, + /** + * Let the user set any of the parameters with a plain object. * It won't let the user define custom properties. * @method * @param {object} params all the keys and the values to be updated * @return {SearchParameters} a new updated instance */ - setQueryParameters : function setQueryParams( params ) { + setQueryParameters : function setQueryParameters( params ) { return this.mutateMe( function merge( newInstance ) { var ks = keys( newInstance ); forEach( ks, function( k ) { diff --git a/packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParameter.js b/packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParameter.js new file mode 100644 index 0000000000..3a2260e485 --- /dev/null +++ b/packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParameter.js @@ -0,0 +1,57 @@ +"use strict"; +var test = require( "tape" ); +//var _ = require( "lodash" ); +var SearchParameters = require( "../../src/SearchParameters" ); + +test( "setqueryparameter should update existing parameter", function( t ) { + var sp = new SearchParameters( { + facets : ["facet"] + } ); + + var newValue = []; + var newsp = sp.setQueryParameter( "facets", newValue ); + + t.equal( newsp.facets, newValue, "update of an existing parameter" ); + + t.end(); +} ); + +test( "setqueryparameter should add non-existing parameter", function( t ) { + var sp = new SearchParameters( { + facets : ["facet"] + } ); + + var newValue = [ "attributesToHighlight" ]; + var newsp = sp.setQueryParameter( "attributesToHighlight", newValue ); + + t.equal( newsp.attributesToHighlight, newValue, "add new parameter" ); + + t.end(); +} ); + +test( "setQueryParameter should not create a new instance if the update is non effective", function( t ) { + var sp = new SearchParameters( { + facets : ["facet"], + maxValuesPerFacet : 10 + } ); + + var newValue = 10; + var newsp = sp.setQueryParameter( "maxValuesPerFacet", newValue ); + + t.equal( newsp, sp, "No change should result in the same instance" ); + + t.end(); +} ); + +test( "setQueryParameter should not create a new instance if the parameter is unknown", function( t ) { + var sp = new SearchParameters( { + facets : ["facet"] + } ); + + var newValue = [ "attributesToHighlight" ]; + var newsp = sp.setQueryParameter( "unknown", newValue ); + + t.equal( newsp, sp, "Unknown parameter should return the same instance" ); + + t.end(); +} ); diff --git a/packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParams.js b/packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParameters.js similarity index 100% rename from packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParams.js rename to packages/algoliasearch-helper/test/spec/SearchParameters.setQueryParameters.js