From a7dc61b0bd20df1d58c3d144de9b6d9c4d8936e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Fri, 16 Dec 2022 15:52:15 +0100 Subject: [PATCH] feat(useTimeAgo): add `floor` and `ceil` value calculation (#2543) Co-authored-by: Anthony Fu --- packages/core/useTimeAgo/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/core/useTimeAgo/index.ts b/packages/core/useTimeAgo/index.ts index 94967b9c96b..91165a30525 100644 --- a/packages/core/useTimeAgo/index.ts +++ b/packages/core/useTimeAgo/index.ts @@ -57,6 +57,13 @@ export interface UseTimeAgoOptions { * @default false */ showSecond?: boolean + + /** + * Rounding method to apply. + * + * @default 'round' + */ + rounding?: 'round' | 'ceil' | 'floor' } interface UseTimeAgoUnit { @@ -124,9 +131,11 @@ export function useTimeAgo(time: MaybeComputedRef, optio messages = DEFAULT_MESSAGES, fullDateFormatter = DEFAULT_FORMATTER, showSecond = false, + rounding = 'round', } = options - const { abs, round } = Math + const { abs } = Math + const roundFn = Math[rounding] const { now, ...controls } = useNow({ interval: updateInterval, controls: true }) function getTimeAgo(from: Date, now: Date) { @@ -160,7 +169,7 @@ export function useTimeAgo(time: MaybeComputedRef, optio } function format(diff: number, unit: UseTimeAgoUnit) { - const val = round(abs(diff) / unit.value) + const val = roundFn(abs(diff) / unit.value) const past = diff > 0 const str = applyFormat(unit.name, val, past)