-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b87c7bc
Implemented ago feature. TODO: Test
79b2b44
Added just now case
873f8cd
Ago inspired by Timeago (+ testing)
70af462
Add hours case
db9cbd5
Updated README
80b13b6
Changed 'ago' to 'from now'
91e9f7a
Fixed autoreplace typo (`fromNow to ago`)
3f49268
Added correct singular labeling semantics when the timeAgo is 1 (year…
ca73a30
Merge branch 'master' into add-ago-feature
iamkun a01ba56
Minor optimization to the method
a7f04f8
Merged @next into add-ago-feature
2230d6c
Integrated locales with timeAgo()
47f7e1a
fixed locale with add/subtract method
c605a9a
Merged 'master'
7812e72
Added fromNow as a plugin
e28c56a
Adapted it to the plugin and locale method
aalises 5130a5e
Reference to Utils fix (in fromNow plugin)
aalises 58e8a3d
fix package.json
aalises b8a9fa4
Implemented relativeTime on locales for the plugin.
e61f356
Small optimization
cf12edb
Difference 'describe' block on display test correctly closed
aalises 76ae72e
Merge branch 'master' into add-ago-feature
iamkun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(() => { | ||
|
@@ -111,44 +112,62 @@ describe('Difference', () => { | |
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true)) | ||
}) | ||
}) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 👍 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @aalises!