forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-utils.js
37 lines (32 loc) · 942 Bytes
/
common-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { parseDate } = require('@/utils/parse-date');
// convert a string into title case
const toTitleCase = (str) =>
str
.toLowerCase()
.split(' ')
.map((word) => word.replace(word[0], word[0].toUpperCase()))
.join(' ');
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;
};
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();
};
module.exports = {
toTitleCase,
collapseWhitespace,
convertDateToISO8601,
};