Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 1x 1x 1x 6x 6x 6x 6x 1x 1x 1x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x | import { getUnitPrice } from '@/helpers/price';
import { sortablePurityGrade } from '@/helpers/science';
import type { Row } from '@tanstack/react-table';
/**
* @category Tanstack Sorting Functions
* @showCategories
* @categoryDescription Scientific formula parsing and chemical notation utilities.
* @source
*/
/**
* TanStack sorting comparator that orders two product rows by their normalized
* `baseQuantity` (a missing quantity sorts as 0).
* @category Tanstack Sorting Functions
* @param rowA - The first row to compare.
* @param rowB - The second row to compare.
* @returns `1` if rowA ranks after rowB, `-1` if before, `0` if equal.
* @example
* ```ts
* useReactTable({ sortingFns: { quantity: quantitySortingFn }, ... });
* // A row with baseQuantity 500 sorts after one with 100.
* ```
* @source
*/
export function quantitySortingFn(rowA: Row<Product>, rowB: Row<Product>) {
const a = rowA.original.baseQuantity ?? 0;
const b = rowB.original.baseQuantity ?? 0;
return a > b ? 1 : a < b ? -1 : 0;
}
/**
* Custom sorting function for match percentage comparison between two product rows.
* Compares the match percentage of products and returns a sort order value.
* @category Tanstack Sorting Functions
* @param rowA - The first row to compare.
* @param rowB - The second row to compare.
* @returns `1` if rowA ranks after rowB, `-1` if before, `0` if equal.
* @example
* ```ts
* useReactTable({ sortingFns: { match: matchPercentageSortingFn }, ... });
* // A row with matchPercentage 90 sorts after one with 70.
* ```
* @source
*/
export function matchPercentageSortingFn(rowA: Row<Product>, rowB: Row<Product>) {
const a = rowA.original.matchPercentage ?? 0;
const b = rowB.original.matchPercentage ?? 0;
return a > b ? 1 : a < b ? -1 : 0;
}
/**
* Custom sorting function for price comparison between two product rows.
* Compares the USD prices of products (falling back to raw price) and returns
* a sort order value.
* @category Tanstack Sorting Functions
* @param rowA - The first row to compare.
* @param rowB - The second row to compare.
* @returns `1` if rowA ranks after rowB, `-1` if before, `0` if equal.
* @example
* ```ts
* useReactTable({ sortingFns: { price: priceSortingFn }, ... });
* // A row with usdPrice 29.99 sorts after one with 9.99.
* ```
* @source
*/
export function priceSortingFn(rowA: Row<Product>, rowB: Row<Product>) {
const a = rowA.original.usdPrice ?? rowA.original.price ?? 0;
const b = rowB.original.usdPrice ?? rowB.original.price ?? 0;
return a > b ? 1 : a < b ? -1 : 0;
}
/**
* TanStack sorting comparator that orders two product rows by their price per
* base unit ({@link getUnitPrice}). Rows whose unit price can't be computed
* (missing price/quantity or an unconvertible unit) sort to the bottom rather
* than appearing as the cheapest.
* @category Tanstack Sorting Functions
* @param rowA - The first row to compare.
* @param rowB - The second row to compare.
* @returns `1` if rowA ranks after rowB, `-1` if before, `0` if equal.
* @example
* ```ts
* useReactTable({ sortingFns: { unitPriceSortingFn }, ... });
* // A row at $0.10/g sorts after one at $0.02/g.
* ```
* @source
*/
export function unitPriceSortingFn(rowA: Row<Product>, rowB: Row<Product>) {
const a = getUnitPrice(rowA.original) ?? Number.POSITIVE_INFINITY;
const b = getUnitPrice(rowB.original) ?? Number.POSITIVE_INFINITY;
return a > b ? 1 : a < b ? -1 : 0;
}
/**
* TanStack sorting comparator that orders two product rows by purity. The column mixes two
* kinds of value — a chemical grade (`"ACS Grade"`) and a percentage (`"≥99.8%"`) — so both
* are put on one numeric scale by {@link sortablePurityGrade}. Reads `grade ?? purity`, the
* same precedence the purity column's accessor uses, so the sort order matches what the cell
* shows. A row with neither (or an unrecognized grade, e.g. `"Ungraded"`) sorts as 0.
* @category Tanstack Sorting Functions
* @param rowA - The first row to compare.
* @param rowB - The second row to compare.
* @returns `1` if rowA ranks after rowB, `-1` if before, `0` if equal.
* @example
* ```ts
* useReactTable({ sortingFns: { puritySortingFn }, ... });
* // "ACS Grade" (99.8) sorts after "Technical Grade" (90), which sorts after "95%".
* ```
* @source
*/
export function puritySortingFn(rowA: Row<Product>, rowB: Row<Product>) {
const a = sortablePurityGrade(rowA.original.grade ?? rowA.original.purity ?? '');
const b = sortablePurityGrade(rowB.original.grade ?? rowB.original.purity ?? '');
return a > b ? 1 : a < b ? -1 : 0;
}
|