UNPKG

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.

50 lines (38 loc) 1.49 kB
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { CellFilterType, Point } from 'types'; import { cellFilterInitialState } from './cellFilterInitialState'; export const cellFilterSlice = createSlice({ initialState: cellFilterInitialState, name: 'cellFilter', reducers: { toggle: (state, action: PayloadAction<Point>) => { const { x, y } = action.payload; const currentEntry = state.find((point) => point.x === x && point.y === y); const has = Boolean(currentEntry); const nextType = currentEntry ? toggleCellFilterState(currentEntry.type) : 'include'; if (nextType === null) { return state.filter((point) => point.x !== x || point.y !== y); } if (!has) { return [...state, { ...action.payload, type: nextType }]; } return state.map((entry) => { if (entry.x === x && entry.y === y) { return { ...entry, type: nextType }; } return entry; }); }, cancel: (state, action: PayloadAction<Point>) => { const { x, y } = action.payload; return state.filter((point) => point.x !== x || point.y !== y); }, reset: () => cellFilterInitialState, }, }); const toggleCellFilterState = (type: CellFilterType): CellFilterType | null => { const chain: (CellFilterType | null)[] = ['include', 'exclude', null]; const index = chain.indexOf(type); const nextIndex = (index + 1) % chain.length; return chain[nextIndex]; };