Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
67 changes: 62 additions & 5 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const unitToMS = {
weeks: MILLISECONDS_A_WEEK
}

const RELATIVE_TIME_UNITS = ['years', 'months', 'days', 'hours', 'minutes', 'seconds']
const RELATIVE_TIME_UNIT_MAP = {
years: 'y',
months: 'M',
days: 'd',
hours: 'h',
minutes: 'm',
seconds: 's'
}

const isDuration = d => d instanceof Duration // eslint-disable-line no-use-before-define

let $d
Expand Down Expand Up @@ -229,11 +239,58 @@ class Duration {
return wrapper(this.$ms, this)
}

humanize(withSuffix) {
return $d()
.add(this.$ms, 'ms')
.locale(this.$l)
.fromNow(!withSuffix)
humanize(options) {
let withSuffix = false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like withSuffix should rather be interval, referring to it being a time interval.

after all this isn't necessarily a suffix, it changes the entire sentence, not only the end of it.

let round = true

if (typeof options === 'boolean') {
withSuffix = options
} else if (typeof options === 'object' && options !== null) {
({ withSuffix, round = true } = options)
}

if (round) {
return $d()
.add(this.$ms, 'ms')
.locale(this.$l)
.fromNow(!withSuffix)
}

const loc = $d().locale(this.$l).$locale().relativeTime
let resultStr = ''
const isFuture = this.$ms > 0

for (let i = 0; i < RELATIVE_TIME_UNITS.length; i += 1) {
const unitName = RELATIVE_TIME_UNITS[i]
const val = this.get(unitName)
if (val) {
const absVal = Math.abs(val)
const unitKey = RELATIVE_TIME_UNIT_MAP[unitName]

let locKey = unitKey
if (absVal !== 1) {
const pluralKey = `${unitKey}${unitKey}`
if (loc[pluralKey]) {
locKey = pluralKey
}
}

const format = loc[locKey]
resultStr = format.replace('%d', absVal)
break
}
}

if (!resultStr) {
resultStr = loc.s
}

if (withSuffix) {
const pastOrFuture = isFuture ? loc.future : loc.past
return pastOrFuture.replace('%s', resultStr)
}

return resultStr
}

valueOf() {
Expand Down
1 change: 1 addition & 0 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default (o, c, d) => {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
m: 'a minute',
mm: '%d minutes',
h: 'an hour',
Expand Down
27 changes: 27 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,30 @@ describe('Format', () => {
.toBe('2/02.0002TEST9:09:6:06:8:08:5:05:1:01:010')
})
})

it('humanize() with round: false option', () => {
expect(dayjs.duration({ minutes: 45 }).humanize({ round: false }))
.toBe('45 minutes')
expect(dayjs.duration({ seconds: 10 }).humanize({ round: false }))
.toBe('10 seconds')
expect(dayjs.duration({ seconds: 1 }).humanize({ round: false }))
.toBe('a few seconds')
Comment on lines +318 to +319

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel like if we say

dayjs.duration({ seconds: 1 }).humanize({ round: false })

it should be a second, to be consistent with

dayjs.duration({ minutes: 1 }).humanize({ round: false })

being a minute

same applies to withSuffix: true

expect(dayjs.duration({ minutes: 1 }).humanize({ round: false }))
.toBe('a minute')
expect(dayjs.duration({ hours: 2 }).humanize({ round: false }))
.toBe('2 hours')
expect(dayjs.duration({ years: 1 }).humanize({ round: false }))
.toBe('a year')
expect(dayjs.duration({ minutes: 45 }).humanize())
.toBe('an hour')
expect(dayjs.duration({ minutes: 45 }).humanize({ round: false, withSuffix: true }))
.toBe('in 45 minutes')
expect(dayjs.duration({ minutes: 45 }).humanize(true))
.toBe('in an hour')
expect(dayjs.duration({ minutes: -45 }).humanize({ round: false, withSuffix: true }))
.toBe('45 minutes ago')
expect(dayjs.duration({ milliseconds: 100 }).humanize({ round: false }))
.toBe('a few seconds')
expect(dayjs.duration({ milliseconds: 200 }).humanize({ round: false, withSuffix: true }))
.toBe('in a few seconds')
})