-
-
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #83 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 11 12 +1
Lines 275 288 +13
Branches 47 50 +3
=====================================
+ Hits 275 288 +13
Continue to review full report at Codecov.
|
… instead of years, etc), changed some names for constants.
Perfect thanks. see my comment #50 |
src/index.js
Outdated
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' : ''}` |
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!
@iamkun implementation of the timeago with the locales. TO IMPROVE: Maybe in the spanish local find a way to use other properties of the exported object as a template for the plural forms (to reduce code), maybe using getters... Also fixed the add method with the locales (was returning a dayjs object without a specified locale), do not know if this is the correct way to do it (maybe cloning it and then setting the date would be better) |
@aalises Thanks your lovely work. It's great. Some questions:
Something I'm thinking: I think we might better add fromNow as a plugin instead of adding to the core code. We may have to add a lot more functions to meets others need such as timezone, UTC support, calendar time in the future. And if we add every thing to our core code, we can't control the total size. (Another moment in size may be). That's why we have a plugin system now. And we will treat these plugins (mentioned above ) as Day.js extensions with official support rather than user's customized plugin. And added these to our main repo and released with core and locale to NPM. And if you agree with me, another problem comes. Should we add locales used by plugins to our main locale file? If so, the bundle size will increase. If not, how could we ship these plugins' locale? P.S. join us at #123 about locale && plugin |
@iamkun of course! making a plugin out of it would be great. Timeago.js was used for the test cases for its simplicity, but we can use moment as well! and the getter thing was to simplify the locale by reusing object properties to be part (as a string) of another one, but nvm. |
I tend to use moment.js. And any simple example code of 'getter'? Still can't get it, sad. |
@iamkun implemented fromNow as a plugin! Still have to figure out what to do with the locales as the plugin sort of extend them (adding, the future, past and now fields and the plural forms), should we put this fields separately inside the plugin or just add them to the locales? |
Nice plugin! |
Perfect! let me know when it's fixed 👍 So you suggest putting them on plugin/locale? |
Sure. I mean just put them on |
Adapted it to the release plugin and locale. However, for the next and current locales to work, the plural forms also need to be specified (see the spanish local as an example). WDYT? @iamkun |
test/display.test.js
Outdated
@@ -155,44 +155,44 @@ 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 comment
The reason will be displayed to describe this comment to others. Learn more.
why this removal? and why the formatting blew?
src/plugin/fromNow.js
Outdated
@@ -0,0 +1,28 @@ | |||
import * as C from '../constant' | |||
import * as _U from '../utils' |
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.
no need import, plugin could use Utils through this.$utils()
reference here
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.
Perfect! Also, just for consistency and getting rid of a line in the method
const Cs = this.$locale().M ? this.$locale() : C
Should we put the C.D , C.M ... inside the en export? and use it in index.js not as C. something but referencing the locale
package.json
Outdated
@@ -6,7 +6,7 @@ | |||
"types": "index.d.ts", | |||
"scripts": { | |||
"test": "jest", | |||
"lint": "./node_modules/.bin/eslint src/* test/* build/*", | |||
"lint": "./node_modules/.bin/eslint src/* test/* build/* --fix", |
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.
no --fix
, this could cause a CI pass(auto fix) but not qualified code.
@iamkun yes! moving it into the plugin would keep the constant file leaner, but any other locale needs to have the plural forms implemented. And I used timeago.js instead of moment because moment is more complex and wouldn't pass the checks (timeago only has X time ago, in X time, and now), while moment has a lot more semantics. I preferred to keep it simple. |
All right, just timeago.js at the moment. Plus, what I mean is to move en locale to plugin only, and leave the reset to their local files. That is to say, all plugin comes with a built in en locale, the same as core module. But if you want to use other locales, you have to import them ahead. |
Also, consider moment.js's local file: relativeTime : {
future : 'en %s',
past : 'hace %s',
s : 'unos segundos',
ss : '%d segundos',
m : 'un minuto',
mm : '%d minutos',
h : 'una hora',
hh : '%d horas',
d : 'un día',
dd : '%d días',
M : 'un mes',
MM : '%d meses',
y : 'un año',
yy : '%d años'
}, locale comes with a namespace And is |
Yep! @iamkun I have a question regarding how to update the locale. Right now I have:
However this.$locale() is not updated. What should I use there? I am not sure about what d means here... |
plugin(option, Dayjs, dayjs) so d for dayjs function. you could move your code above proto... jist like advancedFormat. And a namespace is needed so that you could just do d.en.relativeTime = {} instead of merge the object. replied on my phone, sorry for the code format. Plus, sorry for the lack of docs, I'll finish it tomorrow. |
@iamkun great! will make those changes sometime tomorrow when I can find some spare time :) |
@iamkun I pushed the changes yesterday night. With that, everything should be done by now I think. Now the locales implement relativeTime for the plugin. WDYT? |
looks great. BTW, what's the reason for changing or formatting |
@iamkun lint autofixer did it for me, you can revert it if you consider it! |
@aalises I think it's not because of the lint auto-fixer. Seems you've deleted one line of code L#158. |
@iamkun Yes, when I first coded the fromNow tests they were on the display.test files under the Difference describe block. When moving them to a sepparate file y accidentally deleted the closing bracket it seems, but did not crash because it was still opened at the bottom. Now the block is correctly scoped |
@aalises Got it. Looks good to me. Will merge into next release. THX 😀 |
@iamkun perfect thanks! we should put on the documentation some template for the locales as now the relativeTime should be part of it in order for the plugin to work. |
@aalises I am doing the docs. tough work 😬 |
Will release in 1.6.4 |
Great! |
Implementation of the fromNow() function of moment based on timeago.js . The API is as follows:
And returns a string with the time passed between the two dates with the following format:
For the sake of simplicity, the time is adjusted to the highest time measurement instead of being unit defined, e.g 60 days will be 2 months , 94670856 seconds will be 2 years, and 76 seconds will be 1 minute , and so on.
Tests are also implemented and passing, which compare the function output with timeago() in various scenarios.
TO DO: Maybe adding some template to change the locale of the messages (change X years ago for X years, or just X...) or to be able to customize them as moment, but to be fair I don't see it as necessary, just adding the chinese version should be it, but it could certainly be implemented. Adding a few lines of code, the version with a specific unit specified (I want it defined in weeks), can also be put, but that would be maybe complicating it a lot?