ConstThe test fails on a binding the player would refuse with invalid, such
as modifiers out of order or a misspelt key name. isBinding applies the
same grammar as the player's parser: the part after the last + is the
input, everything before it modifiers in the order of modifiers; an
input is a listed name or one other printable character, and Shift
never goes with a digit or such a character.
import assert from "node:assert/strict";
import { BINDING_INPUTS } from "@vestra-player/sdk/testing";
const { modifiers, letters, digits, functionKeys, named, mouse } = BINDING_INPUTS;
const names = new Set([...letters, ...functionKeys, ...named, ...mouse]);
function isBinding(binding: string): boolean {
// "Ctrl++" is not a binding: the + key is written "Plus".
const parts = binding.split("+");
const input = parts.pop()!;
const order = parts.map((m) => (modifiers as readonly string[]).indexOf(m));
// Each modifier known, at most once, in the listed order.
if (order.some((at, i) => at < 0 || (i > 0 && at <= order[i - 1]!)))
return false;
if (names.has(input)) return true; // letters are upper case: "A"
const typed = digits.includes(input) || /^[^\p{L}\p{N}\p{C}\s+]$/u.test(input);
// ">" rather than "Shift+.": the character already says Shift.
return typed && !parts.includes("Shift");
}
for (const binding of ["Ctrl+Shift+PageUp", "Alt+Mouse:WheelUp", ">"])
assert.ok(isBinding(binding), binding);
for (const binding of ["Shift+Ctrl+A", "Ctrl+a", "Shift+."])
assert.ok(!isBinding(binding), binding);
The names a binding (
CommandOptions.bindings,Bindings.set) is made of, as the player's binding parser accepts them. A binding is{ modifier "+" } input: modifiers in the order ofmodifiers, each at most once, then one input. An input is a name ofletters,digits,functionKeys,namedormouse, or one other printable character that is not a letter, digit, space or+(">","[","/"), written as typed, never withShift(">", not"Shift+.";"!", not"Shift+1"). Anything else fails with codeinvalid. The player's tests check that these agree with the player.