Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add strict and custom thresholds support for relativeTime plugin #851

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 70 additions & 30 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
import * as C from '../../constant'

const LOOSE_THRESHOLDS = [
{ l: 's', r: 44, d: C.S },
{ l: 'm', r: 89 },
{ l: 'mm', r: 44, d: C.MIN },
{ l: 'h', r: 89 },
{ l: 'hh', r: 21, d: C.H },
{ l: 'd', r: 35 },
{ l: 'dd', r: 25, d: C.D },
{ l: 'M', r: 45 },
{ l: 'MM', r: 10, d: C.M },
{ l: 'y', r: 17 },
{ l: 'yy', d: C.Y }
]

const STRICT_THRESHOLDS = [
{ l: 's', r: 1 },
{ l: 'ss', r: 59, d: C.S },
{ 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 }
]

const LOOSE_EN_LOCALE = {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}

const STRICT_EN_LOCALE = {
future: 'in %s',
past: '%s ago',
s: '%d second', // 0 or 1
ss: '%d seconds',
m: '1 minute',
mm: '%d minutes',
h: '1 hour',
hh: '%d hours',
d: '1 day',
dd: '%d days',
M: '1 month',
MM: '%d months',
y: '1 year',
yy: '%d years'
}

export default (o, c, d) => {
o = o || {}
const proto = c.prototype
d.en.relativeTime = {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
hh: '%d hours',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
d.en.relativeTime = o.strict ? STRICT_EN_LOCALE : LOOSE_EN_LOCALE
const fromTo = (input, withoutSuffix, instance, isFrom) => {
const loc = instance.$locale().relativeTime
const T = [
{ l: 's', r: 44, d: C.S },
{ l: 'm', r: 89 },
{ l: 'mm', r: 44, d: C.MIN },
{ l: 'h', r: 89 },
{ l: 'hh', r: 21, d: C.H },
{ l: 'd', r: 35 },
{ l: 'dd', r: 25, d: C.D },
{ l: 'M', r: 45 },
{ l: 'MM', r: 10, d: C.M },
{ l: 'y', r: 17 },
{ l: 'yy', d: C.Y }
]
const T = o.thresholds || (o.strict ? STRICT_THRESHOLDS : LOOSE_THRESHOLDS)
if (!loc.ss) {
loc.ss = loc.s // locale like Chinese
}
const Tl = T.length
let result
let out
@@ -44,10 +84,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 = Math[o.strict ? 'floor' : 'round'](Math.abs(result))
JounQin marked this conversation as resolved.
Show resolved Hide resolved
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)
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -45,6 +45,7 @@ const prettyUnit = (u) => {
const isUndefined = s => s === undefined

export default {
C, // for custom plugin
JounQin marked this conversation as resolved.
Show resolved Hide resolved
s: padStart,
z: padZoneStr,
m: monthDiff,
37 changes: 37 additions & 0 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
@@ -115,3 +115,40 @@ it('Time from now with UTC', () => {

expect(dutc.fromNow()).toBe(mutc.fromNow())
})

it('Strict support', () => {
expect(dayjs().fromNow()).toBe('a few seconds ago')
expect(dayjs().subtract(45, 's').fromNow()).toBe('a minute ago')
expect(dayjs().subtract(45, 'm').fromNow()).toBe('an hour ago')
dayjs.extend(relativeTime, {
strict: true
})
expect(dayjs().fromNow()).toBe('0 second ago')
expect(dayjs().subtract(1, 's').fromNow()).toBe('1 second ago')
expect(dayjs().subtract(45, 's').fromNow()).toBe('45 seconds ago')
expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago')
})

it('Custom thresholds support', () => {
const { C } = dayjs().$utils()
dayjs.extend(relativeTime, {
thresholds: [
{ l: 's', r: 44, d: C.S },
{ 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: 30, d: C.D },
{ l: 'M', r: 1 },
{ l: 'MM', r: 11, d: C.M },
{ l: 'y' },
{ l: 'yy', d: C.Y }
]
})
expect(dayjs().fromNow()).toBe('a few seconds ago')
expect(dayjs().subtract(44, 's').fromNow()).toBe('a few seconds ago')
expect(dayjs().subtract(45, 's').fromNow()).toBe('a minute ago')
expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago')
expect(dayjs().subtract(60, 'm').fromNow()).toBe('an hour ago')
})
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
JounQin marked this conversation as resolved.
Show resolved Hide resolved

export function extend(plugin: PluginFunc, option?: any): Dayjs
export function extend<T = unknown>(plugin: PluginFunc<T>, option?: T): Dayjs
JounQin marked this conversation as resolved.
Show resolved Hide resolved

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

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 {
strict?: boolean
thresholds?: RelativeTimeThreshold[]
}

declare const plugin: PluginFunc<RelativeTimeOptions>
export = plugin

declare module 'dayjs' {