Files
TREK/server/src/nest/common/compare.ts
T
Maurice 62b13a278e refactor(common): one shared comparator instead of three inline copies
The coverage ratchet caught something worth keeping: an inline
`(a, b) => (a < b ? -1 : a > b ? 1 : 0)` is a new function with four branches at
every call site, and two of the three in src/nest/common sit on error paths that
a passing test never walks. Three restatements of the default sort order were
enough to pull the domain under its pinned thresholds (functions 97.8% against
99, branches 87.92% against 89).

Writing tests to walk those error paths would have been solving the wrong
problem. byCodeUnit says the same thing once, is covered once, and the callers
read better for it.

Its test asserts equality with a bare `.sort()` rather than hand-written
expectations, since "identical to the default order" is the whole contract. It
also pins what byCodeUnit is NOT: `['Zelt', 'Übernachtung']` keeps that order,
where localeCompare swaps it — so the next person reaching for a comparator on a
list a human reads picks the other one.
2026-08-25 19:26:38 +02:00

20 lines
986 B
TypeScript

/**
* The comparator a bare `.sort()` already applies, written out.
*
* `Array.prototype.sort()` with no argument compares the UTF-16 code units of
* each element's string form. For ISO dates, ASCII keys, currency codes and the
* like that is the order the code wants, but Sonar's S2871 flags the bare call
* because the same syntax silently mis-sorts numbers ([10, 9, 1] becomes
* [1, 10, 9]).
*
* Shared rather than inlined at each call site: an inline arrow is a new
* function on every sort, and the ones that live on error paths are never
* executed by a passing test — which drags a whole domain's coverage down for a
* comparator that does nothing but restate the default. One exported function,
* covered once.
*
* NOT for user-visible names. Code-unit order files every accented word behind
* the entire ASCII range, so a list a person reads wants `localeCompare`.
*/
export const byCodeUnit = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);