From c4b1b4246126c4dae3a0251c1b6978c57456deba Mon Sep 17 00:00:00 2001 From: Nemo Date: Wed, 28 Jul 2021 17:22:05 +0530 Subject: [PATCH] feat: Parse as durations IFF first timestamp is >0 --- src/parser.js | 16 +++++++++------- test/parser_test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/parser.js b/src/parser.js index 77afabe..5e4b701 100644 --- a/src/parser.js +++ b/src/parser.js @@ -167,14 +167,16 @@ export function parse( if (!options.forceTimestamps) { // If our timestamps are not in increasing order // Assume that we've been given a duration list instead - result.forEach((current, index, list) => { - if (index > 0) { - let previous = list[index - 1]; - if (current.start.calc < previous.start.calc) { - durations = true; + if (result[0].start.calc!=0) { + result.forEach((current, index, list) => { + if (index > 0) { + let previous = list[index - 1]; + if (current.start.calc < previous.start.calc) { + durations = true; + } } - } - }); + }); + } if (durations || options.forceDurations == true) { fixDurations(result); diff --git a/test/parser_test.js b/test/parser_test.js index 2bfa933..bb3e592 100644 --- a/test/parser_test.js +++ b/test/parser_test.js @@ -113,6 +113,37 @@ describe("Parser", function() { }); }); + it("should parse as timestamps if first timestamp is 00:00", function() { + let result = parse(`1. Artist - Title 00:00 +2. Another Artist - Another Title 01:00 +3. Yet Another Artist - Yet another title 02:00`); + assert.deepEqual(result[0], { + artist: "Artist", + title: "Title", + track: 1, + start: { ts: "00:00:00", hh: 0, mm: 0, ss: 0, calc: 0 }, + end: { ts: "00:01:00", hh: 0, mm: 1, ss: 0, calc: 60 }, + _: { left_text: "Artist - Title", right_text: "" }, + }); + + assert.deepEqual(result[1], { + artist: "Another Artist", + title: "Another Title", + track: 2, + end: { ts: "00:02:00", hh: 0, mm: 2, ss: 0, calc: 120 }, + start: { ts: "00:01:00", hh: 0, mm: 1, ss: 0, calc: 60 }, + _: { left_text: "Another Artist - Another Title", right_text: "" }, + }); + assert.deepEqual(result[2], { + artist: "Yet Another Artist", + title: "Yet another title", + track: 3, + end: null, + start: { ts: "00:02:00", hh: 0, mm: 2, ss: 0, calc: 120 }, + _: { left_text: "Yet Another Artist - Yet another title", right_text: "" }, + }); + }); + it("should parse durations as timestamps when forced", function() { let result = parse( `1. Artist - Title 5:00