From c4a932c37e9eded0793be4eaebbad1ce0288e75d Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Thu, 4 Oct 2018 21:22:27 +0200 Subject: [PATCH] Fix: .ass does not want 3 decimals in ms --- lib/mstime.js | 10 ++++------ test/substation.js | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/mstime.js b/lib/mstime.js index f9dd57d..c64be33 100644 --- a/lib/mstime.js +++ b/lib/mstime.js @@ -12,17 +12,15 @@ var msTime = module.exports = function (val, leadingZeros) { return res }) - var ms = val.toString() - if (ms.length < 3) { - for (var i = 0, l = ms.length; i <= leadingZeros - l; i += 1) { - ms = '0' + ms - } + var ms = Math.floor(val / 10).toString() + for (var i = 0, l = ms.length; i <= leadingZeros - l; i += 1) { + ms = '0' + ms } return time.join(':') + ',' + ms } msTime.substation = function (val) { - var time = msTime(val, true) + var time = msTime(val, 1) return time.replace(',', '.').slice(1) } diff --git a/test/substation.js b/test/substation.js index 48f8511..471c178 100644 --- a/test/substation.js +++ b/test/substation.js @@ -4,12 +4,20 @@ var substation = require('../lib/substation') suite('substation', function () { suite('stringify', function () { test('single', function () { - var subtitle = { - id: 1, - startTime: 1000, - endTime: 3000, - text: 'Hello World' - } + var subtitles = [ + { + id: 1, + startTime: 1000, + endTime: 3000, + text: 'Hello World' + }, + { + id: 2, + startTime: 4123, + endTime: 5555, + text: 'Hello World 2' + } + ] var params = { Fontname: 'Verdana', @@ -17,12 +25,13 @@ suite('substation', function () { PrimaryColour: '11861244' } - var lines = substation.stringify([ subtitle ], params).split('\n') + var lines = substation.stringify(subtitles, params).split('\n') lines.pop() expect(lines.shift()).to.be.equal('[Script Info]') expect(lines.shift()).to.be.equal('Title: Videoshow video') expect(lines.shift()).to.be.equal('ScriptType: v4') + expect(lines.pop()).to.be.equal('Dialogue: Marked=0,0:00:04.12,0:00:05.55,DefaultVCD,NTP,0000,0000,0000,,Hello World 2') expect(lines.pop()).to.be.equal('Dialogue: Marked=0,0:00:01.00,0:00:03.00,DefaultVCD,NTP,0000,0000,0000,,Hello World') expect(lines.pop()).to.be.equal('Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text') })