From 388c04cd823bb10bd744ea28fe955c6e4dfe3b38 Mon Sep 17 00:00:00 2001 From: vyneer Date: Thu, 4 Jan 2024 16:00:15 +0300 Subject: [PATCH 1/3] refactor: switch to moment for duration parsing --- assets/chat/js/chat.js | 36 ++++++------------------------------ assets/chat/js/regex.js | 3 ++- 2 files changed, 8 insertions(+), 31 deletions(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index c73c1dd1..2e18a3e3 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -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) { diff --git a/assets/chat/js/regex.js b/assets/chat/js/regex.js index 5e05600b..17bd3489 100644 --- a/assets/chat/js/regex.js +++ b/assets/chat/js/regex.js @@ -1,5 +1,6 @@ const regexslashcmd = /^\/([a-z0-9]+)[\s]?/i; -const regextime = /(\d+)([a-z]+)?/gi; +const regextime = + /(?\d+)(?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; From dfb5a245507d8f02400839b665f46af3a5e231b8 Mon Sep 17 00:00:00 2001 From: vyneer Date: Mon, 8 Jan 2024 03:26:18 +0300 Subject: [PATCH 2/3] chore: nicer duration notation --- assets/chat/js/chat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/chat/js/chat.js b/assets/chat/js/chat.js index 2e18a3e3..462275bc 100644 --- a/assets/chat/js/chat.js +++ b/assets/chat/js/chat.js @@ -2406,7 +2406,7 @@ class Chat { return ( moment .duration(number, (unit ?? 'seconds').toLowerCase()) - .asMilliseconds() * 1000000 + .asMilliseconds() * 1e6 ); } From 43daf50f73c6047dcb61c032e8df631bf1728f39 Mon Sep 17 00:00:00 2001 From: vyneer Date: Mon, 8 Jan 2024 03:27:02 +0300 Subject: [PATCH 3/3] chore: more readable duration regex --- assets/chat/js/regex.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/chat/js/regex.js b/assets/chat/js/regex.js index 17bd3489..cea543ff 100644 --- a/assets/chat/js/regex.js +++ b/assets/chat/js/regex.js @@ -1,6 +1,6 @@ const regexslashcmd = /^\/([a-z0-9]+)[\s]?/i; const regextime = - /(?\d+)(?s(?:econds?)?$|m(?:inutes?)?$|h(?:ours?)?$|d(?:ays?)?$)?/i; + /(?\d+)(?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;