timoxley / keycode

Convert between keyboard keycodes and keynames and vice versa.
MIT License
455 stars 62 forks source link

use .charCodeAt() instead of looping. #6

Closed yields closed 11 years ago

yields commented 11 years ago

the tests are passing. also i converted tabs to spaces.

timoxley commented 11 years ago

Cool, definitely need to add more tests.

This removes all characters from the publicly exposed exports.codes object :(

yields commented 11 years ago

That's not a problem, since we can do exports.codes[num] || String.fromCharCode(num);, and i just found out we can do the same with the numbers loop.

'1'.charCodeAt(0); // 49
String.fromCharCode(49); // 1
'A'.charCodeAt(0); // 65
String.fromCharCode(65); // A

let me see what i can do, hopefully i will make it work as expected without an loops.

yields commented 11 years ago

btw i just remembered that we already have keycode > keyname utility using component/keyname. so it might be a good idea to deprecate keycode > keyname conversation and stay with keyname > keycode.

timoxley commented 11 years ago

@yields possibly, though component/keyname only supports named keys, e.g. keyname(65) !== 'a'. Perhaps should just add them to it.

timoxley commented 11 years ago

Also modified your commit a bit, left the loop in but falls back to charCodeAt if it can't find an existing named key, for non-standard keyboards

timoxley commented 11 years ago

Sounds good, loops are crap