From c441792d3fe0e00e17e30d7dacd9eadff092106a Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Tue, 19 Nov 2024 23:00:32 +0300 Subject: [PATCH] fix failing test ci --- .eslintrc.json | 3 ++- src/react/Keybinding.tsx | 19 +------------------ ...ng.test.ts => parseKeybindingName.test.ts} | 2 +- src/react/parseKeybindingName.ts | 17 +++++++++++++++++ vitest.config.ts | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) rename src/react/{keybinding.test.ts => parseKeybindingName.test.ts} (93%) create mode 100644 src/react/parseKeybindingName.ts diff --git a/.eslintrc.json b/.eslintrc.json index f454100aa..8b2225b56 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -197,7 +197,8 @@ "no-async-promise-executor": "off", "no-bitwise": "off", "unicorn/filename-case": "off", - "max-depth": "off" + "max-depth": "off", + "unicorn/no-typeof-undefined": "off" }, "overrides": [ { diff --git a/src/react/Keybinding.tsx b/src/react/Keybinding.tsx index 7c6c1bc86..8edc94a59 100644 --- a/src/react/Keybinding.tsx +++ b/src/react/Keybinding.tsx @@ -4,6 +4,7 @@ import triangle from './ps_icons/playstation_triangle_console_controller_gamepad import square from './ps_icons/playstation_square_console_controller_gamepad_icon.svg' import circle from './ps_icons/circle_playstation_console_controller_gamepad_icon.svg' import cross from './ps_icons/cross_playstation_console_controller_gamepad_icon.svg' +import { parseBindingName } from './parseKeybindingName' type Props = { @@ -30,24 +31,6 @@ export default ({ type, val, isPS }: Props) => { } -export async function parseBindingName (binding: string) { - if (!binding) return '' - - const { keyboard } = navigator - const layoutMap = await keyboard?.getLayoutMap?.() ?? new Map() - - const mapKey = (key: string) => layoutMap.get(key) || key - - const cut = binding.replaceAll(/(Digit|Key)/g, '') - const parts = cut.includes('+') ? cut.split('+') : [cut] - - for (let i = 0; i < parts.length; i++) { - parts[i] = mapKey(parts[i]).split(/(?<=[a-z])(?=\d)/).join(' ').split(/(?=[A-Z])/).reverse().join(' ') - } - - return parts.join(' + ') -} - const buttonsMap = { 'A': cross, 'B': circle, diff --git a/src/react/keybinding.test.ts b/src/react/parseKeybindingName.test.ts similarity index 93% rename from src/react/keybinding.test.ts rename to src/react/parseKeybindingName.test.ts index 6c59bc566..2c6396919 100644 --- a/src/react/keybinding.test.ts +++ b/src/react/parseKeybindingName.test.ts @@ -1,5 +1,5 @@ import { test, expect } from 'vitest' -import { parseBindingName } from './Keybinding' +import { parseBindingName } from './parseKeybindingName' test('display keybinding correctly', async () => { expect(await parseBindingName('unknown')).toMatchInlineSnapshot('"unknown"') diff --git a/src/react/parseKeybindingName.ts b/src/react/parseKeybindingName.ts new file mode 100644 index 000000000..2c41d3f87 --- /dev/null +++ b/src/react/parseKeybindingName.ts @@ -0,0 +1,17 @@ +export async function parseBindingName (binding: string) { + if (!binding) return '' + + const { keyboard } = navigator + const layoutMap = (typeof keyboard === 'undefined' ? undefined : await keyboard?.getLayoutMap?.()) ?? new Map() + + const mapKey = (key: string) => layoutMap.get(key) || key + + const cut = binding.replaceAll(/(Digit|Key)/g, '') + const parts = cut.includes('+') ? cut.split('+') : [cut] + + for (let i = 0; i < parts.length; i++) { + parts[i] = mapKey(parts[i]).split(/(?<=[a-z])(?=\d)/).join(' ').split(/(?=[A-Z])/).reverse().join(' ') + } + + return parts.join(' + ') +} diff --git a/vitest.config.ts b/vitest.config.ts index fcda26c8c..c27621d57 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ include: [ '../../src/botUtils.test.ts', '../../src/markdownToFormattedText.test.ts', - '../../src/react/keybinding.test.ts', + '../../src/react/parseKeybindingName.test.ts', 'lib/mesher/test/tests.test.ts', 'sign-renderer/tests.test.ts' ],