Files

33 lines
846 B
JavaScript
Raw Permalink Normal View History

2022-04-02 17:44:45 +08:00
const { parseDate } = require('@/utils/parse-date');
const title = require('title');
2022-04-02 17:44:45 +08:00
2019-06-12 04:09:27 +01:00
// convert a string into title case
const toTitleCase = (str) => title(str);
2019-06-12 04:09:27 +01:00
2022-03-22 02:13:15 +08:00
const rWhiteSpace = /\s+/;
const rAllWhiteSpace = /\s+/g;
// collapse all whitespaces into a single space (like "white-space: normal;" would do), and trim
const collapseWhitespace = (str) => {
if (str && rWhiteSpace.test(str)) {
return str.replace(rAllWhiteSpace, ' ').trim();
}
return str;
};
2022-04-02 17:44:45 +08:00
const convertDateToISO8601 = (date) => {
if (!date) {
return date;
}
if (typeof date !== 'object') {
// some routes may call `.toUTCString()` before passing the date to ctx...
date = parseDate(date);
}
return date.toISOString();
};
2019-06-12 04:09:27 +01:00
module.exports = {
toTitleCase,
2022-03-22 02:13:15 +08:00
collapseWhitespace,
2022-04-02 17:44:45 +08:00
convertDateToISO8601,
2019-06-12 04:09:27 +01:00
};