Skip to content

Commit c7d09ef

Browse files
authored
feat(useTimeAgoIntl): custom units (#5188)
1 parent 2e4c165 commit c7d09ef

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/core/useTimeAgoIntl/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export interface FormatTimeAgoIntlOptions {
3434
* If provided, it will be used instead of the default join logic.
3535
*/
3636
joinParts?: (parts: Intl.RelativeTimeFormatPart[], locale?: Intl.UnicodeBCP47LocaleIdentifier | Intl.Locale) => string
37+
38+
/**
39+
* Custom units
40+
*/
41+
units?: TimeAgoUnit[]
3742
}
3843

3944
export interface UseTimeAgoIntlOptions<Controls extends boolean> extends FormatTimeAgoIntlOptions {
@@ -136,7 +141,8 @@ function getTimeAgoIntlResult(
136141
const diff = +from - +now
137142
const absDiff = Math.abs(diff)
138143

139-
for (const { name, ms } of UNITS) {
144+
const units = options.units ?? UNITS
145+
for (const { name, ms } of units) {
140146
if (absDiff >= ms) {
141147
return {
142148
resolvedLocale,
@@ -147,7 +153,7 @@ function getTimeAgoIntlResult(
147153

148154
return {
149155
resolvedLocale,
150-
parts: rtf.formatToParts(0, 'second'),
156+
parts: rtf.formatToParts(0, units[units.length - 1].name),
151157
}
152158
}
153159

0 commit comments

Comments
 (0)