normalizeKey.ts 565 B

123456789101112131415161718192021222324252627
  1. import { isString } from '../../type/type';
  2. /**
  3. * The map to associate a non-standard name to the standard one.
  4. *
  5. * @since 4.0.0
  6. */
  7. export const NORMALIZATION_MAP = {
  8. Spacebar: ' ',
  9. Right : 'ArrowRight',
  10. Left : 'ArrowLeft',
  11. Up : 'ArrowUp',
  12. Down : 'ArrowDown',
  13. };
  14. /**
  15. * Normalizes the key.
  16. *
  17. * @param key - A string or a KeyboardEvent object.
  18. *
  19. * @return A normalized key.
  20. */
  21. export function normalizeKey( key: string | KeyboardEvent ): string {
  22. key = isString( key ) ? key : key.key;
  23. return NORMALIZATION_MAP[ key ] || key;
  24. }