Utils broken down into seperate files and exports
This commit is contained in:
36
src/js/utils/time.js
Normal file
36
src/js/utils/time.js
Normal file
@ -0,0 +1,36 @@
|
||||
// ==========================================================================
|
||||
// Time utils
|
||||
// ==========================================================================
|
||||
|
||||
import is from './is';
|
||||
|
||||
// Time helpers
|
||||
export const getHours = value => parseInt((value / 60 / 60) % 60, 10);
|
||||
export const getMinutes = value => parseInt((value / 60) % 60, 10);
|
||||
export const getSeconds = value => parseInt(value % 60, 10);
|
||||
|
||||
// Format time to UI friendly string
|
||||
export function formatTime(time = 0, displayHours = false, inverted = false) {
|
||||
// Bail if the value isn't a number
|
||||
if (!is.number(time)) {
|
||||
return formatTime(null, displayHours, inverted);
|
||||
}
|
||||
|
||||
// Format time component to add leading zero
|
||||
const format = value => `0${value}`.slice(-2);
|
||||
|
||||
// Breakdown to hours, mins, secs
|
||||
let hours = getHours(time);
|
||||
const mins = getMinutes(time);
|
||||
const secs = getSeconds(time);
|
||||
|
||||
// Do we need to display hours?
|
||||
if (displayHours || hours > 0) {
|
||||
hours = `${hours}:`;
|
||||
} else {
|
||||
hours = '';
|
||||
}
|
||||
|
||||
// Render
|
||||
return `${inverted ? '-' : ''}${hours}${format(mins)}:${format(secs)}`;
|
||||
}
|
Reference in New Issue
Block a user