UNPKG

@hypothesis/frontend-shared

Version:

Shared components, styles and utilities for Hypothesis projects

47 lines (44 loc) 1.27 kB
import { useMemo } from 'preact/hooks'; const collator = new Intl.Collator(undefined, { sensitivity: 'case' }); /** * Orders a list of rows based on provided order options. * Provided rows are not mutated, but a copy is returned instead. * Strings are compared using `Intl.Collator`, other types are compared using * standard JavaScript comparisons. */ export function useOrderedRows(rows, order) { return useMemo(() => { if (!order) { return rows; } // Order nulls last by default const { nullsLast = true } = order; return [...rows].sort(({ [order.field]: a }, { [order.field]: b }) => { const [x, y] = order.direction === 'ascending' ? [a, b] : [b, a]; if (typeof x === 'string' && typeof y === 'string') { return collator.compare(x, y); } if (x === y) { return 0; } // We check a/b instead of x/y because nulls should not be affected by the // regular order direction. if (a === null || a === undefined) { return nullsLast ? 1 : -1; } if (b === null || b === undefined) { return nullsLast ? -1 : 1; } return x > y ? 1 : -1; }); }, [order, rows]); } //# sourceMappingURL=use-ordered-rows.js.map