|
|
@ -1,4 +1,4 @@ |
|
|
|
import { Duration, Interval } from 'date-fns'; |
|
|
|
import { Duration, Interval, isAfter } from 'date-fns'; |
|
|
|
import add from 'date-fns/add'; |
|
|
|
import add from 'date-fns/add'; |
|
|
|
import intervalToDuration from 'date-fns/intervalToDuration'; |
|
|
|
import intervalToDuration from 'date-fns/intervalToDuration'; |
|
|
|
|
|
|
|
|
|
|
@ -21,6 +21,12 @@ const durationMap: { [key in Required<keyof Duration>]: string[] } = { |
|
|
|
* @public |
|
|
|
* @public |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
export function intervalToAbbreviatedDurationString(interval: Interval, includeSeconds = true): string { |
|
|
|
export function intervalToAbbreviatedDurationString(interval: Interval, includeSeconds = true): string { |
|
|
|
|
|
|
|
// An edge case that causes the app to crash (e.g. browser's clock behind the rule/alert date)
|
|
|
|
|
|
|
|
// The code will again return a proper duration when the browser's clock >= rule/alert date
|
|
|
|
|
|
|
|
if (isAfter(interval.start, interval.end)) { |
|
|
|
|
|
|
|
return ''; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const duration = intervalToDuration(interval); |
|
|
|
const duration = intervalToDuration(interval); |
|
|
|
return (Object.entries(duration) as Array<[keyof Duration, number | undefined]>).reduce((str, [unit, value]) => { |
|
|
|
return (Object.entries(duration) as Array<[keyof Duration, number | undefined]>).reduce((str, [unit, value]) => { |
|
|
|
if (value && value !== 0 && !(unit === 'seconds' && !includeSeconds && str)) { |
|
|
|
if (value && value !== 0 && !(unit === 'seconds' && !includeSeconds && str)) { |
|
|
|