Skip to content

Instantly share code, notes, and snippets.

@Haroenv
Last active March 22, 2019 09:35
Show Gist options
  • Save Haroenv/ee02e438e2086c0f0f1e1584cc430fde to your computer and use it in GitHub Desktop.
Save Haroenv/ee02e438e2086c0f0f1e1584cc430fde to your computer and use it in GitHub Desktop.
Which version do you prefer? (find is lodash.find for IE11)
function getHierarchicalRefinement(state, attributeName, name, resultsFacets) {
var facet = find(resultsFacets, {name: attributeName});
var facetDeclaration = state.getHierarchicalFacetByName(attributeName);
var splitted = name.split(facetDeclaration.separator);
var configuredName = splitted[splitted.length - 1];
for (var i = 0; facet !== undefined && i < splitted.length; ++i) {
facet = find(facet.data, {name: splitted[i]});
}
var count = get(facet, 'count');
var exhaustive = get(facet, 'exhaustive');
return {
type: 'hierarchical',
attributeName: attributeName,
name: configuredName,
count: count || 0,
exhaustive: exhaustive || false
};
}
function getHierarchicalRefinement(state, attributeName, name, resultsFacets) {
var facetDeclaration = state.getHierarchicalFacetByName(attributeName);
var separator = state._getHierarchicalFacetSeparator(facetDeclaration);
var split = name.split(separator);
var configuredName = split[split.length - 1];
var rootFacet = find(resultsFacets, {name: attributeName});
var facet = split.reduce(function(intermediateFacet, part) {
var newFacet =
intermediateFacet && find(intermediateFacet.data, {name: part});
return newFacet !== undefined ? newFacet : intermediateFacet;
}, rootFacet);
return {
type: 'hierarchical',
attributeName: attributeName,
name: configuredName,
count: (facet && facet.count) || 0,
exhaustive: (facet && facet.exhaustive) || false
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment