UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

52 lines (47 loc) 1.15 kB
/** * MSKCC 2021, 2024 */ /** * 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 }); }); }; export { defaultCompareItems, defaultSortItems };