Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch to moment for duration parsing #397

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() * 1e6
);
}

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