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

Commit

Permalink
Fix #84 : Add a more friendly way to update a single parameter on Sea…
Browse files Browse the repository at this point in the history
…rchParameters
  • Loading branch information
Alexandre Stanislawski committed Jun 2, 2015
1 parent 47a2073 commit 8895e5f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
57 changes: 57 additions & 0 deletions test/spec/SearchParameters.setQueryParameter.js
Original file line number Diff line number Diff line change
@@ -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();
} );

0 comments on commit 8895e5f

Please sign in to comment.