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.
43 lines (34 loc) • 1.44 kB
text/typescript
import { BLANK } from '@scrabble-solver/constants';
import { type Config, type Locale } from '@scrabble-solver/types';
import { localeTransliterate } from './localeTransliterate';
interface Options {
upperCaseDigraphsOnly?: boolean;
}
export const extractCharacters = (config: Config, value: string, options?: Options): string[] => {
let index = 0;
const characters: string[] = [];
const finalValue = localeTransliterate(config.locale, value);
while (index < finalValue.length) {
const character = finalValue[index];
const characterLowercase = character.toLocaleLowerCase(config.locale);
const nextCharacter = finalValue[index + 1];
const digraph = `${character}${nextCharacter}`;
const digraphLowercase = digraph.toLocaleLowerCase(config.locale);
const isValidDigraph = options?.upperCaseDigraphsOnly
? config.twoCharacterTiles.includes(digraphLowercase) && isUpperCase(config.locale, digraph)
: config.twoCharacterTiles.includes(digraphLowercase);
if (isValidDigraph) {
characters.push(digraphLowercase);
index += digraphLowercase.length;
} else if (config.hasCharacter(characterLowercase) || characterLowercase === BLANK) {
characters.push(characterLowercase);
++index;
} else {
++index;
}
}
return characters;
};
const isUpperCase = (locale: Locale, value: string): boolean => {
return value === value.toLocaleUpperCase(locale);
};