UNPKG

ld-videojs-record

Version:

A video.js plugin for recording audio/video/image files.

70 lines (62 loc) 2.04 kB
/** * @file format-time.js * @since 2.0.0 */ /** * Format seconds as a time string, H:MM:SS, M:SS or M:SS:MMM. * * Supplying a guide (in seconds) will force a number of leading zeros * to cover the length of the guide. * * @param {number} seconds - Number of seconds to be turned into a * string. * @param {number} guide - Number (in seconds) to model the string * after. * @param {number} msDisplayMax - Number (in milliseconds) to model the string * after. * @return {string} Time formatted as H:MM:SS, M:SS or M:SS:MMM, e.g. * 0:00:12. * @private */ const formatTime = function(seconds, guide, msDisplayMax) { // buddy ignore:start // Default to using seconds as guide seconds = seconds < 0 ? 0 : seconds; guide = guide || seconds; let s = Math.floor(seconds % 60), m = Math.floor(seconds / 60 % 60), h = Math.floor(seconds / 3600), gm = Math.floor(guide / 60 % 60), gh = Math.floor(guide / 3600), ms = Math.floor((seconds - s) * 1000); // handle invalid times if (isNaN(seconds) || seconds === Infinity) { // '-' is false for all relational operators (e.g. <, >=) so this // setting will add the minimum number of fields specified by the // guide h = m = s = ms = '-'; } // Check if we need to show milliseconds if (guide > 0 && guide < msDisplayMax) { if (ms < 100) { if (ms < 10) { ms = '00' + ms; } else { ms = '0' + ms; } } ms = ':' + ms; } else { ms = ''; } // Check if we need to show hours h = (h > 0 || gh > 0) ? h + ':' : ''; // If hours are showing, we may need to add a leading zero. // Always show at least one digit of minutes. m = (((h || gm >= 10) && m < 10) ? '0' + m : m) + ':'; // Check if leading zero is need for seconds s = ((s < 10) ? '0' + s : s); // buddy ignore:end return h + m + s + ms; }; export default formatTime;