all files / lib-songbeamer/libs/ song.js

100% Statements 42/42
100% Branches 16/16
100% Functions 3/3
100% Lines 42/42
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101                          46× 46× 24× 24×   22× 22× 22×   146×   138×   146× 146× 144×       22× 20×                   26× 24×     20× 20× 20×   20× 138× 138×               146× 146×   138×                                      
/**
 * @overview Song-File Lib
 * @module song
 * @author Dominik Sigmund
 * @version 1.0
 * @description Gets Json from and json to song-files
 * @memberof lib-songbeamer
 */
/** Get Json from a string
 * @param {string} data - String as delivered by fs.readFile
 * @param {song~jsonCallback} callback - A Callback with an error or the Json
 * @returns Nothing
 * */
exports.toJSON = function (data, callback) {
  var lines = data.split('\r')
  var json = {}
  json.slides = []
  for (let index = 0; index < lines.length; index++) {
    var line = lines[index].trim()
    if (line.startsWith('#')) { // Properties
      var kv = line.split('=')
      json[kv[0].replace('#', '')] = kv[1]
    } else { // Slides
      var slide = {}
      slide.lines = []
      while (line !== '---') {
          // check if line is a caption
        if (isCaption(line)) {
          slide.caption = line
        } else {
          slide.lines.push(line)
        }
        index++
        if (typeof lines[index] !== 'undefined') {
          line = lines[index].trim()
        } else {
          break
        }
      }
      if (slide.lines.length > 0) {
        json.slides.push(slide)
      }
    }
  }
  callback(null, json)
}
/** Write Json to a File String
 * @param {object} json - String as delivered by fs.readFile
 * @param {song~stringCallback} callback - A Callback with an error or the String as delivered to fs.writeFile
 * @returns Nothing
 * */
exports.toFile = function (json, callback) {
  var file = ''
  for (var property in json) {
    if (property !== 'slides' && json.hasOwnProperty(property)) {
      file += '#' + property + '=' + json[property] + '\r\n'
    }
  }
  for (let index = 0; index < json.slides.length; index++) {
    file += '---\r\n'
    const slide = json.slides[index]
    if (typeof slide.caption !== 'undefined') {
      file += slide.caption + '\r\n'
    }
    for (let lineindex = 0; lineindex < slide.lines.length; lineindex++) {
      const line = slide.lines[lineindex]
      file += line + '\r\n'
    }
  }
  file = file.replace(/\r\n$/, '') // remove last newline
  callback(null, file)
}
/** Checks if a string is a caption (Vers, Bridge, Chorus, ...)
 * @param {string} text - A Line of Text
 * @returns {bool} - True, if is a caption, else false
 * */
function isCaption (text) {
  var captions = ['Vers', 'Verse', 'Bridge', 'Chorus', 'Refrain']
  if (new RegExp(captions.join('|')).test(text)) {
    return true
  } else {
    return false
  }
}
/**
  * This callback is displayed as part of the song class.
  * @callback song~jsonCallback
  * @param {object} Error or null
  * @param {object.status} Number of Error (Uses HTTP-Status)
  * @param {object.message} Custom Error Message
  * @param {object} Json - The JSON. See Schema
  */
  /**
  * This callback is displayed as part of the song class.
  * @callback song~stringCallback
  * @param {object} Error or null
  * @param {object.status} Number of Error (Uses HTTP-Status)
  * @param {object.message} Custom Error Message
  * @param {object} String - The String, as in the file
  */