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

Implement simple fromNow() API based on timeago.js #83

Merged
merged 22 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from 9 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
10 changes: 10 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ dayjs().diff(Dayjs, unit);
dayjs().diff(dayjs(), 'years'); // 0
```

#### fromNow

* returns a String

Get the time that has passed between two `Dayjs` object. inspired by [timeago.js](http://timefromNow.org/)

```js
dayjs().fromNow(Dayjs); //e.g 5 years fromNow, in 2 hours, just now, 6 minutes fromNow, in 10 seconds...
```

#### Unix Timestamp (milliseconds)

* returns a Number
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"pre-commit": "^1.2.2",
"rollup": "^0.57.1",
"rollup-plugin-babel": "^4.0.0-beta.4",
"rollup-plugin-uglify": "^3.0.0"
"rollup-plugin-uglify": "^3.0.0",
"timeago.js": "^3.0.2"
},
"dependencies": {
}
"dependencies": {}
}
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ class Dayjs {
return float ? result : Utils.absFloor(result)
}

fromNow(input) {
const P = [{ l: C.S, v: C.MILLISECONDS_A_SECOND }, { l: C.M, v: C.MILLISECONDS_A_SECOND * 60 },
{ l: C.H, v: C.MILLISECONDS_A_HOUR }, { l: C.D, v: C.MILLISECONDS_A_DAY },
{ l: C.W, v: C.MILLISECONDS_A_WEEK }, { l: C.M, v: C.MILLISECONDS_A_WEEK * 4 },
{ l: C.Q, v: C.MILLISECONDS_A_WEEK * 12 }, { l: C.Y, v: C.MILLISECONDS_A_WEEK * 4 * 12 }]

const result = input.diff(this, `${C.MS}s`)
const resabs = Math.abs(result)
let out = ''
for (let i = 0; i < P.length; i += 1) {
if (resabs >= P[i].v) out = `${Utils.absFloor(resabs / P[i].v)} ${P[i].l}${(resabs / P[i].v !== 1) ? 's' : ''}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment to this part:
(resabs / P[i].v !== 1) ? 's' : ''
Probably this is equal to:
resabs !== P[i].v ? 's': ''
without division it must be a bit faster :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you very much @Imperat ! it is indeed cleaner and faster. Just pushed the change (some merge conflicts have to be resolved though 👍 )

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you, @aalises!

}
if (!result) return 'just now'
return result > 0 ? `in ${out}` : `${out} ago`
}

daysInMonth() {
return this.endOf(C.M).$D
}
Expand Down
81 changes: 50 additions & 31 deletions test/display.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from 'moment'
import MockDate from 'mockdate'
import timeago from 'timeago.js'
import dayjs from '../src'

beforeEach(() => {
Expand Down Expand Up @@ -111,44 +112,62 @@ describe('Difference', () => {
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})
})
Copy link
Owner

Choose a reason for hiding this comment

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

why this removal? and why the formatting blew?

it('fromNow -> in seconds, days, weeks, months, quarters, years ', () => {
const dayjsA = dayjs()
const dayjsB = dayjs().add(1000, 'days')
const dayjsC = dayjs().subtract(1000, 'days')
const dayjsD = dayjs().add(20, 'days')
const dayjsE = dayjs().subtract(30, 'seconds')
const dayjsF = dayjs().subtract(5, 'hours')
const dayjsG = dayjs().add(1001, 'days')

expect(dayjsA.fromNow(dayjsB)).toBe(timeago(dayjsA.toDate()).format(dayjsB.toDate()))
expect(dayjsA.fromNow(dayjsC)).toBe(timeago(dayjsA.toDate()).format(dayjsC.toDate()))
expect(dayjsA.fromNow(dayjsD)).toBe(timeago(dayjsA.toDate()).format(dayjsD.toDate()))
expect(dayjsA.fromNow(dayjsE)).toBe(timeago(dayjsA.toDate()).format(dayjsE.toDate()))
expect(dayjsA.fromNow(dayjsF)).toBe(timeago(dayjsA.toDate()).format(dayjsF.toDate()))
expect(dayjsB.fromNow(dayjsC)).toBe(timeago(dayjsB.toDate()).format(dayjsC.toDate()))
expect(dayjsB.fromNow(dayjsB)).toBe(timeago(dayjsB.toDate()).format(dayjsB.toDate()))
expect(dayjsG.fromNow(dayjsB)).toBe(timeago(dayjsG.toDate()).format(dayjsB.toDate()))
})

it('Unix Timestamp (milliseconds)', () => {
expect(dayjs().valueOf()).toBe(moment().valueOf())
})
it('Unix Timestamp (milliseconds)', () => {
expect(dayjs().valueOf()).toBe(moment().valueOf())
})

it('Unix Timestamp (seconds)', () => {
expect(dayjs().unix()).toBe(moment().unix())
})
it('Unix Timestamp (seconds)', () => {
expect(dayjs().unix()).toBe(moment().unix())
})

it('Days in Month', () => {
expect(dayjs().daysInMonth()).toBe(moment().daysInMonth())
expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
})
it('Days in Month', () => {
expect(dayjs().daysInMonth()).toBe(moment().daysInMonth())
expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
})

it('As Javascript Date -> toDate', () => {
const base = dayjs()
const momentBase = moment()
const jsDate = base.toDate()
expect(jsDate).toEqual(momentBase.toDate())
expect(jsDate).toEqual(new Date())
it('As Javascript Date -> toDate', () => {
const base = dayjs()
const momentBase = moment()
const jsDate = base.toDate()
expect(jsDate).toEqual(momentBase.toDate())
expect(jsDate).toEqual(new Date())

jsDate.setFullYear(1970)
expect(jsDate.toUTCString()).not.toBe(base.toString())
})
jsDate.setFullYear(1970)
expect(jsDate.toUTCString()).not.toBe(base.toString())
})

it('As Array -> toArray', () => {
expect(dayjs().toArray()).toEqual(moment().toArray())
})
it('As Array -> toArray', () => {
expect(dayjs().toArray()).toEqual(moment().toArray())
})

it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
})
it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
})

it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})

it('As Object -> toObject', () => {
expect(dayjs().toObject()).toEqual(moment().toObject())
it('As Object -> toObject', () => {
expect(dayjs().toObject()).toEqual(moment().toObject())
})
})