2021-05-30 14:07:09 +00:00
|
|
|
const utils = require('./utils');
|
2017-06-10 20:23:12 +00:00
|
|
|
var colors = require('mocha/lib/reporters/base').colors;
|
|
|
|
|
|
|
|
colors['pass'] = '30;42';
|
|
|
|
|
2017-06-07 20:47:10 +00:00
|
|
|
/*jshint esversion: 6 */
|
2021-05-30 14:07:09 +00:00
|
|
|
/**
|
|
|
|
* https://regex101.com/r/LEPUGb/1/
|
|
|
|
* This regex parses out the following groups:
|
|
|
|
* tracknumber at the start of the line, optional
|
|
|
|
* start_ts: complete track start timestamp (hh:mm:ss) (mm:ss is minimum)
|
|
|
|
* start_hh: starting hh, optional
|
|
|
|
* start_mm: starting minutes, required
|
|
|
|
* start_ss: starting seconds, required
|
|
|
|
*
|
|
|
|
* end_ts: complete track end timestamp (hh:mm:ss) (mm:ss is minimum to match). optional
|
|
|
|
* end:hh: track end hour, optional
|
|
|
|
* end:mm: track end minute, optional
|
|
|
|
* end:ss: track end seconds, optional
|
|
|
|
*
|
|
|
|
* text_1: text found to the left of the timestamp
|
|
|
|
* text_2: text found to the right of the timestamp
|
|
|
|
*
|
|
|
|
* It is suggested to check their lengths and pick one to parse as the Track Title
|
|
|
|
*/
|
|
|
|
const TS_REGEX = /^((?<track>\d{1,3})\.)* *(?<text_1>.*?) *(?<start_ts>((?<start_hh>\d{1,2}):)?(?<start_mm>\d{1,2}):(?<start_ss>\d{1,2})) *-? *(?<end_ts>(?<end_hh>\d{1,2}:)?(?<end_mm>\d{1,2}):(?<end_ss>\d{1,2}))? *(?<text_2>.*?)$/;
|
2017-06-10 20:23:12 +00:00
|
|
|
const getArtistTitle = require('get-artist-title');
|
|
|
|
var _options = {};
|
2017-06-07 20:14:09 +00:00
|
|
|
|
|
|
|
var filterTimestamp = function(line) {
|
2021-05-30 14:07:09 +00:00
|
|
|
return TS_REGEX.test(line)
|
2017-06-07 20:14:09 +00:00
|
|
|
};
|
|
|
|
|
2021-05-30 14:07:09 +00:00
|
|
|
var parse = function(line) {
|
2017-06-07 20:14:09 +00:00
|
|
|
let matches = line.match(TS_REGEX);
|
|
|
|
return {
|
2021-05-30 14:07:09 +00:00
|
|
|
track: parseInt(matches.groups['track'], 10),
|
|
|
|
start: {
|
|
|
|
ts: matches.groups['start_ts'],
|
|
|
|
hh: matches.groups['start_hh'] ? +matches.groups['start_hh'] : 0,
|
|
|
|
// These 2 are always set
|
|
|
|
mm: +matches.groups['start_mm'],
|
|
|
|
ss: +matches.groups['start_ss'],
|
|
|
|
},
|
|
|
|
end: (matches.groups['end_ts']!==undefined ? {
|
|
|
|
ts: matches.groups['end_ts']? matches.groups['end_ts'] : null,
|
|
|
|
hh: matches.groups['end_hh']? +matches.groups['end_hh'] : null,
|
|
|
|
mm: matches.groups['end_mm']? +matches.groups['end_mm'] : null,
|
|
|
|
ss: matches.groups['end_ss']? +matches.groups['end_ss'] : null,
|
|
|
|
} : null),
|
|
|
|
_: {
|
|
|
|
left_text: matches.groups['text_1'],
|
|
|
|
right_text: matches.groups['text_2']
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 20:14:09 +00:00
|
|
|
};
|
|
|
|
|
2021-05-30 14:07:09 +00:00
|
|
|
var calcTimestamp = function(obj) {
|
|
|
|
if(obj.end) {
|
|
|
|
obj.end.calc = utils.convertTime(obj.end.hh,obj.end.mm,obj.end.ss)
|
|
|
|
}
|
|
|
|
obj.start.calc = utils.convertTime(obj.start.hh,obj.start.mm,obj.start.ss)
|
|
|
|
return obj
|
|
|
|
}
|
2017-06-07 20:14:09 +00:00
|
|
|
|
2021-05-30 14:07:09 +00:00
|
|
|
var parseTitle = function(obj) {
|
|
|
|
let title = obj._.left_text.length > obj._.right_text.length
|
|
|
|
? obj._.left_text : obj._.right_text;
|
2017-06-07 20:14:09 +00:00
|
|
|
|
2021-05-30 14:07:09 +00:00
|
|
|
return Object.assign({title: title}, obj)
|
|
|
|
}
|
2017-06-07 20:14:09 +00:00
|
|
|
|
2017-06-10 20:23:12 +00:00
|
|
|
var parseArtist = function(obj) {
|
|
|
|
let [artist, title] = getArtistTitle(obj.title, {
|
|
|
|
defaultArtist: _options.artist,
|
|
|
|
});
|
|
|
|
return Object.assign({ artist: artist, title: title }, obj);
|
|
|
|
};
|
|
|
|
|
2017-06-07 20:14:09 +00:00
|
|
|
module.exports = {
|
2017-06-10 20:23:12 +00:00
|
|
|
parse: function(text, options = { artist: 'Unknown' }) {
|
|
|
|
_options = options;
|
2017-06-07 20:14:09 +00:00
|
|
|
return text
|
|
|
|
.split('\n')
|
|
|
|
.filter(filterTimestamp)
|
2021-05-30 14:07:09 +00:00
|
|
|
.map(parse)
|
|
|
|
.map(calcTimestamp)
|
2017-06-10 20:23:12 +00:00
|
|
|
.map(parseTitle)
|
|
|
|
.map(parseArtist);
|
2017-06-07 20:14:09 +00:00
|
|
|
},
|
|
|
|
};
|