Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
refactor(menu): remove orderBy & use helper method
Browse files Browse the repository at this point in the history
These tests provide no security at all, but this change in behaviour can be confirmed in the stories.
  • Loading branch information
Haroenv committed May 24, 2019
1 parent af37a12 commit adbe458
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,16 @@ describe('connectMenu', () => {
};
results.getFacetValues.mockClear();
results.getFacetValues.mockImplementation(() => [
{
name: 'wat',
isRefined: false,
count: 20,
},
{
name: 'oy',
isRefined: true,
count: 10,
},
{
name: 'wat',
isRefined: false,
count: 20,
},
]);

props = connect.getProvidedProps(
Expand Down
113 changes: 16 additions & 97 deletions packages/react-instantsearch-core/src/connectors/connectMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,82 +30,6 @@ function getCurrentRefinement(props, searchState, context) {
);
}

function compareAscending(value, other) {
if (value !== other) {
const valIsDefined = value !== undefined;
const valIsNull = value === null;

const othIsDefined = other !== undefined;
const othIsNull = other === null;

if (
(!othIsNull && value > other) ||
(valIsNull && othIsDefined) ||
!valIsDefined
) {
return 1;
}
if (
(!valIsNull && value < other) ||
(othIsNull && valIsDefined) ||
!othIsDefined
) {
return -1;
}
}
return 0;
}

/**
* orderBy utility, same as lodash.orderBy, implementation taken from
* https://github.com/algolia/algoliasearch-helper-js/blob/2472e4c1b5f76b990a032104a08e90e76996032f/src/functions/orderBy.js
*
* @param {Array<object>} collection object with keys in attributes
* @param {Array<string>} iteratees attributes
* @param {Array<string>} orders asc | desc
*/
function orderBy(collection, iteratees, orders) {
if (!Array.isArray(collection)) {
return [];
}

if (!Array.isArray(orders)) {
orders = [];
}

const result = collection.map((value, index) => ({
criteria: iteratees.map(iteratee => value[iteratee]),
index,
value,
}));

result.sort(function comparer(object, other) {
let index = -1;

while (++index < object.criteria.length) {
const res = compareAscending(
object.criteria[index],
other.criteria[index]
);
if (res) {
if (index >= orders.length) {
return res;
}
if (orders[index] === 'desc') {
return -res;
}
return res;
}
}

// This ensures a stable sort in V8 and other engines.
// See https://bugs.chromium.org/p/v8/issues/detail?id=90 for more details.
return object.index - other.index;
});

return result.map(res => res.value);
}

function getValue(name, props, searchState, context) {
const currentRefinement = getCurrentRefinement(props, searchState, context);
return name === currentRefinement ? '' : name;
Expand All @@ -126,7 +50,7 @@ function cleanUp(props, searchState, context) {
return cleanUpValue(searchState, context, `${namespace}.${getId(props)}`);
}

const sortBy = ['count:desc', 'name:asc'];
const defaultSortBy = ['count:desc', 'name:asc'];

/**
* connectMenu connector provides the logic to build a widget that will
Expand Down Expand Up @@ -221,28 +145,23 @@ export default createConnector({
count: v.count,
isRefined: v.isRefined,
}))
: results.getFacetValues(attribute, { sortBy }).map(v => ({
label: v.name,
value: getValue(v.name, props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
}),
count: v.count,
isRefined: v.isRefined,
}));

const sortedItems =
searchable && !isFromSearch
? orderBy(
items,
['isRefined', 'count', 'label'],
['desc', 'desc', 'asc']
)
: items;
: results
.getFacetValues(attribute, {
sortBy: searchable ? undefined : defaultSortBy,
})
.map(v => ({
label: v.name,
value: getValue(v.name, props, searchState, {
ais: props.contextValue,
multiIndexContext: props.indexContextValue,
}),
count: v.count,
isRefined: v.isRefined,
}));

const transformedItems = props.transformItems
? props.transformItems(sortedItems)
: sortedItems;
? props.transformItems(items)
: items;

return {
items: transformedItems.slice(0, getLimit(props)),
Expand Down

0 comments on commit adbe458

Please sign in to comment.