/[sudobot]/trunk/src/util.js
ViewVC logotype

Annotation of /trunk/src/util.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 37 - (hide annotations)
Mon Jul 29 17:28:18 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: text/javascript
File size: 1224 byte(s)
Fixed issues and improved logging
1 rakin 5 module.exports = {
2     escapeRegex(string) {
3     return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
4 rakin 37 },
5     timeSince(date) {
6     const seconds = Math.floor((Date.now() - date) / 1000);
7     let interval = seconds / (60 * 60 * 24 * 30 * 365);
8    
9     if (interval > 1) {
10     return Math.floor(interval) + " year" + (Math.floor(interval) === 1 ? '' : 's');
11     }
12    
13     interval = seconds / (60 * 60 * 24 * 30);
14    
15     if (interval > 1) {
16     return Math.floor(interval) + " month" + (Math.floor(interval) === 1 ? '' : 's');
17     }
18    
19     interval = seconds / (60 * 60 * 24);
20    
21     if (interval > 1) {
22     return Math.floor(interval) + " day" + (Math.floor(interval) === 1 ? '' : 's');
23     }
24    
25     interval = seconds / (60 * 60);
26    
27     if (interval > 1) {
28     return Math.floor(interval) + " hour" + (Math.floor(interval) === 1 ? '' : 's');
29     }
30    
31     interval = seconds / 60;
32    
33     if (interval > 1) {
34     return Math.floor(interval) + " minute" + (Math.floor(interval) === 1 ? '' : 's');
35     }
36    
37     interval = seconds;
38    
39     return Math.floor(interval) + " second" + (Math.floor(interval) === 1 ? '' : 's');
40     }
41 rakin 5 };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26