-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
38 lines (34 loc) · 913 Bytes
/
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
38
import dayjs from 'dayjs';
const utils = {
titleCase: (text) => {
if (text.length === 0) {
return text;
}
return `${text.charAt(0).toUpperCase()}${text.substring(1).toLowerCase()}`;
},
pluralize: (text) => {
return text.endsWith('s') ? text : text + 's'
},
parseFoodDateRange: (text) => {
try {
if (text.includes(' to ')) {
const [start, to, end, unit] = text.split(' ');
const unitPlural = unit.endsWith('s') ? unit : unit + 's';
return {
minTime: dayjs.duration({ [unitPlural]: parseInt(start, 10) }),
maxTime: dayjs.duration({ [unitPlural]: parseInt(end, 10) }),
};
} else {
const [end, unit] = text.split(' ');
const unitPlural = unit.endsWith('s') ? unit : unit + 's';
return {
maxTime: dayjs.duration({ [unitPlural]: parseInt(end, 10) })
};
}
} catch (err) {
console.warn(err);
return null;
}
}
}
export default utils;