-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
32 lines (27 loc) · 834 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
/**
* return current time as `HH:MM:SS`
* @returns {string} the current time string
*/
function getCurrentTime() {
const date = new Date();
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}
/**
* get the cut string with max length
* @param {string} text source string
* @param {number} limit max length of the string
* @returns {string} the cut string, replaced by `...`
*/
function getCutText(text, limit = 60) {
if (limit < 5 || limit > text.length) {
return text;
}
return text.slice(0, limit / 4) + '...' + text.slice(- limit / 4 * 3 + 3);
}
module.exports = {
getCurrentTime,
getCutText
};