Skip to content

Commit

Permalink
refactor: switch to moment for duration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
vyneer committed Jan 4, 2024
1 parent e6b3c6d commit 388c04c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
36 changes: 6 additions & 30 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2402,36 +2402,12 @@ class Chat {
}

static parseTimeInterval(str) {
let nanoseconds = 0;
const units = {
s: 1000000000,
sec: 1000000000,
secs: 1000000000,
second: 1000000000,
seconds: 1000000000,

m: 60000000000,
min: 60000000000,
mins: 60000000000,
minute: 60000000000,
minutes: 60000000000,

h: 3600000000000,
hr: 3600000000000,
hrs: 3600000000000,
hour: 3600000000000,
hours: 3600000000000,

d: 86400000000000,
day: 86400000000000,
days: 86400000000000,
};
str.replace(regextime, ($0, number, unit) => {
const addNs =
number * (unit ? units[unit.toLowerCase()] || units.s : units.s);
nanoseconds += addNs;
});
return nanoseconds;
const { number, unit } = str.match(regextime)?.groups ?? {};
return (
moment
.duration(number, (unit ?? 'seconds').toLowerCase())
.asMilliseconds() * 1000000
);
}

static loadCss(url) {
Expand Down
3 changes: 2 additions & 1 deletion assets/chat/js/regex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const regexslashcmd = /^\/([a-z0-9]+)[\s]?/i;
const regextime = /(\d+)([a-z]+)?/gi;
const regextime =
/(?<number>\d+)(?<unit>s(?:econds?)?$|m(?:inutes?)?$|h(?:ours?)?$|d(?:ays?)?$)?/i;
const nickmessageregex = /(?=@?)(\w{3,20})/g;
const nickregex = /^\w{3,20}$/;
const nsfwregex = /\bNSFW\b/i;
Expand Down

0 comments on commit 388c04c

Please sign in to comment.