🏡 index : github.com/captn3m0/youtube-cue.git

author Nemo <commits@captnemo.in> 2021-07-28 17:22:05.0 +05:30:00
committer Nemo <commits@captnemo.in> 2021-07-28 17:22:05.0 +05:30:00
commit
c4b1b4246126c4dae3a0251c1b6978c57456deba [patch]
tree
396194fcb98384c3294d32968e6fcfc272fced82
parent
a737d371057cf59eec67209b84dbf166ce0dd57f
download
c4b1b4246126c4dae3a0251c1b6978c57456deba.tar.gz

feat: Parse as durations IFF first timestamp is >0



Diff

 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
+++ a/src/parser.js
@@ -167,14 +167,16 @@
  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
+++ a/test/parser_test.js
@@ -113,6 +113,37 @@
    });
  });

  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