feat(Pairing): add nextFocalId cycle math and expose slice API

This commit is contained in:
Ilia Mashkov
2026-06-24 13:48:50 +03:00
parent 91bb046339
commit f29e0b0c7c
4 changed files with 65 additions and 0 deletions
@@ -0,0 +1,21 @@
/**
* The id one step from `currentId` in board order, wrapping at both ends.
*
* @param orderedIds - Pairing ids in board order.
* @param currentId - The currently focal id to step from.
* @param direction - +1 for next, -1 for previous.
* @returns The neighbouring id (wrapped), `currentId` unchanged if it isn't in
* the list, or null for an empty list.
*/
export function nextFocalId(orderedIds: string[], currentId: string, direction: 1 | -1): string | null {
if (orderedIds.length === 0) {
return null;
}
const i = orderedIds.indexOf(currentId);
if (i === -1) {
return currentId;
}
const len = orderedIds.length;
const next = (i + direction + len) % len;
return orderedIds[next];
}