From 73d27aa034268663a7e88e3918d0f8393f029ddb Mon Sep 17 00:00:00 2001 From: userquin Date: Wed, 14 Dec 2022 18:53:00 +0100 Subject: [PATCH 1/2] feat(useTimeAgo): add `floor` and `ceil` value calculation --- 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..e262d1cb100 100644 --- a/packages/core/useTimeAgo/index.ts +++ b/packages/core/useTimeAgo/index.ts @@ -57,6 +57,13 @@ export interface UseTimeAgoOptions { * @default false */ showSecond?: boolean + + /** + * Function to apply to the diff to computed the time ago. + * + * @default 'round' + */ + diffEval?: 'round' | 'ceil' | 'floor' } interface UseTimeAgoUnit { @@ -124,9 +131,11 @@ export function useTimeAgo(time: MaybeComputedRef, optio messages = DEFAULT_MESSAGES, fullDateFormatter = DEFAULT_FORMATTER, showSecond = false, + diffEval = 'round', } = options - const { abs, round } = Math + const { abs } = Math + const evalDiff = Math[diffEval] 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 = evalDiff(abs(diff) / unit.value) const past = diff > 0 const str = applyFormat(unit.name, val, past) From b8e6727c6c56ace9b3cd395793a0fa7cdcf57214 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Fri, 16 Dec 2022 15:49:23 +0100 Subject: [PATCH 2/2] chore: upate --- packages/core/useTimeAgo/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/useTimeAgo/index.ts b/packages/core/useTimeAgo/index.ts index e262d1cb100..91165a30525 100644 --- a/packages/core/useTimeAgo/index.ts +++ b/packages/core/useTimeAgo/index.ts @@ -59,11 +59,11 @@ export interface UseTimeAgoOptions { showSecond?: boolean /** - * Function to apply to the diff to computed the time ago. + * Rounding method to apply. * * @default 'round' */ - diffEval?: 'round' | 'ceil' | 'floor' + rounding?: 'round' | 'ceil' | 'floor' } interface UseTimeAgoUnit { @@ -131,11 +131,11 @@ export function useTimeAgo(time: MaybeComputedRef, optio messages = DEFAULT_MESSAGES, fullDateFormatter = DEFAULT_FORMATTER, showSecond = false, - diffEval = 'round', + rounding = 'round', } = options const { abs } = Math - const evalDiff = Math[diffEval] + const roundFn = Math[rounding] const { now, ...controls } = useNow({ interval: updateInterval, controls: true }) function getTimeAgo(from: Date, now: Date) { @@ -169,7 +169,7 @@ export function useTimeAgo(time: MaybeComputedRef, optio } function format(diff: number, unit: UseTimeAgoUnit) { - const val = evalDiff(abs(diff) / unit.value) + const val = roundFn(abs(diff) / unit.value) const past = diff > 0 const str = applyFormat(unit.name, val, past)