UNPKG

normalplaytime

Version:

Parser for Normal Play Time (RFC 2326)

55 lines (46 loc) 1.54 kB
"use strict"; var parse_ms_string = function parse_ms_string(msstring) { if (!msstring) { return 0; } switch (msstring.length) { case 0: return 0;break; case 1: return parseInt(msstring, 10) * 100;break; case 2: return parseInt(msstring, 10) * 10;break; default: return parseInt(msstring.substr(0, 3), 10);break; } }; var parse = function parse(timestring) { timestring = timestring.trim(); var pattern_seconds = /^(\d+)(?:\.(\d+))?$/; var pattern_minutes = /^(\d+):(\d\d?)(?:\.(\d+))?$/; var pattern_hours = /^(\d+):(\d\d?):(\d\d?)(?:\.(\d+))?$/; var matches; var ms = 0; var sec = 0; var min = 0; var hr = 0; if (matches = timestring.match(pattern_seconds)) { ms = parse_ms_string(matches[2]); sec = matches[1] ? parseInt(matches[1], 10) : 0; } else if (matches = timestring.match(pattern_minutes)) { ms = parse_ms_string(matches[3]); sec = matches[2] ? parseInt(matches[2], 10) : 0; min = matches[1] ? parseInt(matches[1], 10) : 0; } else if (matches = timestring.match(pattern_hours)) { ms = parse_ms_string(matches[4]); sec = matches[3] ? parseInt(matches[3], 10) : 0; min = matches[2] ? parseInt(matches[2], 10) : 0; hr = matches[1] ? parseInt(matches[1], 10) : 0; } else { return null; } return ((hr * 60 + min) * 60 + sec) * 1000 + ms; }; module.exports = { parse: parse };