Simple monitor for displaying MIDI status for digital pianos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

isNaturalKey.ts 551 B

123456789101112131415161718192021
  1. const NATURAL_KEYS = [0, 2, 4, 5, 7, 9, 11]
  2. interface IsNaturalKey {
  3. (k: number): boolean
  4. }
  5. const isNaturalKey: IsNaturalKey = (k: number): boolean => {
  6. const type = typeof (k as unknown)
  7. if ((type as string) !== 'number') {
  8. throw TypeError(`Invalid value type passed to isNaturalKey, expected 'number', got ${type}.`)
  9. }
  10. if (isNaN(k)) {
  11. throw RangeError('Value passed is NaN.')
  12. }
  13. if (k < 0) {
  14. throw RangeError('Value must be positive.')
  15. }
  16. return NATURAL_KEYS.includes(Math.floor(k) % 12)
  17. }
  18. export default isNaturalKey