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

Commit

Permalink
refactor(SearchParameters): removes default values (#670)
Browse files Browse the repository at this point in the history
* refactor(SearchParameters): removes default values

* fix(helper.derive): use toBeUndefined
  • Loading branch information
samouss committed May 9, 2019
1 parent 2464aa1 commit 235c57b
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 306 deletions.
273 changes: 0 additions & 273 deletions src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,6 @@ function findArray(array, searchedValue) {
function SearchParameters(newParameters) {
var params = newParameters ? SearchParameters._parseNumbers(newParameters) : {};

/**
* Targeted index. This parameter is mandatory.
* @member {string}
*/
this.index = params.index || '';

// Query
/**
* Query string of the instant search. The empty string is a valid query.
* @member {string}
* @see https://www.algolia.com/doc/rest#param-query
*/
this.query = params.query || '';

// Facets
/**
* This attribute contains the list of all the conjunctive facets
* used. This list will be added to requested facets in the
Expand Down Expand Up @@ -199,264 +184,6 @@ function SearchParameters(newParameters) {
*/
this.hierarchicalFacetsRefinements = params.hierarchicalFacetsRefinements || {};

/**
* Contains the numeric filters in the raw format of the Algolia API. Setting
* this parameter is not compatible with the usage of numeric filters methods.
* @see https://www.algolia.com/doc/javascript#numericFilters
* @member {string}
*/
this.numericFilters = params.numericFilters;

/**
* Contains the tag filters in the raw format of the Algolia API. Setting this
* parameter is not compatible with the of the add/remove/toggle methods of the
* tag api.
* @see https://www.algolia.com/doc/rest#param-tagFilters
* @member {string}
*/
this.tagFilters = params.tagFilters;

/**
* Contains the optional tag filters in the raw format of the Algolia API.
* @see https://www.algolia.com/doc/rest#param-tagFilters
* @member {string}
*/
this.optionalTagFilters = params.optionalTagFilters;

/**
* Contains the optional facet filters in the raw format of the Algolia API.
* @see https://www.algolia.com/doc/rest#param-tagFilters
* @member {string}
*/
this.optionalFacetFilters = params.optionalFacetFilters;


// Misc. parameters
/**
* Number of hits to be returned by the search API
* @member {number}
* @see https://www.algolia.com/doc/rest#param-hitsPerPage
*/
this.hitsPerPage = params.hitsPerPage;
/**
* Number of values for each faceted attribute
* @member {number}
* @see https://www.algolia.com/doc/rest#param-maxValuesPerFacet
*/
this.maxValuesPerFacet = params.maxValuesPerFacet;
/**
* The current page number
* @member {number}
* @see https://www.algolia.com/doc/rest#param-page
*/
this.page = params.page || 0;
/**
* How the query should be treated by the search engine.
* Possible values: prefixAll, prefixLast, prefixNone
* @see https://www.algolia.com/doc/rest#param-queryType
* @member {string}
*/
this.queryType = params.queryType;
/**
* How the typo tolerance behave in the search engine.
* Possible values: true, false, min, strict
* @see https://www.algolia.com/doc/rest#param-typoTolerance
* @member {string}
*/
this.typoTolerance = params.typoTolerance;

/**
* Number of characters to wait before doing one character replacement.
* @see https://www.algolia.com/doc/rest#param-minWordSizefor1Typo
* @member {number}
*/
this.minWordSizefor1Typo = params.minWordSizefor1Typo;
/**
* Number of characters to wait before doing a second character replacement.
* @see https://www.algolia.com/doc/rest#param-minWordSizefor2Typos
* @member {number}
*/
this.minWordSizefor2Typos = params.minWordSizefor2Typos;
/**
* Configure the precision of the proximity ranking criterion
* @see https://www.algolia.com/doc/rest#param-minProximity
*/
this.minProximity = params.minProximity;
/**
* Should the engine allow typos on numerics.
* @see https://www.algolia.com/doc/rest#param-allowTyposOnNumericTokens
* @member {boolean}
*/
this.allowTyposOnNumericTokens = params.allowTyposOnNumericTokens;
/**
* Should the plurals be ignored
* @see https://www.algolia.com/doc/rest#param-ignorePlurals
* @member {boolean}
*/
this.ignorePlurals = params.ignorePlurals;
/**
* Restrict which attribute is searched.
* @see https://www.algolia.com/doc/rest#param-restrictSearchableAttributes
* @member {string}
*/
this.restrictSearchableAttributes = params.restrictSearchableAttributes;
/**
* Enable the advanced syntax.
* @see https://www.algolia.com/doc/rest#param-advancedSyntax
* @member {boolean}
*/
this.advancedSyntax = params.advancedSyntax;
/**
* Enable the analytics
* @see https://www.algolia.com/doc/rest#param-analytics
* @member {boolean}
*/
this.analytics = params.analytics;
/**
* Tag of the query in the analytics.
* @see https://www.algolia.com/doc/rest#param-analyticsTags
* @member {string}
*/
this.analyticsTags = params.analyticsTags;
/**
* Enable the synonyms
* @see https://www.algolia.com/doc/rest#param-synonyms
* @member {boolean}
*/
this.synonyms = params.synonyms;
/**
* Should the engine replace the synonyms in the highlighted results.
* @see https://www.algolia.com/doc/rest#param-replaceSynonymsInHighlight
* @member {boolean}
*/
this.replaceSynonymsInHighlight = params.replaceSynonymsInHighlight;
/**
* Add some optional words to those defined in the dashboard
* @see https://www.algolia.com/doc/rest#param-optionalWords
* @member {string}
*/
this.optionalWords = params.optionalWords;
/**
* Possible values are "lastWords" "firstWords" "allOptional" "none" (default)
* @see https://www.algolia.com/doc/rest#param-removeWordsIfNoResults
* @member {string}
*/
this.removeWordsIfNoResults = params.removeWordsIfNoResults;
/**
* List of attributes to retrieve
* @see https://www.algolia.com/doc/rest#param-attributesToRetrieve
* @member {string}
*/
this.attributesToRetrieve = params.attributesToRetrieve;
/**
* List of attributes to highlight
* @see https://www.algolia.com/doc/rest#param-attributesToHighlight
* @member {string}
*/
this.attributesToHighlight = params.attributesToHighlight;
/**
* Code to be embedded on the left part of the highlighted results
* @see https://www.algolia.com/doc/rest#param-highlightPreTag
* @member {string}
*/
this.highlightPreTag = params.highlightPreTag;
/**
* Code to be embedded on the right part of the highlighted results
* @see https://www.algolia.com/doc/rest#param-highlightPostTag
* @member {string}
*/
this.highlightPostTag = params.highlightPostTag;
/**
* List of attributes to snippet
* @see https://www.algolia.com/doc/rest#param-attributesToSnippet
* @member {string}
*/
this.attributesToSnippet = params.attributesToSnippet;
/**
* Enable the ranking informations in the response, set to 1 to activate
* @see https://www.algolia.com/doc/rest#param-getRankingInfo
* @member {number}
*/
this.getRankingInfo = params.getRankingInfo;
/**
* Remove duplicates based on the index setting attributeForDistinct
* @see https://www.algolia.com/doc/rest#param-distinct
* @member {boolean|number}
*/
this.distinct = params.distinct;
/**
* Center of the geo search.
* @see https://www.algolia.com/doc/rest#param-aroundLatLng
* @member {string}
*/
this.aroundLatLng = params.aroundLatLng;
/**
* Center of the search, retrieve from the user IP.
* @see https://www.algolia.com/doc/rest#param-aroundLatLngViaIP
* @member {boolean}
*/
this.aroundLatLngViaIP = params.aroundLatLngViaIP;
/**
* Radius of the geo search.
* @see https://www.algolia.com/doc/rest#param-aroundRadius
* @member {number}
*/
this.aroundRadius = params.aroundRadius;
/**
* Precision of the geo search.
* @see https://www.algolia.com/doc/rest#param-aroundPrecision
* @member {number}
*/
this.minimumAroundRadius = params.minimumAroundRadius;
/**
* Precision of the geo search.
* @see https://www.algolia.com/doc/rest#param-minimumAroundRadius
* @member {number}
*/
this.aroundPrecision = params.aroundPrecision;
/**
* Geo search inside a box.
* @see https://www.algolia.com/doc/rest#param-insideBoundingBox
* @member {string}
*/
this.insideBoundingBox = params.insideBoundingBox;
/**
* Geo search inside a polygon.
* @see https://www.algolia.com/doc/rest#param-insidePolygon
* @member {string}
*/
this.insidePolygon = params.insidePolygon;
/**
* Allows to specify an ellipsis character for the snippet when we truncate the text
* (added before and after if truncated).
* The default value is an empty string and we recommend to set it to "…"
* @see https://www.algolia.com/doc/rest#param-insidePolygon
* @member {string}
*/
this.snippetEllipsisText = params.snippetEllipsisText;
/**
* Allows to specify some attributes name on which exact won't be applied.
* Attributes are separated with a comma (for example "name,address" ), you can also use a
* JSON string array encoding (for example encodeURIComponent('["name","address"]') ).
* By default the list is empty.
* @see https://www.algolia.com/doc/rest#param-disableExactOnAttributes
* @member {string|string[]}
*/
this.disableExactOnAttributes = params.disableExactOnAttributes;
/**
* Applies 'exact' on single word queries if the word contains at least 3 characters
* and is not a stop word.
* Can take two values: true or false.
* By default, its set to false.
* @see https://www.algolia.com/doc/rest#param-enableExactOnSingleWordQuery
* @member {boolean}
*/
this.enableExactOnSingleWordQuery = params.enableExactOnSingleWordQuery;

// Undocumented parameters, still needed otherwise we fail
this.offset = params.offset;
this.length = params.length;

var self = this;
forOwn(params, function checkForUnknownParameter(paramValue, paramName) {
if (SearchParameters.PARAMETERS.indexOf(paramName) === -1) {
Expand Down
7 changes: 5 additions & 2 deletions src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,8 @@ AlgoliaSearchHelper.prototype.toggleTag = function(tag) {
* // returns 1
*/
AlgoliaSearchHelper.prototype.nextPage = function() {
return this.setPage(this.state.page + 1);
var page = this.state.page || 0;
return this.setPage(page + 1);
};

/**
Expand All @@ -729,7 +730,8 @@ AlgoliaSearchHelper.prototype.nextPage = function() {
* // returns 0
*/
AlgoliaSearchHelper.prototype.previousPage = function() {
return this.setPage(this.state.page - 1);
var page = this.state.page || 0;
return this.setPage(page - 1);
};

/**
Expand All @@ -739,6 +741,7 @@ function setCurrentPage(page) {
if (page < 0) throw new Error('Page requested below 0.');

this._change(this.state.setPage(page));

return this;
}

Expand Down
5 changes: 5 additions & 0 deletions test/datasets/SearchParameters/search.dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ function getData() {
};

var searchParams = new SearchParameters({
// @TODO: at the moment we have to provide a default value for the page, otherwise
// some methods reset the page to 0 (because of the reset behavior). This is what
// happens: page omit -> page defined with 0. We'll fix those issues with a next PR
// that implements a proper reset with the updated structure of the `SearchParameters`.
page: 0,
index: 'test_hotels-node',
disjunctiveFacets: ['city'],
disjunctiveFacetsRefinements: {
Expand Down
2 changes: 1 addition & 1 deletion test/integration-spec/helper.derive.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test(
}).then(function(responses) {
var mainResponse = responses[0];

expect(mainResponse.state.query).toBe('');
expect(mainResponse.state.query).toBeUndefined();
expect(mainResponse.results.hits.length).toBe(2);

var derivedResponse = responses[1];
Expand Down
2 changes: 1 addition & 1 deletion test/spec/algoliasearch.helper/derive/multiqueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('[Derived helper] no derived helpers, modification', function() {

function searchTest(requests) {
expect(requests.length).toBe(2);
expect(requests[0].params.query).toBe('');
expect(requests[0].params.query).toBeUndefined();
expect(requests[1].params.query).toBe('otherQuery');

delete requests[0].params.query;
Expand Down
5 changes: 5 additions & 0 deletions test/spec/algoliasearch.helper/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ test('Change events should only be emitted for meaningful changes', function() {
var fakeClient = makeFakeClient();
var helper = algoliaSearchHelper(fakeClient, 'Index', {
query: 'a',
// @TODO: at the moment we have to provide a default value for the page, otherwise
// some methods reset the page to 0 (because of the reset behavior). This is what
// happens: page omit -> page defined with 0. We'll fix those issues with a next PR
// that implements a proper reset with the updated structure of the `SearchParameters`.
page: 0,
disjunctiveFacets: ['city'],
disjunctiveFacetsRefinements: {city: ['Paris']},
facets: ['tower'],
Expand Down
2 changes: 0 additions & 2 deletions test/spec/algoliasearch.helper/getQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ test('getQuery', function() {
});

expect(helper.getQuery()).toEqual({
query: '',
page: 0,
minWordSizefor1Typo: 8,
ignorePlurals: true,
facets: ['facet1', 'facet2', 'facet3', 'df1', 'df2', 'df3'],
Expand Down
Loading

0 comments on commit 235c57b

Please sign in to comment.