compareCodePoints documented a Unicode code-point comparison but used </> operators, which compare UTF-16 code units. For astral code points (above U+FFFF) the lead surrogate 0xD800-0xDBFF is numerically below the BMP tail U+E000-U+FFFF, so those characters sorted before it. Delegate to the existing compareUnicodeCodePoints, which the domain already uses elsewhere, so the two canonical comparators agree. All call sites use only the sign, so sort and '< 0' checks are unaffected. Adds the missing regression coverage for canonicalOrdering. Co-authored-by: TahsinArafat <TahsinArafat@users.noreply.github.com> Co-authored-by: N0zoM1z0 <1705004398@qq.com>
21 lines
897 B
TypeScript
21 lines
897 B
TypeScript
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
|
|
|
|
/**
|
|
* Canonical code-point ordering shared across domain and application layers.
|
|
*
|
|
* Compares strings by Unicode code point so graph ordering, fingerprints, and
|
|
* deterministic tie-breaks stay stable regardless of locale. Prefer this over
|
|
* `String.prototype.localeCompare`, which is locale-dependent.
|
|
*
|
|
* Ordering by code point differs from ordering by UTF-16 code unit for astral
|
|
* code points (above U+FFFF), whose lead surrogate is numerically below the
|
|
* BMP tail U+E000-U+FFFF.
|
|
*/
|
|
export const compareCodePoints = (left: string, right: string): number =>
|
|
compareUnicodeCodePoints(left, right);
|
|
|
|
/** Deduplicate and sort strings with canonical code-point ordering. */
|
|
export const uniqueSorted = <Value extends string>(
|
|
values: readonly Value[],
|
|
): Value[] => [...new Set(values)].sort(compareCodePoints);
|