@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
27 lines (25 loc) • 1.02 kB
JavaScript
import { isSingleLetterKey } from "./constants.js";
// src/_keys.ts
/** Tests for one Unicode code point, including characters represented by a surrogate pair. */
function isPrintableKey(key) {
return key !== "" && Array.from(key).length === 1;
}
/** Identifies ASCII letters whose layout-produced value takes precedence over positional fallbacks. */
function isAsciiLetter(key) {
return /^[A-Za-z]$/.test(key);
}
/** Identifies a single Latin-script character, including accented letters. */
function isLatinLetter(key) {
return /^\p{Script_Extensions=Latin}$/u.test(key);
}
let collator;
/** Compares letters without case, preserving accents and avoiding expansions such as ß → SS. */
function keysEqual(left, right) {
if (left === right) return true;
if (isSingleLetterKey(left) && isSingleLetterKey(right)) {
collator ??= new Intl.Collator(void 0, { sensitivity: "accent" });
return collator.compare(left, right) === 0;
}
return false;
}
export { isAsciiLetter, isLatinLetter, isPrintableKey, keysEqual };