@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
57 lines (50 loc) • 1.27 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Use the local `localCompare` with the `numeric` option to sort two,
* potentially alpha-numeric, strings in a list of items.
*
* @param {string} itemA
* @param {string} itemB
* @param {object} options
* @param {string} options.locale
* @returns {number}
*/
const defaultCompareItems = (itemA, itemB, _ref) => {
let {
locale
} = _ref;
return itemA.localeCompare(itemB, locale, {
numeric: true
});
};
/**
* Default sorting algorithm for options in a selection control
*/
const defaultSortItems = (items, _ref2) => {
let {
selectedItems = [],
itemToString,
compareItems,
locale = 'en'
} = _ref2;
return items.sort((itemA, itemB) => {
const hasItemA = selectedItems.includes(itemA);
const hasItemB = selectedItems.includes(itemB);
// Prefer whichever item is in the `selectedItems` array first
if (hasItemA && !hasItemB) {
return -1;
}
if (hasItemB && !hasItemA) {
return 1;
}
return compareItems(itemToString(itemA), itemToString(itemB), {
locale
});
});
};
exports.defaultCompareItems = defaultCompareItems;
exports.defaultSortItems = defaultSortItems;