aboutsummaryrefslogtreecommitdiffhomepage
path: root/keycodes.js
blob: f141c3630bf8ab207d712dedbbe2ffdb1c3298c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

export function loadKeycodes () {
  return fetch('data/keycodes.json')
    .then(response => response.json())
    .then(normalizeKeycodes)
}

export async function loadIndexedKeycodes() {
  return loadKeycodes()
    .then(keycodes => (
      keycodes.reduce((map, keycode) => Object.assign(map, { [keycode.code]: keycode }), {})
    ))
}

function normalizeKeycodes (keycodes) {
  return keycodes.map(keycode => {
    keycode.params = (keycode.code.match(/\((.+?)\)/) || ['', ''])[1]
      .split(',')
      .map(t => t.trim())
      .filter(t => !!t)

    keycode.aliases = [keycode.code, ...(keycode.aliases || [])]
      .map(code => code.replace(/`/g, ''))
      .map(code => code.replace(/\(.+\)/, ''))

    keycode.symbol = keycode.symbol || (
      [...keycode.aliases]
        .sort((a, b) => a.length - b.length)[0]
        .replace(/^KC_/, '')
    )

    return keycode
  })
  .reduce((keycodes, keycode) => {
    for (let alias of keycode.aliases) {
      keycodes.push(Object.assign({}, keycode, { code: alias }))
    }

    return keycodes
  }, [])
}