-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create individual file for
add
function
- Loading branch information
Showing
2 changed files
with
50 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var toMS = require('./toMS') | ||
var toSrtTime = require('./toSrtTime') | ||
|
||
/** | ||
* Adds a new caption into the array of subtitles. | ||
* @param {Array} subtitles | ||
* @param {Object} caption | ||
* @return {Array} subtitles | ||
*/ | ||
|
||
module.exports = function add (subtitles, caption) { | ||
if (!caption.start || !caption.end || !caption.text) { | ||
throw new Error('Invalid caption data') | ||
} | ||
|
||
for (var prop in caption) { | ||
if (!caption.hasOwnProperty(prop) || prop === 'text') { | ||
continue | ||
} | ||
|
||
if (prop === 'start' || prop === 'end') { | ||
if (/^(\d{2}):(\d{2}):(\d{2}),(\d{3})$/.test(caption[prop])) { | ||
continue | ||
} | ||
if (/^\d+$/.test(caption[prop])) { | ||
caption[prop] = toSrtTime(caption[prop]) | ||
} else { | ||
throw new Error('Invalid caption time format') | ||
} | ||
} | ||
} | ||
|
||
subtitles.push({ | ||
index: subtitles.length + 1, | ||
start: caption.start, | ||
end: caption.end, | ||
duration: toMS(caption.end) - toMS(caption.start), | ||
text: caption.text | ||
}) | ||
|
||
return subtitles | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters