Skip to content

Commit

Permalink
fix: Fix relativeTime plugin Math.round bug
Browse files Browse the repository at this point in the history
* fix: Fix relativeTime plugin error Math.round bug

* chore: add comment

* chore: add 'day' will ignore decimal by dayjs at current version
  • Loading branch information
iamkun authored Apr 8, 2019
1 parent 566873a commit 40bea40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ export default (o, c, d) => {
let out

for (let i = 0; i < Tl; i += 1) {
const t = T[i]
let t = T[i]
if (t.d) {
result = isFrom
? d(input).diff(instance, t.d, true)
: instance.diff(input, t.d, true)
}
const abs = Math.ceil(Math.abs(result))
const abs = Math.round(Math.abs(result))
if (abs <= t.r || !t.r) {
if (abs === 1) t = T[i - 1] // 1 minutes -> a minute
out = loc[t.l].replace('%d', abs)
break
}
Expand Down
12 changes: 12 additions & 0 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@ it('Time from X', () => {
const T = [
[0, 'second'], // a few seconds
[44, 'second'], // a few seconds
[44.4, 'second'], // a few seconds
[44.5, 'second'], // a minute
[45, 'second'], // a minute
[89, 'second'], // a minute
[89.4, 'second'], // a minute
[89.5, 'second'], // a minute
[90, 'second'], // 2 minutes
[44, 'minute'], // 44 minutes
[44.4, 'minute'],
[44.5, 'minute'],
[45, 'minute'], // an hour
[89, 'minute'], // an hour
[89.4, 'minute'],
[89.5, 'minute'],
[90, 'minute'], // 2 hours
[21, 'hour'], // 21 hours
[21.4, 'hour'],
[21.5, 'hour'],
[22, 'hour'], // a day
[35, 'hour'], // a day
[35.4, 'hour'],
[35.5, 'hour'],
[36, 'hour'], // 2 days
[25, 'day'], // 25 days
[26, 'day'], // a month
Expand Down

0 comments on commit 40bea40

Please sign in to comment.