@hypothesis/frontend-shared
Advanced tools
Comparing version 7.7.0 to 7.7.1
@@ -5,3 +5,5 @@ import type { Order } from '../types'; | ||
* 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 declare function useOrderedRows<Row>(rows: Row[], order?: Order<keyof Row>): Row[]; |
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. | ||
*/ | ||
@@ -11,10 +17,15 @@ export function useOrderedRows(rows, order) { | ||
} | ||
return [...rows].sort((a, b) => { | ||
if (a[order.field] === b[order.field]) { | ||
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; | ||
} | ||
if (order.direction === 'ascending') { | ||
return a[order.field] > b[order.field] ? 1 : -1; | ||
} | ||
return a[order.field] > b[order.field] ? -1 : 1; | ||
return x > y ? 1 : -1; | ||
}); | ||
@@ -21,0 +32,0 @@ }, [order, rows]); |
{ | ||
"name": "@hypothesis/frontend-shared", | ||
"version": "7.7.0", | ||
"version": "7.7.1", | ||
"description": "Shared components, styles and utilities for Hypothesis projects", | ||
@@ -5,0 +5,0 @@ "license": "BSD-2-Clause", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3162698
41063