algoliasearch-helper
Advanced tools
Comparing version 2.0.0-rc3 to 2.0.0-rc4
{ | ||
"name": "algoliasearch-helper", | ||
"homepage": "https://github.com/algolia/algoliasearch-client-js", | ||
"homepage": "https://github.com/algolia/algoliasearch-helper-js", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Algolia Team <support@algolia.com>" |
@@ -21,4 +21,4 @@ /** | ||
*/ | ||
helper.version = "2.0.0-rc3" | ||
helper.version = "2.0.0-rc4"; | ||
module.exports = helper; |
{ | ||
"name": "algoliasearch-helper", | ||
"version": "2.0.0-rc3", | ||
"version": "2.0.0-rc4", | ||
"description": "Helper for implementing advanced search features with algolia", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,3 +34,3 @@ "use strict"; | ||
*/ | ||
AlgoliaSearchHelper.prototype.search = function(){ | ||
AlgoliaSearchHelper.prototype.search = function() { | ||
this._search(); | ||
@@ -46,4 +46,3 @@ return this; | ||
AlgoliaSearchHelper.prototype.setQuery = function( q ) { | ||
this.state = this.state.setQuery( q ) | ||
.setPage( 0 ); | ||
this.state = this.state.setQuery( q ); | ||
@@ -55,7 +54,8 @@ this.emit( "change", this.state ); | ||
/** | ||
* Remove all refinements (disjunctive + conjunctive + excludes ) | ||
* Remove all refinements (disjunctive + conjunctive + excludes + numeric filters) | ||
* @param {string} [name] - If given, name of the facet / attribute on which we want to remove all refinements | ||
* @return {AlgoliaSearchHelper} | ||
*/ | ||
AlgoliaSearchHelper.prototype.clearRefinements = function( ) { | ||
this.state = this.state.clearRefinements(); | ||
AlgoliaSearchHelper.prototype.clearRefinements = function( name ) { | ||
this.state = this.state.clearRefinements( name ); | ||
this.emit( "change", this.state ); | ||
@@ -96,7 +96,7 @@ return this; | ||
*/ | ||
AlgoliaSearchHelper.prototype.addNumericRefinement = function( attribute, operator, value ){ | ||
AlgoliaSearchHelper.prototype.addNumericRefinement = function( attribute, operator, value ) { | ||
this.state = this.state.addNumericRefinement( attribute, operator, value ); | ||
this.emit( "change", this.state ); | ||
return this; | ||
} | ||
}; | ||
@@ -110,7 +110,7 @@ /** | ||
*/ | ||
AlgoliaSearchHelper.prototype.removeNumericRefinement = function( attribute, operator, value ){ | ||
AlgoliaSearchHelper.prototype.removeNumericRefinement = function( attribute, operator, value ) { | ||
this.state = this.state.removeNumericRefinement( attribute, operator, value ); | ||
this.emit( "change", this.state ); | ||
return this; | ||
} | ||
}; | ||
@@ -191,4 +191,6 @@ /** | ||
else { | ||
/* eslint-disable */ | ||
console.log( "warning : you're trying to refine the undeclared facet '" + facet + | ||
"'; add it to the helper options 'facets' or 'disjunctiveFacets'" ); | ||
/* eslint-enable */ | ||
return this; | ||
@@ -205,3 +207,3 @@ } | ||
AlgoliaSearchHelper.prototype.nextPage = function() { | ||
return this.setPage( this.state.page + 1 ); | ||
return this.setCurrentPage( this.state.page + 1 ); | ||
}; | ||
@@ -214,3 +216,3 @@ | ||
AlgoliaSearchHelper.prototype.previousPage = function() { | ||
return this.setPage( this.state.page - 1 ); | ||
return this.setCurrentPage( this.state.page - 1 ); | ||
}; | ||
@@ -223,3 +225,3 @@ | ||
*/ | ||
AlgoliaSearchHelper.prototype.setPage = function( page ) { | ||
AlgoliaSearchHelper.prototype.setCurrentPage = function( page ) { | ||
if( page < 0 ) throw new Error( "Page requested below 0." ); | ||
@@ -247,3 +249,3 @@ | ||
*/ | ||
AlgoliaSearchHelper.prototype.setState = function( newState ){ | ||
AlgoliaSearchHelper.prototype.setState = function( newState ) { | ||
this.state = new SearchParameters( newState ); | ||
@@ -335,3 +337,2 @@ this.emit( "change", this.state ); | ||
var disjunctiveFacets = state.getRefinedDisjunctiveFacets(); | ||
var formattedResponse = new SearchResults( state, content ); | ||
@@ -361,3 +362,3 @@ | ||
if( numericFilters.length > 0 ){ | ||
if( numericFilters.length > 0 ) { | ||
additionalParams.numericFilters = numericFilters; | ||
@@ -376,3 +377,3 @@ } | ||
AlgoliaSearchHelper.prototype._getDisjunctiveFacetSearchParams = function( facet ) { | ||
var facetFilters = this._getFacetFilters( facet ) | ||
var facetFilters = this._getFacetFilters( facet ); | ||
var numericFilters = this._getNumericFilters(); | ||
@@ -389,7 +390,7 @@ var additionalParams = { | ||
if( numericFilters.length > 0 ){ | ||
if( numericFilters.length > 0 ) { | ||
additionalParams.numericFilters = numericFilters; | ||
} | ||
if( facetFilters.length > 0 ){ | ||
if( facetFilters.length > 0 ) { | ||
additionalParams.facetFilters = facetFilters; | ||
@@ -409,3 +410,3 @@ additionalParams.distinct = this.state.distinct || false; | ||
var numericFilters = []; | ||
forEach( this.state.numericRefinements, function( operators, attribute ){ | ||
forEach( this.state.numericRefinements, function( operators, attribute ) { | ||
forEach( operators, function( value, operator ) { | ||
@@ -416,3 +417,3 @@ numericFilters.push( attribute + operator + value ); | ||
return numericFilters; | ||
} | ||
}; | ||
@@ -419,0 +420,0 @@ /** |
@@ -6,2 +6,4 @@ "use strict"; | ||
var isEmpty = require( "lodash/lang/isEmpty" ); | ||
var isUndefined = require( "lodash/lang/isUndefined" ); | ||
var isString = require( "lodash/lang/isString" ); | ||
@@ -63,3 +65,3 @@ /** | ||
/** | ||
* @member {number} | ||
* @member {number} | ||
**/ | ||
@@ -108,8 +110,16 @@ this.maxValuesPerFacet = params.maxValuesPerFacet || 10; | ||
constructor : SearchParameters, | ||
clearRefinements : function clearRefinements() { | ||
/** | ||
* Remove all refinements (disjunctive + conjunctive + excludes + numeric filters) | ||
* @method | ||
* @param {string} [name] - If given, name of the facet / attribute on which we want to remove all refinements | ||
* @return {AlgoliaSearchHelper} | ||
*/ | ||
clearRefinements : function clearRefinements( name ) { | ||
return this.mutateMe( function( m ) { | ||
m.facetsRefinements = {}; | ||
m.facetsExcludes = {}; | ||
m.disjunctiveFacetsRefinements = {}; | ||
m.numericRefinements = {}; | ||
m.page = 0; | ||
m._clearNumericRefinements( name ); | ||
m._clearFacetRefinements( name ); | ||
m._clearExcludeRefinements( name ); | ||
m._clearDisjunctiveFacetRefinements( name ); | ||
} ); | ||
@@ -124,4 +134,5 @@ }, | ||
setQuery : function setQuery( newQuery ) { | ||
return this.mutateMe( function( newState ) { | ||
newState.query = newQuery; | ||
return this.mutateMe( function( m ) { | ||
m.query = newQuery; | ||
m.page = 0; | ||
} ); | ||
@@ -174,2 +185,3 @@ }, | ||
m.HitsPerPage = n; | ||
m.page = 0; | ||
} ); | ||
@@ -179,2 +191,5 @@ }, | ||
* Add or update a numeric filter for a given attribute | ||
* Current limitation of the numeric filters : you can't have more than one value | ||
* filtered for each (attribute, oprator). It means that you can't have a filter | ||
* for ( "attribute", "=", 3 ) and ( "attribute", "=", 8 ) | ||
* @method | ||
@@ -186,4 +201,5 @@ * @param {string} attribute attribute to set the filter on | ||
addNumericRefinement : function( attribute, operator, value ) { | ||
return this.mutateMe( function( m ){ | ||
if( !m.numericRefinements[ attribute ] ){ | ||
return this.mutateMe( function( m ) { | ||
m.page = 0; | ||
if( !m.numericRefinements[ attribute ] ) { | ||
m.numericRefinements[ attribute ] = {}; | ||
@@ -199,11 +215,11 @@ } | ||
* @param {string} operator operator of the filter ( possible values : =, >, >=, <, <=, != ) | ||
* @param {number} value value of the filter | ||
*/ | ||
removeNumericRefinement : function( attribute, operator, value ) { | ||
removeNumericRefinement : function( attribute, operator ) { | ||
return this.mutateMe( function( m ) { | ||
if( m.numericRefinements[ attribute ] ) { | ||
m.page = 0; | ||
var value = m.numericRefinements[ attribute ][ operator ]; | ||
if( value ) { | ||
if( !isUndefined( value ) ) { | ||
delete m.numericRefinements[ attribute ][ operator ]; | ||
if( isEmpty( m.numericRefinements[ attribute ] ) ){ | ||
if( isEmpty( m.numericRefinements[ attribute ] ) ) { | ||
delete m.numericRefinements[ attribute ]; | ||
@@ -216,2 +232,20 @@ } | ||
/** | ||
* Clear numeric filters. | ||
* @method | ||
* @private | ||
* @param {string} [attribute] - | ||
* - If not given, means to clear all the filters. | ||
* - If `string`, means to clear all refinements for the `attribute` named filter. | ||
*/ | ||
_clearNumericRefinements : function _clearNumericRefinements( attribute ) { | ||
if ( isUndefined( attribute ) ) { | ||
this.numericRefinements = {}; | ||
} | ||
else if ( isString( attribute ) ) { | ||
if ( !isUndefined( this.numericRefinements[ attribute ] ) ) { | ||
delete this.numericRefinements[ attribute ]; | ||
} | ||
} | ||
}, | ||
/** | ||
* Add a refinement on a "normal" facet | ||
@@ -225,2 +259,3 @@ * @method | ||
return this.mutateMe( function( m ) { | ||
m.page = 0; | ||
m.facetsRefinements[ facet ] = value; | ||
@@ -236,4 +271,5 @@ } ); | ||
*/ | ||
addExcludeRefinement : function addExcludedValue( facet, value ) { | ||
addExcludeRefinement : function addExcludeRefinement( facet, value ) { | ||
return this.mutateMe( function( m ) { | ||
m.page = 0; | ||
if( !m.facetsExcludes[ facet ] ) { | ||
@@ -254,2 +290,3 @@ m.facetsExcludes[ facet ] = []; | ||
return this.mutateMe( function( m ) { | ||
m.page = 0; | ||
if( !m.disjunctiveFacetsRefinements[ facet ] ) { | ||
@@ -269,3 +306,4 @@ m.disjunctiveFacetsRefinements[ facet ] = []; | ||
return this.mutateMe( function( m ) { | ||
delete m.facetsRefinements[ facet ]; | ||
m.page = 0; | ||
m._clearFacetRefinements( facet ); | ||
} ); | ||
@@ -283,6 +321,7 @@ }, | ||
if( m.facetsExcludes[ facet ] ) { | ||
m.page = 0; | ||
var idx = m.facetsExcludes[ facet ].indexOf( value ); | ||
if( idx > -1 ){ | ||
if( idx > -1 ) { | ||
m.facetsExcludes[ facet ].splice( idx, 1 ); | ||
if( m.facetsExcludes[ facet ].length === 0 ){ | ||
if( m.facetsExcludes[ facet ].length === 0 ) { | ||
delete m.facetsExcludes[ facet ]; | ||
@@ -304,6 +343,7 @@ } | ||
if( m.disjunctiveFacetsRefinements[ facet ] ) { | ||
m.page = 0; | ||
var idx = m.disjunctiveFacetsRefinements[ facet ].indexOf( value ); | ||
if( idx > -1 ){ | ||
if( idx > -1 ) { | ||
m.disjunctiveFacetsRefinements[ facet ].splice( idx, 1 ); | ||
if( m.disjunctiveFacetsRefinements[facet].length === 0 ){ | ||
if( m.disjunctiveFacetsRefinements[facet].length === 0 ) { | ||
delete m.disjunctiveFacetsRefinements[ facet ]; | ||
@@ -316,2 +356,56 @@ } | ||
/** | ||
* Clear the facet refinements | ||
* @method | ||
* @private | ||
* @param {string} [facet] - | ||
* - If not given, means to clear the refinement of all facets. | ||
* - If `string`, means to clear the refinement for the `facet` named facet. | ||
*/ | ||
_clearFacetRefinements : function _clearFacetRefinements( facet ) { | ||
if ( isUndefined( facet ) ) { | ||
this.facetsRefinements = {}; | ||
} | ||
else if ( isString( facet ) ) { | ||
if ( !isUndefined( this.facetsRefinements[ facet ] ) ) { | ||
delete this.facetsRefinements[ facet ]; | ||
} | ||
} | ||
}, | ||
/** | ||
* Clear the exclude refinements | ||
* @method | ||
* @private | ||
* @param {string} [facet] - | ||
* - If not given, means to clear all the excludes of all facets. | ||
* - If `string`, means to clear all the excludes for the `facet` named facet. | ||
*/ | ||
_clearExcludeRefinements : function _clearExcludeRefinements( facet ) { | ||
if ( isUndefined( facet ) ) { | ||
this.facetsExcludes = {}; | ||
} | ||
else if ( isString( facet ) ) { | ||
if ( !isUndefined( this.facetsExcludes[ facet ] ) ) { | ||
delete this.facetsExcludes[ facet ]; | ||
} | ||
} | ||
}, | ||
/** | ||
* Clear the disjunctive refinements | ||
* @method | ||
* @private | ||
* @param {string} [facet] - | ||
* - If not given, means to clear all the refinements of all disjunctive facets. | ||
* - If `string`, means to clear all the refinements for the `facet` named facet. | ||
*/ | ||
_clearDisjunctiveFacetRefinements : function _clearDisjunctiveFacetRefinements( facet ) { | ||
if ( isUndefined( facet ) ) { | ||
this.disjunctiveFacetsRefinements = {}; | ||
} | ||
else if ( isString( facet ) ) { | ||
if ( !isUndefined( this.disjunctiveFacetsRefinements[ facet ] ) ) { | ||
delete this.disjunctiveFacetsRefinements[ facet ]; | ||
} | ||
} | ||
}, | ||
/** | ||
* Switch the refinement applied over a facet/value | ||
@@ -421,13 +515,13 @@ * @method | ||
"facetsExcludes", "disjunctiveFacetsRefinements", | ||
"numericRefinements" | ||
"numericRefinements" | ||
], | ||
getQueryParams : function getQueryParams(){ | ||
getQueryParams : function getQueryParams() { | ||
var managedParameters = this.managedParameters; | ||
return reduce( this, function( memo, value, parameter, parameters) { | ||
return reduce( this, function( memo, value, parameter, parameters ) { | ||
if( managedParameters.indexOf( parameter ) === -1 && | ||
parameters[ parameter ] !== undefined ){ | ||
parameters[ parameter ] !== undefined ) { | ||
memo[ parameter ] = value; | ||
}; | ||
} | ||
return memo; | ||
}, {}); | ||
}, {} ); | ||
}, | ||
@@ -434,0 +528,0 @@ /** |
@@ -0,4 +1,23 @@ | ||
"use strict"; | ||
var forEach = require( "lodash/collection/forEach" ); | ||
var compact = require( "lodash/array/compact" ); | ||
function getIndices( obj ) { | ||
var indices = {}; | ||
forEach( obj, function( val, idx ) { indices[ val ] = idx; } ); | ||
return indices; | ||
} | ||
function assignFacetStats( dest, facetStats, key ) { | ||
if ( facetStats && facetStats[key] ) { | ||
dest.stats = facetStats[key]; | ||
} | ||
} | ||
function assignFacetTimeout( dest, timeoutCounts, getRankingInfo ) { | ||
if ( getRankingInfo ) { | ||
dest.timeout = !!( timeoutCounts ); | ||
} | ||
} | ||
/** | ||
@@ -74,4 +93,6 @@ * Constructor for SearchResults | ||
forEach( mainSubResponse.facets, function( facetValueObject, facetKey ) { | ||
if( state.disjunctiveFacets.indexOf( facetKey ) !== -1 ) { | ||
var position = disjunctiveFacetsIndices[ facetKey ]; | ||
var isFacetDisjunctive = state.disjunctiveFacets.indexOf( facetKey ) !== -1; | ||
var position = isFacetDisjunctive ? disjunctiveFacetsIndices[ facetKey ] : | ||
facetsIndices[ facetKey ]; | ||
if( isFacetDisjunctive ) { | ||
this.disjunctiveFacets[ position ] = { | ||
@@ -82,6 +103,8 @@ name : facetKey, | ||
assignFacetStats( this.disjunctiveFacets[ position ], mainSubResponse.facets_stats, facetKey ); | ||
assignFacetTimeout( this.disjunctiveFacets[ position ], state.getRankingInfo, mainSubResponse.timeoutCounts, facetKey); | ||
assignFacetTimeout( this.disjunctiveFacets[ position ], | ||
state.getRankingInfo, | ||
mainSubResponse.timeoutCounts, | ||
facetKey ); | ||
} | ||
else { | ||
var position = facetsIndices[ facetKey ]; | ||
this.facets[ position ] = { | ||
@@ -92,3 +115,3 @@ name : facetKey, | ||
assignFacetStats( this.facets[ position ], mainSubResponse.facets_stats, facetKey ); | ||
assignFacetTimeout( this.facets[ position ], state.getRankingInfo, mainSubResponse.timeoutCounts, facetKey); | ||
assignFacetTimeout( this.facets[ position ], state.getRankingInfo, mainSubResponse.timeoutCounts, facetKey ); | ||
} | ||
@@ -102,3 +125,3 @@ }, this ); | ||
// There should be only item in facets. | ||
forEach( result.facets, function( facetResults, dfacet ){ | ||
forEach( result.facets, function( facetResults, dfacet ) { | ||
var position = disjunctiveFacetsIndices[ dfacet ]; | ||
@@ -111,9 +134,9 @@ | ||
assignFacetStats( this.disjunctiveFacets[ position ], result.facets_stats, dfacet ); | ||
assignFacetTimeout( this.disjunctiveFacets[ position ], state.getRankingInfo, result.timeoutCounts, dfacet); | ||
assignFacetTimeout( this.disjunctiveFacets[ position ], state.getRankingInfo, result.timeoutCounts, dfacet ); | ||
if ( state.disjunctiveFacetsRefinements[dfacet] ) { | ||
forEach( state.disjunctiveFacetsRefinements[ dfacet ], function( refinementValue ){ | ||
forEach( state.disjunctiveFacetsRefinements[ dfacet ], function( refinementValue ) { | ||
// add the disjunctive refinements if it is no more retrieved | ||
if ( !this.disjunctiveFacets[position].data[refinementValue] && | ||
state.disjunctiveFacetsRefinements[dfacet].indexOf(refinementValue) > -1 ) { | ||
state.disjunctiveFacetsRefinements[dfacet].indexOf( refinementValue ) > -1 ) { | ||
this.disjunctiveFacets[position].data[refinementValue] = 0; | ||
@@ -144,20 +167,2 @@ } | ||
function getIndices( obj ){ | ||
var indices = {}; | ||
forEach( obj, function( val, idx ){ indices[ val ] = idx; } ); | ||
return indices; | ||
} | ||
function assignFacetStats( dest, facets_stats, key ) { | ||
if ( facets_stats && facets_stats[key] ) { | ||
dest.stats = facets_stats[key]; | ||
} | ||
} | ||
function assignFacetTimeout( dest, timeoutCounts, getRankingInfo ) { | ||
if ( getRankingInfo ) { | ||
dest.timeout = !!( timeoutCounts ); | ||
} | ||
} | ||
module.exports = SearchResults; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1217312
1708