UNPKG

validate-chord

Version:

🎼 Validates a chord (scientific pitch notation) and throws errors if needed

33 lines (24 loc) • 999 B
import validateNote from 'validate-note'; import types from './data/types'; import getNote from './lib/getNote'; export default (function (chord) { var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, _ref$maxOctave = _ref.maxOctave, maxOctave = _ref$maxOctave === undefined ? 8 : _ref$maxOctave, _ref$flatToSharp = _ref.flatToSharp, flatToSharp = _ref$flatToSharp === undefined ? false : _ref$flatToSharp; if (typeof chord !== 'string') { throw new Error('\'' + chord + '\' is not a valid chord'); } var note = getNote(chord); var sNote = validateNote(note, { maxOctave: maxOctave, flatToSharp: flatToSharp, octave: true }); var type = types[chord.split(note)[1].toLowerCase()]; if (!type) throw new Error('\'' + chord.split(note)[1] + '\' is not a valid chord type'); Object.keys(sNote).forEach(function (key) { return !sNote[key] && delete sNote[key]; }); return { note: sNote, type: type }; });