UNPKG

srt-validator

Version:
82 lines (81 loc) 3.13 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var date_1 = require("./date"); var EOL = '\n'; /** * Given a timestamp () * @example * serializeTimeStamp(1000) === 00:00:01,000 * @param {Integer} timeStamp - Timestamp in microseconds * @return {String} - A string representation of the timestamp in SRT format */ function serializeTimeStamp(timeStamp, msSeperator) { var remainder = timeStamp; var hours = timeStamp / date_1.default.hour; remainder %= date_1.default.hour; var minutes = remainder / date_1.default.minute; remainder %= date_1.default.minute; var seconds = remainder / date_1.default.second; remainder %= date_1.default.second; var millis = remainder; var padding = [2, 2, 2, 3]; var _a = [ hours, minutes, seconds, millis, ] // Map numbers to 0-padding + rounded strings .map(function (value, i) { return "".concat(Math.floor(value)).padStart(padding[i], '0'); }), strHours = _a[0], strMinutes = _a[1], strSeconds = _a[2], strMillis = _a[3]; return "".concat(strHours, ":").concat(strMinutes, ":").concat(strSeconds).concat(msSeperator).concat(strMillis); } /** * Given a TimeSpan { start, end }, return the serialized representation * @example * serializeTimeSpan({start: 0, end: 1}) === '00:00:00,000 --> 00:00:00,001' * @param {[type]} timeSpan [description] * @return {[type]} [description] */ function serializeTimeSpan(timeSpan, msSeperator) { return "".concat(serializeTimeStamp(timeSpan.start, msSeperator), " --> ").concat(serializeTimeStamp(timeSpan.end, msSeperator)); } /** * Builds the file contents of an SRT given an array of SRTChunks * @param {Array} srtChunks * @return {String} */ function serialize(srtChunks, format) { if (format === void 0) { format = 'SRT'; } var options = { FILE_HEADER: '', MS_SEPERATOR: ',', FORMAT_TEXT: function (text) { return text; }, CHUNK_SEPARATOR: "".concat(EOL).concat(EOL), }; switch (format.toLowerCase()) { case 'webvtt': options = __assign(__assign({}, options), { FILE_HEADER: "WEBVTT".concat(EOL).concat(EOL), MS_SEPERATOR: '.' }); break; case 'srt': // default is already SRT format break; default: throw new Error("Unrecognized format: ".concat(format)); } return (options.FILE_HEADER + srtChunks .map(function (chunk) { return "".concat(chunk.sequenceNumber, "\n").concat(serializeTimeSpan(chunk.time, options.MS_SEPERATOR), "\n").concat(options.FORMAT_TEXT(chunk.text)); }) .join(options.CHUNK_SEPARATOR)); } exports.default = serialize;