scrabble-solver
Version:
Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Crossplay, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.
27 lines (20 loc) • 712 B
text/typescript
import { type Comparator } from '@/types';
import { createStringComparator } from './createStringComparator';
import { numberComparator } from './numberComparator';
export const createKeyComparator = <T extends Record<keyof T, unknown>>(
key: keyof T,
locale: string,
): Comparator<T> => {
const stringComparator = createStringComparator(locale);
return (a: T, b: T): number => {
const aValue = a[key];
const bValue = b[key];
if (typeof aValue === 'string' && typeof bValue === 'string') {
return stringComparator(aValue, bValue);
}
if (typeof aValue === 'number' && typeof bValue === 'number') {
return numberComparator(aValue, bValue);
}
return 0;
};
};