- 1 :
/**
- 2 :
* @file format-time.js
- 3 :
* @module format-time
- 4 :
*/
- 5 :
- 6 :
/**
- 7 :
* Format seconds as a time string, H:MM:SS or M:SS. Supplying a guide (in
- 8 :
* seconds) will force a number of leading zeros to cover the length of the
- 9 :
* guide.
- 10 :
*
- 11 :
* @private
- 12 :
* @param {number} seconds
- 13 :
* Number of seconds to be turned into a string
- 14 :
*
- 15 :
* @param {number} guide
- 16 :
* Number (in seconds) to model the string after
- 17 :
*
- 18 :
* @return {string}
- 19 :
* Time formatted as H:MM:SS or M:SS
- 20 :
*/
- 21 :
const defaultImplementation = function(seconds, guide) {
- 22 :
seconds = seconds < 0 ? 0 : seconds;
- 23 :
let s = Math.floor(seconds % 60);
- 24 :
let m = Math.floor(seconds / 60 % 60);
- 25 :
let h = Math.floor(seconds / 3600);
- 26 :
const gm = Math.floor(guide / 60 % 60);
- 27 :
const gh = Math.floor(guide / 3600);
- 28 :
- 29 :
// handle invalid times
- 30 :
if (isNaN(seconds) || seconds === Infinity) {
- 31 :
// '-' is false for all relational operators (e.g. <, >=) so this setting
- 32 :
// will add the minimum number of fields specified by the guide
- 33 :
h = m = s = '-';
- 34 :
}
- 35 :
- 36 :
// Check if we need to show hours
- 37 :
h = (h > 0 || gh > 0) ? h + ':' : '';
- 38 :
- 39 :
// If hours are showing, we may need to add a leading zero.
- 40 :
// Always show at least one digit of minutes.
- 41 :
m = (((h || gm >= 10) && m < 10) ? '0' + m : m) + ':';
- 42 :
- 43 :
// Check if leading zero is need for seconds
- 44 :
s = (s < 10) ? '0' + s : s;
- 45 :
- 46 :
return h + m + s;
- 47 :
};
- 48 :
- 49 :
// Internal pointer to the current implementation.
- 50 :
let implementation = defaultImplementation;
- 51 :
- 52 :
/**
- 53 :
* Replaces the default formatTime implementation with a custom implementation.
- 54 :
*
- 55 :
* @param {Function} customImplementation
- 56 :
* A function which will be used in place of the default formatTime
- 57 :
* implementation. Will receive the current time in seconds and the
- 58 :
* guide (in seconds) as arguments.
- 59 :
*/
- 60 :
export function setFormatTime(customImplementation) {
- 61 :
implementation = customImplementation;
- 62 :
}
- 63 :
- 64 :
/**
- 65 :
* Resets formatTime to the default implementation.
- 66 :
*/
- 67 :
export function resetFormatTime() {
- 68 :
implementation = defaultImplementation;
- 69 :
}
- 70 :
- 71 :
/**
- 72 :
* Delegates to either the default time formatting function or a custom
- 73 :
* function supplied via `setFormatTime`.
- 74 :
*
- 75 :
* Formats seconds as a time string (H:MM:SS or M:SS). Supplying a
- 76 :
* guide (in seconds) will force a number of leading zeros to cover the
- 77 :
* length of the guide.
- 78 :
*
- 79 :
* @static
- 80 :
* @example formatTime(125, 600) === "02:05"
- 81 :
* @param {number} seconds
- 82 :
* Number of seconds to be turned into a string
- 83 :
*
- 84 :
* @param {number} guide
- 85 :
* Number (in seconds) to model the string after
- 86 :
*
- 87 :
* @return {string}
- 88 :
* Time formatted as H:MM:SS or M:SS
- 89 :
*/
- 90 :
function formatTime(seconds, guide = seconds) {
- 91 :
return implementation(seconds, guide);
- 92 :
}
- 93 :
- 94 :
export default formatTime;