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

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
/**
* Convert a duration in seconds into H:i:s format.
* If H is 0, it will be omitted.
*/
2022-05-14 15:13:29 +00:00
export const secondsToHis = (d: number) => {
2022-04-15 14:24:30 +00:00
d = ~~d
const s = d % 60
const sString = s < 10 ? `0${s}` : String(s)
const i = Math.floor((d / 60) % 60)
const iString = i < 10 ? `0${i}` : String(i)
const h = Math.floor(d / 3600)
const hString = h < 10 ? `0${h}` : String(h)
return (hString === '00' ? '' : `${hString}:`) + iString + ':' + sString
}
export type ServerValidationError = {
message: string
errors: Record<string, string[]>
}
/**
* Parse the validation error from the server into a flattened array of messages.
*/
2022-05-14 15:13:29 +00:00
export const parseValidationError = (error: ServerValidationError) => {
let messages: string[] = []
2022-04-15 14:24:30 +00:00
2022-05-14 15:13:29 +00:00
Object.keys(error.errors).forEach(key => {
messages = messages.concat(...error.errors[key])
2022-04-15 14:24:30 +00:00
})
return messages
}
/**
* Turn <br> into new line characters.
*/
2022-05-14 15:37:25 +00:00
export const br2nl = (str: string) => str ? str.replace(/<br\s*\/?>/gi, '\n') : ''
2022-04-15 14:24:30 +00:00
2022-05-14 15:37:25 +00:00
export const slugToTitle = (slug: string, separator = '-') => {
let title = slug.split(separator).map(w => w.charAt(0).toUpperCase() + w.substring(1).toLowerCase()).join(' ')
return title.replace(/\s+/g, ' ').trim()
}
2022-05-14 15:13:29 +00:00
export const pluralize = (count: number | undefined, singular: string) => {
count = count ?? 0
return count === 1 ? `${count} ${singular}` : `${count.toLocaleString()} ${singular}s`
}