scrabble-solver
Version:
Scrabble Solver 2 - Free, open-source, cross-platform, multi-language analysis tool for Scrabble, Scrabble Duel, Super Scrabble, Letter League, Literaki, and Kelimelik. Quickly find the top-scoring words using the given board and tiles.
36 lines (27 loc) • 939 B
text/typescript
import { Result } from '@scrabble-solver/types';
import { CellFilterEntry, GroupedResults } from 'types';
import { createRegExp } from './createRegExp';
import { resultMatchesCellFilter } from './resultMatchesCellFilter';
export const groupResults = (
results: Result[] | undefined,
query: string,
cellFilter: CellFilterEntry[],
): GroupedResults | undefined => {
if (typeof results === 'undefined') {
return results;
}
const regExp = createRegExp(query);
const { matching, other } = results.reduce<GroupedResults>(
(groupedResults, result) => {
const matchesQuery = () => Boolean(result.word.match(regExp));
if (resultMatchesCellFilter(result, cellFilter) && matchesQuery()) {
groupedResults.matching.push(result);
} else {
groupedResults.other.push(result);
}
return groupedResults;
},
{ matching: [], other: [] },
);
return { matching, other };
};