Code coverage report for lib/playlist.js

Statements: 100% (17 / 17)      Branches: 100% (2 / 2)      Functions: 100% (5 / 5)      Lines: 100% (17 / 17)      Ignored: none     

All files » lib/ » playlist.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 48 49 50 51 52 53 54 55 561               1 10               1 3     3 2     2 1 1   1                     1 2             1 1 1       1  
var abc = require('./converters/abc'),
  fs = require('fs'),
  p = require('path'),
  converters = { abc: abc };
 
/**
 * class Playlist
 */
function Playlist() {
  this.songs = [];
}
 
/**
 * Load all songs stored in data directory.
 *
 * @param {String} dir: data directory
 */
Playlist.prototype.load = function (dir) {
  var files = fs.readdirSync(dir),
    self = this;
 
  files.forEach(function (file) {
    var type = p.extname(file).replace(/^\./, ''),
      data = fs.readFileSync(p.join(dir, file)).toString();
 
    if (converters[type]) {
      var song = converters[type].convert(data);
      self.songs.push(song);
    } else {
      console.error('WARN | Unable to convert %s', file);
    }
  });
};
 
/**
 * Get song from the playlist based on its track number.
 *
 * @param {Number} trackNumber: track number of the song in the playlist
 * @return {Object} the retrieved song, undefined if doesn't exist
 */
Playlist.prototype.getSong = function (trackNumber) {
  return this.songs[trackNumber - 1];
};
 
/**
 * Display a list of all songs in the playlist.
 * Songs list will be displayed to stdout.
 */
Playlist.prototype.displaySongs = function () {
  for (var i = 0, ln = this.songs.length; i < ln; i += 1) {
    console.log('%d - %s', i + 1, this.songs[i].title);
  }
};
 
module.exports = Playlist;