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

50 lines
892 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.
*/
2016-11-26 03:25:35 +00:00
export function secondsToHis (d) {
d = ~~d
2016-03-06 07:44:38 +00:00
2016-11-26 03:25:35 +00:00
let s = d % 60
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
if (s < 10) {
2016-11-26 03:25:35 +00:00
s = '0' + s
2016-06-25 16:05:24 +00:00
}
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
let i = Math.floor((d / 60) % 60)
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
if (i < 10) {
2016-11-26 03:25:35 +00:00
i = '0' + i
2016-06-25 16:05:24 +00:00
}
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +00:00
let h = Math.floor(d / 3600)
2015-12-13 04:42:28 +00:00
2016-06-25 16:05:24 +00:00
if (h < 10) {
2016-11-26 03:25:35 +00:00
h = '0' + h
2016-06-25 16:05:24 +00:00
}
2015-12-13 04:42:28 +00:00
2016-11-26 03:25:35 +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>}
*/
2016-11-26 03:25:35 +00:00
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-11-26 03:25:35 +00:00
return str.replace(/<br\s*[\/]?>/gi, '\n')
}