algoliasearch-helper 3.4.5 → 3.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG CHANGED
@@ -1,3 +1,15 @@
1
+ 3.5.3 - 2021-06-14
2
+ * fix(ts): correct required for getFacetValues https://github.com/algolia/algoliasearch-helper-js/commit/55a909fe1ef892b3e76ddc109155336c77875292
3
+
4
+ 3.5.2 - 2021-06-14
5
+ * fix(facetOrdering): hierarchical attributes sort by path https://github.com/algolia/algoliasearch-helper-js/commit/c1d9764b7ed9492356a8b9ccfdb980fe0361c46f
6
+
7
+ 3.5.1 - 2021-06-14
8
+ * fix(ts): correctly optional renderingContent https://github.com/algolia/algoliasearch-helper-js/commit/41d27f8336f48dd2eaec53afafcceb81a58d0dfb
9
+
10
+ 3.5.0 - 2021-06-14
11
+ * feat(getFacetValues): process facetOrdering (#822) https://github.com/algolia/algoliasearch-helper-js/commit/8c7ff444407cc7855c4d76b15954f4a6864d0b5d
12
+
1
13
  3.4.5 - 2021-06-10
2
14
  * feat(ts): document renderingContent (#823) https://github.com/algolia/algoliasearch-helper-js/commit/7b176a7bbc38de193ac1c6a34dacde63059e4b3b
3
15
 
@@ -2931,20 +2931,29 @@ function extractNormalizedFacetValues(results, attribute) {
2931
2931
  }
2932
2932
 
2933
2933
  /**
2934
- * Sort nodes of a hierarchical facet results
2934
+ * Sort nodes of a hierarchical or disjunctive facet results
2935
2935
  * @private
2936
- * @param {HierarchicalFacet} node node to upon which we want to apply the sort
2936
+ * @param {function} sortFn
2937
+ * @param {HierarchicalFacet|Array} node node upon which we want to apply the sort
2938
+ * @param {string[]} names attribute names
2939
+ * @param {number} [level=0] current index in the names array
2937
2940
  */
2938
- function recSort(sortFn, node) {
2941
+ function recSort(sortFn, node, names, level) {
2942
+ level = level || 0;
2943
+
2944
+ if (Array.isArray(node)) {
2945
+ return sortFn(node, names[level]);
2946
+ }
2947
+
2939
2948
  if (!node.data || node.data.length === 0) {
2940
2949
  return node;
2941
2950
  }
2942
2951
 
2943
2952
  var children = node.data.map(function(childNode) {
2944
- return recSort(sortFn, childNode);
2953
+ return recSort(sortFn, childNode, names, level + 1);
2945
2954
  });
2946
- var sortedChildren = sortFn(children);
2947
- var newNode = merge({}, node, {data: sortedChildren});
2955
+ var sortedChildren = sortFn(children, names[level]);
2956
+ var newNode = defaultsPure({data: sortedChildren}, node);
2948
2957
  return newNode;
2949
2958
  }
2950
2959
 
@@ -2954,6 +2963,72 @@ function vanillaSortFn(order, data) {
2954
2963
  return data.sort(order);
2955
2964
  }
2956
2965
 
2966
+ /**
2967
+ * @typedef FacetOrdering
2968
+ * @type {Object}
2969
+ * @property {string[]} [order]
2970
+ * @property {'count' | 'alpha' | 'hidden'} [sortRemainingBy]
2971
+ */
2972
+
2973
+ /**
2974
+ * Sorts facet arrays via their facet ordering
2975
+ * @param {Array} facetValues the values
2976
+ * @param {FacetOrdering} facetOrdering the ordering
2977
+ * @returns {Array}
2978
+ */
2979
+ function sortViaFacetOrdering(facetValues, facetOrdering) {
2980
+ var orderedFacets = [];
2981
+ var remainingFacets = [];
2982
+
2983
+ var order = facetOrdering.order || [];
2984
+ /**
2985
+ * an object with the keys being the values in order, the values their index:
2986
+ * ['one', 'two'] -> { one: 0, two: 1 }
2987
+ */
2988
+ var reverseOrder = order.reduce(function(acc, name, i) {
2989
+ acc[name] = i;
2990
+ return acc;
2991
+ }, {});
2992
+
2993
+ facetValues.forEach(function(item) {
2994
+ // hierarchical facets get sorted using their raw name
2995
+ var name = item.path || item.name;
2996
+ if (reverseOrder[name] !== undefined) {
2997
+ orderedFacets[reverseOrder[name]] = item;
2998
+ } else {
2999
+ remainingFacets.push(item);
3000
+ }
3001
+ });
3002
+
3003
+ var sortRemainingBy = facetOrdering.sortRemainingBy;
3004
+ var ordering;
3005
+ if (sortRemainingBy === 'hidden') {
3006
+ return orderedFacets;
3007
+ } else if (sortRemainingBy === 'alpha') {
3008
+ ordering = [['path', 'name'], ['asc', 'asc']];
3009
+ } else {
3010
+ ordering = [['count'], ['desc']];
3011
+ }
3012
+
3013
+ return orderedFacets.concat(
3014
+ orderBy(remainingFacets, ordering[0], ordering[1])
3015
+ );
3016
+ }
3017
+
3018
+ /**
3019
+ * @param {SearchResults} results the search results class
3020
+ * @param {string} attribute the attribute to retrieve ordering of
3021
+ * @returns {FacetOrdering=}
3022
+ */
3023
+ function getFacetOrdering(results, attribute) {
3024
+ return (
3025
+ results.renderingContent &&
3026
+ results.renderingContent.facetOrdering &&
3027
+ results.renderingContent.facetOrdering.values &&
3028
+ results.renderingContent.facetOrdering.values[attribute]
3029
+ );
3030
+ }
3031
+
2957
3032
  /**
2958
3033
  * Get a the list of values for a given facet attribute. Those values are sorted
2959
3034
  * refinement first, descending count (bigger value on top), and name ascending
@@ -2966,6 +3041,9 @@ function vanillaSortFn(order, data) {
2966
3041
  * might not be respected if you have facet values that are already refined.
2967
3042
  * @param {string} attribute attribute name
2968
3043
  * @param {object} opts configuration options.
3044
+ * @param {boolean} [opts.facetOrdering]
3045
+ * Force the use of facetOrdering from the result if a sortBy is present. If
3046
+ * sortBy isn't present, facetOrdering will be used automatically.
2969
3047
  * @param {Array.<string> | function} opts.sortBy
2970
3048
  * When using strings, it consists of
2971
3049
  * the name of the [FacetValue](#SearchResults.FacetValue) or the
@@ -3005,30 +3083,41 @@ SearchResults.prototype.getFacetValues = function(attribute, opts) {
3005
3083
  return undefined;
3006
3084
  }
3007
3085
 
3008
- var options = defaultsPure({}, opts, {sortBy: SearchResults.DEFAULT_SORT});
3086
+ var options = defaultsPure({}, opts, {
3087
+ sortBy: SearchResults.DEFAULT_SORT,
3088
+ // if no sortBy is given, attempt to sort based on facetOrdering
3089
+ // if it is given, we still allow to sort via facet ordering first
3090
+ facetOrdering: !(opts && opts.sortBy)
3091
+ });
3009
3092
 
3010
- if (Array.isArray(options.sortBy)) {
3011
- var order = formatSort(options.sortBy, SearchResults.DEFAULT_SORT);
3012
- if (Array.isArray(facetValues)) {
3013
- return orderBy(facetValues, order[0], order[1]);
3014
- }
3015
- // If facetValues is not an array, it's an object thus a hierarchical facet object
3016
- return recSort(function(hierarchicalFacetValues) {
3017
- return orderBy(hierarchicalFacetValues, order[0], order[1]);
3018
- }, facetValues);
3019
- } else if (typeof options.sortBy === 'function') {
3020
- if (Array.isArray(facetValues)) {
3021
- return facetValues.sort(options.sortBy);
3093
+ var results = this;
3094
+ var attributes;
3095
+ if (Array.isArray(facetValues)) {
3096
+ attributes = [attribute];
3097
+ } else {
3098
+ var config = results._state.getHierarchicalFacetByName(facetValues.name);
3099
+ attributes = config.attributes;
3100
+ }
3101
+
3102
+ return recSort(function(data, facetName) {
3103
+ if (options.facetOrdering) {
3104
+ var facetOrdering = getFacetOrdering(results, facetName);
3105
+ if (Boolean(facetOrdering)) {
3106
+ return sortViaFacetOrdering(data, facetOrdering);
3107
+ }
3022
3108
  }
3023
- // If facetValues is not an array, it's an object thus a hierarchical facet object
3024
- return recSort(function(data) {
3109
+
3110
+ if (Array.isArray(options.sortBy)) {
3111
+ var order = formatSort(options.sortBy, SearchResults.DEFAULT_SORT);
3112
+ return orderBy(data, order[0], order[1]);
3113
+ } else if (typeof options.sortBy === 'function') {
3025
3114
  return vanillaSortFn(options.sortBy, data);
3026
- }, facetValues);
3027
- }
3028
- throw new Error(
3029
- 'options.sortBy is optional but if defined it must be ' +
3030
- 'either an array of string (predicates) or a sorting function'
3031
- );
3115
+ }
3116
+ throw new Error(
3117
+ 'options.sortBy is optional but if defined it must be ' +
3118
+ 'either an array of string (predicates) or a sorting function'
3119
+ );
3120
+ }, facetValues, attributes);
3032
3121
  };
3033
3122
 
3034
3123
  /**
@@ -5340,7 +5429,7 @@ module.exports = function isValidUserToken(userToken) {
5340
5429
  },{}],23:[function(require,module,exports){
5341
5430
  'use strict';
5342
5431
 
5343
- module.exports = '3.4.5';
5432
+ module.exports = '3.5.3';
5344
5433
 
5345
5434
  },{}]},{},[1])(1)
5346
5435
  });