<commit_before>import { configuration } from './configuration';

export class Notation {
  // Mapping from the normalized string to regex strings that could match it.
  private static _notationMap: { [key: string]: string[] } = {
    'C-': ['ctrl\\+', 'c\\-'],
    'D-': ['cmd\\+', 'd\\-'],
    Esc: ['escape', 'esc'],
    BS: ['backspace', 'bs'],
    Del: ['delete', 'del'],
    Home: ['home'],
    End: ['end'],
    Insert: ['insert'],
    ' ': ['<space>'],
    '\n': ['<cr>', '<enter>'],
  };

  /**
   * Converts keystroke like <tab> to a single control character like \t
   */
  public static ToControlCharacter(key: string) {
    if (key === '<tab>') {
      return '\t';
    }

    return key;
  }

  public static IsControlKey(key: string): boolean {
    key = key.toLocaleUpperCase();
    return (
      this.isSurroundedByAngleBrackets(key) &&
      key !== '<BS>' &&
      key !== '<SHIFT+BS>' &&
      key !== '<TAB>'
    );
  }

  /**
   * Normalizes key to AngleBracketNotation
   * (e.g. <ctrl+x>, Ctrl+x, <c-x> normalized to <C-x>)
   * and converts the characters to their literals
   * (e.g. <space>, <cr>, <leader>)
   */
  public static NormalizeKey(key: string, leaderKey: string): string {
    if (typeof key !== 'string') {
      return key;
    }

    if (!this.isSurroundedByAngleBrackets(key) && key.length > 1) {
      key = `<${key.toLocaleLowerCase()}>`;
    }

    if (key.toLocaleLowerCase() === '<leader>') {
      return leaderKey;
    }

    if (['<up>', '<down>', '<left>', '<right>'].includes(key.toLocaleLowerCase())) {
      return key.toLocaleLowerCase();
    }

    for (const notationMapKey in this._notationMap) {
      if (this._notationMap.hasOwnProperty(notationMapKey)) {
        const regex = new RegExp(this._notationMap[notationMapKey].join('|'), 'gi');
        if (regex.test(key)) {
          key = key.replace(regex, notationMapKey);
          break;
        }<commit_msg>Only construct Regexes for NormalizeKey() once<commit_after>import { configuration } from './configuration';

export class Notation {
  // Mapping from the normalized string to a regex that could match it.
  private static readonly _notationMap: { [key: string]: RegExp } = {
    'C-': /ctrl\+|c\-/gi,
    'D-': /cmd\+|d\-/gi,
    Esc: /escape|esc/gi,
    BS: /backspace|bs/gi,
    Del: /delete|del/gi,
    Home: /home/gi,
    End: /end/gi,
    Insert: /insert/gi,
    ' ': /<space>/gi,
    '\n': /<cr>|<enter>/gi,
  };

  /**
   * Converts keystroke like <tab> to a single control character like \t
   */
  public static ToControlCharacter(key: string) {
    if (key === '<tab>') {
      return '\t';
    }

    return key;
  }

  public static IsControlKey(key: string): boolean {
    key = key.toLocaleUpperCase();
    return (
      this.isSurroundedByAngleBrackets(key) &&
      key !== '<BS>' &&
      key !== '<SHIFT+BS>' &&
      key !== '<TAB>'
    );
  }

  /**
   * Normalizes key to AngleBracketNotation
   * (e.g. <ctrl+x>, Ctrl+x, <c-x> normalized to <C-x>)
   * and converts the characters to their literals
   * (e.g. <space>, <cr>, <leader>)
   */
  public static NormalizeKey(key: string, leaderKey: string): string {
    if (typeof key !== 'string') {
      return key;
    }

    if (!this.isSurroundedByAngleBrackets(key) && key.length > 1) {
      key = `<${key.toLocaleLowerCase()}>`;
    }

    if (key.toLocaleLowerCase() === '<leader>') {
      return leaderKey;
    }

    if (['<up>', '<down>', '<left>', '<right>'].includes(key.toLocaleLowerCase())) {
      return key.toLocaleLowerCase();
    }

    for (const notationMapKey in this._notationMap) {
      if (this._notationMap.hasOwnProperty(notationMapKey)) {
        const regex = this._notationMap[notationMapKey];
        if (regex.test(key)) {
          key = key.replace(regex, notationMapKey);
          break;
        }
{'fraction_non_alphanumeric': 0.18502087682672233, 'fraction_numerical': 0.0005219206680584551, 'mean_word_length': 2.685576923076923, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7223046', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1263, 'n_words': 314}