pucchinglgl/src/keyPosition.ts

19 lines
563 B
TypeScript
Raw Normal View History

/**
*
* @param layout ` `
* @param key `KeyboardEvent.key`
* @return `[x, y]` `[]`
*/
function keyPosition(
layout: readonly string[],
key: string
): [number, number] | [] {
const [x, y] = layout
.map((row) => row.split(" ").indexOf(key))
.flatMap((x, y) => (x >= 0 ? [x, y] : []));
return x >= 0 ? [x, y] : [];
}
export default keyPosition;