added shortcuts for type unints

This commit is contained in:
cupcakearmy 2019-12-10 12:19:14 +01:00
parent faa329e7f1
commit 92ab913f7e

View File

@ -1,18 +1,25 @@
type Intervals = type Intervals =
'millisecond' 'millisecond'
| 'milliseconds' | 'milliseconds'
| 'ms'
| 'second' | 'second'
| 'seconds' | 'seconds'
| 's'
| 'minute' | 'minute'
| 'minutes' | 'minutes'
| 'm'
| 'hour' | 'hour'
| 'hours' | 'hours'
| 'h'
| 'day' | 'day'
| 'days' | 'days'
| 'd'
| 'week' | 'week'
| 'weeks' | 'weeks'
| 'w'
| 'year' | 'year'
| 'years' | 'years'
| 'y'
const Millisecond = 1 const Millisecond = 1
const Second = Millisecond * 1000 const Second = Millisecond * 1000
@ -32,7 +39,7 @@ const defaultHumanizer: Humanizer = [
[d => d.days() > 0, d => `${d.days()} days`], [d => d.days() > 0, d => `${d.days()} days`],
[d => d.hours() > 0, d => `${d.hours()} hours`], [d => d.hours() > 0, d => `${d.hours()} hours`],
[d => d.minutes() > 5, d => `${d.minutes()} minutes`], [d => d.minutes() > 5, d => `${d.minutes()} minutes`],
[d => d.minutes() > 0, d => `a few minutes`], [d => d.minutes() > 0, _ => `a few minutes`],
[() => true, () => `a moment`], [() => true, () => `a moment`],
] ]
@ -49,24 +56,31 @@ export class Duration {
switch (interval.toLowerCase()) { switch (interval.toLowerCase()) {
case 'millisecond': case 'millisecond':
case 'milliseconds': case 'milliseconds':
case 'ms':
return amount * Millisecond return amount * Millisecond
case 'second': case 'second':
case 'seconds': case 'seconds':
case 's':
return amount * Second return amount * Second
case 'minute': case 'minute':
case 'minutes': case 'minutes':
case 'm':
return amount * Minute return amount * Minute
case 'hour': case 'hour':
case 'hours': case 'hours':
case 'h':
return amount * Hour return amount * Hour
case 'day': case 'day':
case 'days': case 'days':
case 'd':
return amount * Day return amount * Day
case 'week': case 'week':
case 'weeks': case 'weeks':
case 'w':
return amount * Week return amount * Week
case 'year': case 'year':
case 'years': case 'years':
case 'y':
return amount * Year return amount * Year
default: default:
throw new Error('Wrong interval') throw new Error('Wrong interval')
@ -140,7 +154,7 @@ export class Duration {
} }
public humanize(humanizer?: Humanizer): string { public humanize(humanizer?: Humanizer): string {
if(!humanizer) humanizer = defaultHumanizer if (!humanizer) humanizer = defaultHumanizer
for (const [control, value] of humanizer) for (const [control, value] of humanizer)
if (control(this)) if (control(this))