UNPKG

validate-note

Version:

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

2 lines (1 loc) • 1.01 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.validateNote=t()}(this,function(){"use strict";var e=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],t=function(t,n){return n="#",t=e[e.indexOf(t)-1].charAt(0),{signature:n,letter:t}},n=function(e){return"b"===e||"#"===e},r=function(e){return!Number.isNaN(e)},o=function(e){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=o.maxOctave,a=void 0===i?8:i,u=o.flatToSharp,f=void 0!==u&&u,c=o.octave,d=void 0!==c&&c,v=d?"1":"0,1",s=new RegExp("^(?!([EB]#)|([CF]b))([A-G]{1})([#|b]{0,1})([0-"+a+"]{"+v+"})$","ig");if(!s.test(e))throw new Error("'"+e+"' is not a valid note");for(var l=e.charAt(0),h="",p=void 0,b=1;b<e.length;b++){var g=e[b];n(g)&&(h=g),r(g)&&(p=parseInt(g))}if("b"===h&&f){var m=t(l,h);h=m.signature,l=m.letter}var x={letter:l,signature:h,octave:p};return Object.keys(x).forEach(function(e){return!x[e]&&delete x[e]}),x};return o});