mirror of
https://github.com/morluto/rea
synced 2026-10-26 22:28:27 -04:00
15 lines
623 B
TypeScript
15 lines
623 B
TypeScript
/** Compare strings lexicographically by Unicode code point, independent of locale. */
|
|
export const compareUnicodeCodePoints = (
|
|
left: string,
|
|
right: string,
|
|
): number => {
|
|
const leftPoints = [...left].map((value) => value.codePointAt(0) ?? 0);
|
|
const rightPoints = [...right].map((value) => value.codePointAt(0) ?? 0);
|
|
const length = Math.min(leftPoints.length, rightPoints.length);
|
|
for (let index = 0; index < length; index += 1) {
|
|
const difference = (leftPoints[index] ?? 0) - (rightPoints[index] ?? 0);
|
|
if (difference !== 0) return difference;
|
|
}
|
|
return leftPoints.length - rightPoints.length;
|
|
};
|