Skip to content

Commit

Permalink
fix: Add config option to RelativeTime plugin (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
allmoviestvshowslistsfilmography28 committed Mar 27, 2020
1 parent a0e81ff commit 9daecea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 },
Expand All @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')
})
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ declare namespace dayjs {
locale(preset: string | ILocale, object?: Partial<ILocale>): Dayjs
}

export type PluginFunc = (option: any, c: typeof Dayjs, d: typeof dayjs) => void
export type PluginFunc<T = unknown> = (option: T, c: typeof Dayjs, d: typeof dayjs) => void

export function extend(plugin: PluginFunc, option?: any): Dayjs
export function extend<T = unknown>(plugin: PluginFunc<T>, option?: T): Dayjs

export function locale(preset: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string

Expand Down
13 changes: 12 additions & 1 deletion types/plugin/relativeTime.d.ts
Original file line number Diff line number Diff line change
@@ -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<RelativeTimeOptions>
export = plugin

declare module 'dayjs' {
Expand Down

0 comments on commit 9daecea

Please sign in to comment.