-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hyper-neutrino
committed
Jun 23, 2024
1 parent
41a2dab
commit 7f7aa30
Showing
4 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Formats a duration in milliseconds into English text. | ||
* @param duration - The duration in milliseconds | ||
* @param style - The style to format the duration in | ||
* @returns The formatted duration | ||
*/ | ||
export const formatDuration = (duration: number, style = DurationStyle.For): string => { | ||
if (duration === Infinity) return 'indefinitely'; | ||
|
||
duration = Math.round(duration / 1000); | ||
|
||
if (duration < 0) { | ||
const core = _formatDuration(-duration); | ||
if (style === DurationStyle.Blank) return `negative ${core}`; | ||
if (style === DurationStyle.For) return `for negative ${core}`; | ||
if (style === DurationStyle.Until) return `until ${core} ago`; | ||
} | ||
|
||
if (duration === 0) { | ||
if (style === DurationStyle.Blank) return 'no time'; | ||
if (style === DurationStyle.For) return 'for no time'; | ||
if (style === DurationStyle.Until) return 'until right now'; | ||
} | ||
|
||
const core = _formatDuration(duration); | ||
if (style === DurationStyle.Blank) return core; | ||
if (style === DurationStyle.For) return `for ${core}`; | ||
if (style === DurationStyle.Until) return `until ${core} from now`; | ||
|
||
return '??'; | ||
}; | ||
|
||
function _formatDuration(duration: number): string { | ||
if (duration === Infinity) return 'indefinitely'; | ||
|
||
const parts: string[] = []; | ||
|
||
for (const [name, scale] of formatTimescales) { | ||
if (duration >= scale) { | ||
const amount = Math.floor(duration / scale); | ||
duration %= scale; | ||
|
||
parts.push(`${amount} ${name}${amount === 1 ? '' : 's'}`); | ||
} | ||
} | ||
|
||
return parts.join(' '); | ||
} | ||
|
||
export enum DurationStyle { | ||
Blank, | ||
For, | ||
Until, | ||
} | ||
|
||
const formatTimescales: [string, number][] = [ | ||
['day', 86400], | ||
['hour', 3600], | ||
['minute', 60], | ||
['second', 1], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Parses English text into a duration in milliseconds. Works for long for (1 day, | ||
* 3 weeks 2 hours) and short form (1d, 3w2h). | ||
* | ||
* @param text - The text to parse | ||
* @returns The duration in milliseconds or null if invalid | ||
*/ | ||
export const parseDuration = (text: string): number | null => { | ||
const match = text.match( | ||
/^(\d+\s*w(eeks?)?\s*)?(\d+\s*d(ays?)?\s*)?(\d+\s*h((ou)?rs?)?\s*)?(\d+\s*m(in(ute)?s?)?\s*)?(\d+\s*s(ec(ond)?s?)?\s*)?$/, | ||
); | ||
|
||
if (!match) return null; | ||
|
||
let duration = 0; | ||
|
||
for (const [index, scale] of parseTimescales) { | ||
const submatch = match[index]?.match(/\d+/); | ||
if (submatch) duration += parseInt(submatch[0]) * scale; | ||
} | ||
|
||
return duration; | ||
}; | ||
|
||
const parseTimescales = [ | ||
[1, 604800000], | ||
[3, 86400000], | ||
[5, 3600000], | ||
[8, 60000], | ||
[11, 1000], | ||
]; |