Code coverage report for lib/converters/abc.js

Statements: 100% (16 / 16)      Branches: 100% (2 / 2)      Functions: 100% (7 / 7)      Lines: 100% (16 / 16)      Ignored: none     

All files » lib/converters/ » abc.js
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 42 43 44 45 46 47 481     1                         1   4 12     4             3 6 3 21 126 177 177 93               3     1
var abcnode = require('abcnode');
 
// reasonably pleasant Roomba song notes between note numbers 60 - 83
const BASE = 60,
  ROOMBA_NOTES = [
   'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B',
   'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'b' 
  ],
  SANITISE = ['!', '\\\\', 'Z:.+'];
 
/**
 * Convert abc notation into Roombox song.
 *
 * @param {String} data: abc notation data
 * @return {Object} Roombox song
 */
function convert(data) {
 
  SANITISE.forEach(function (pattern) {
    data = data.replace(new RegExp(pattern), '');
  });
 
  var parsed = abcnode.parse(data),
    song = {
      title: parsed.header.title,
      by: parsed.header.composer,
      notes: []
    };
 
  parsed.song.forEach(function (x) {
    x.forEach(function (y) {
      y.forEach(function (z) {
        z.chords.forEach(function (chord) {
          chord.notes.forEach(function (note) {
            var number = ROOMBA_NOTES.indexOf(note.note);
            if (number !== -1) {
              song.notes.push([number + BASE, note.duration]);
            }
          });
        });
      });
    });
  });
 
  return song;
}
 
exports.convert = convert;