ampache_react/app/utils/misc.js

35 lines
703 B
JavaScript
Raw Permalink Normal View History

/**
* Miscellaneous helper functions.
*/
2016-07-07 23:23:18 +02:00
/**
* Strict int checking function.
*
* @param value The value to check for int.
* @return Either NaN if the string was not a valid int representation, or the
* int.
2016-07-07 23:23:18 +02:00
*/
export function filterInt(value) {
2016-07-07 23:23:18 +02:00
if (/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) {
return Number(value);
}
return NaN;
}
2016-07-07 23:23:18 +02:00
/**
* Helper to format song length.
*
* @param time Length of the song in seconds.
* @return Formatted length as MM:SS.
*/
export function formatLength(time) {
2016-07-07 23:23:18 +02:00
const min = Math.floor(time / 60);
let sec = (time - 60 * min);
2016-07-07 23:23:18 +02:00
if (sec < 10) {
sec = "0" + sec;
}
return min + ":" + sec;
}