koel/resources/assets/js/utils/formatters.js

50 lines
953 B
JavaScript
Raw Normal View History

2016-04-10 09:51:06 +00:00
/**
* Convert a duration in seconds into H:i:s format.
* If H is 0, it will be ommited.
*/
export function secondsToHis(d) {
d = parseInt(d, 10);
2016-03-06 07:44:38 +00:00
2016-04-10 09:51:06 +00:00
let s = d % 60;
2015-12-13 04:42:28 +00:00
2016-04-10 09:51:06 +00:00
if (s < 10) {
s = '0' + s;
}
2015-12-13 04:42:28 +00:00
2016-04-10 09:51:06 +00:00
let i = Math.floor((d / 60) % 60);
2015-12-13 04:42:28 +00:00
2016-04-10 09:51:06 +00:00
if (i < 10) {
i = '0' + i;
}
2015-12-13 04:42:28 +00:00
2016-04-10 09:51:06 +00:00
let h = Math.floor(d / 3600);
2015-12-13 04:42:28 +00:00
2016-04-10 09:51:06 +00:00
if (h < 10) {
h = '0' + h;
}
2015-12-13 04:42:28 +00:00
2016-04-10 09:51:06 +00:00
return (h === '00' ? '' : h + ':') + i + ':' + s;
};
2016-04-10 09:51:06 +00:00
/**
* Parse the validation error from the server into a flattened array of messages.
*
* @param {Object} error The error object in JSON format.
*
* @return {Array.<String>}
*/
export function parseValidationError(error) {
return Object.keys(error).reduce((messages, field) => messages.concat(error[field]), []);
};
2016-01-11 15:25:58 +00:00
2016-04-10 09:51:06 +00:00
/**
* Turn <br> into new line characters.
*
* @param {string} str
*
* @return {string}
*/
export function br2nl (str) {
2016-04-10 09:51:06 +00:00
return str.replace(/<br\s*[\/]?>/gi, '\n');
2015-12-13 04:42:28 +00:00
};