@tanstack/hotkeys
Version:
Type-safe, framework-agnostic keyboard hotkey management for the browser
37 lines (35 loc) • 1.42 kB
JavaScript
import { isSingleLetterKey } from "./constants.js";
// src/_keys.ts
/**
* Reads a table's own property only, so names such as `toString` or
* `constructor` never read Object.prototype.
*/
function ownValue(table, name) {
return table && Object.hasOwn(table, name) ? table[name] : void 0;
}
/** 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);
}
/**
* Compares letters without case, preserving accents and avoiding expansions
* such as ß → SS. Only case folds: a collator would also equate letter-like
* symbols with letters, `º` with `O` and `ª` with `A`. Both sides are NFC, as
* key events are: Greek AltGr+N types U+0374, which NFC makes U+02B9.
*/
function fold(key) {
return key.normalize("NFC").toLowerCase();
}
function keysEqual(left, right) {
if (left === right) return true;
return isSingleLetterKey(left) && isSingleLetterKey(right) && fold(left) === fold(right);
}
export { isAsciiLetter, isLatinLetter, isPrintableKey, keysEqual, ownValue };