diff --git a/src/plugin/relativeTime/index.js b/src/plugin/relativeTime/index.js index 2a68737..10a8f5d 100644 --- a/src/plugin/relativeTime/index.js +++ b/src/plugin/relativeTime/index.js @@ -1,6 +1,7 @@ import * as C from '../../constant' export default (o, c, d) => { + o = o || {} const proto = c.prototype d.en.relativeTime = { future: 'in %s', @@ -19,7 +20,7 @@ export default (o, c, d) => { } const fromTo = (input, withoutSuffix, instance, isFrom) => { const loc = instance.$locale().relativeTime - const T = [ + const T = o.thresholds || [ { l: 's', r: 44, d: C.S }, { l: 'm', r: 89 }, { l: 'mm', r: 44, d: C.MIN }, @@ -44,10 +45,10 @@ export default (o, c, d) => { ? d(input).diff(instance, t.d, true) : instance.diff(input, t.d, true) } - const abs = Math.round(Math.abs(result)) + const abs = (o.rounding || Math.round)(Math.abs(result)) isFuture = result > 0 if (abs <= t.r || !t.r) { - if (abs === 1 && i > 0) t = T[i - 1] // 1 minutes -> a minute + if (abs <= 1 && i > 0) t = T[i - 1] // 1 minutes -> a minute, 0 seconds -> 0 second const format = loc[t.l] if (typeof format === 'string') { out = format.replace('%d', abs) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index f039ef3..e8a26e0 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -1,6 +1,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' +import * as C from '../../src/constant' import relativeTime from '../../src/plugin/relativeTime' import utc from '../../src/plugin/utc' import '../../src/locale/ru' @@ -115,3 +116,24 @@ it('Time from now with UTC', () => { expect(dutc.fromNow()).toBe(mutc.fromNow()) }) + +it('Custom thresholds and rounding support', () => { + expect(dayjs().subtract(45, 'm').fromNow()).toBe('an hour ago') + dayjs.extend(relativeTime, { + rounding: Math.floor, + thresholds: [ + { l: 's', r: 1 }, + { l: 'm', r: 1 }, + { l: 'mm', r: 59, d: C.MIN }, + { l: 'h', r: 1 }, + { l: 'hh', r: 23, d: C.H }, + { l: 'd', r: 1 }, + { l: 'dd', r: 29, d: C.D }, + { l: 'M', r: 1 }, + { l: 'MM', r: 11, d: C.M }, + { l: 'y' }, + { l: 'yy', d: C.Y } + ] + }) + expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago') +}) diff --git a/types/index.d.ts b/types/index.d.ts index a0f38c8..f971394 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -96,9 +96,9 @@ declare namespace dayjs { locale(preset: string | ILocale, object?: Partial): Dayjs } - export type PluginFunc = (option: any, c: typeof Dayjs, d: typeof dayjs) => void + export type PluginFunc = (option: T, c: typeof Dayjs, d: typeof dayjs) => void - export function extend(plugin: PluginFunc, option?: any): Dayjs + export function extend(plugin: PluginFunc, option?: T): Dayjs export function locale(preset: string | ILocale, object?: Partial, isLocal?: boolean): string diff --git a/types/plugin/relativeTime.d.ts b/types/plugin/relativeTime.d.ts index d08121e..444b0c2 100644 --- a/types/plugin/relativeTime.d.ts +++ b/types/plugin/relativeTime.d.ts @@ -1,6 +1,17 @@ import { PluginFunc, ConfigType } from 'dayjs' -declare const plugin: PluginFunc +declare interface RelativeTimeThreshold { + l: string + r?: number + d?: string +} + +declare interface RelativeTimeOptions { + rounding?: (num: number) => number + thresholds?: RelativeTimeThreshold[] +} + +declare const plugin: PluginFunc export = plugin declare module 'dayjs' {