-
-
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
Display format update #68
Conversation
Codecov Report
@@ Coverage Diff @@
## master #68 +/- ##
==========================================
- Coverage 100% 98.91% -1.09%
==========================================
Files 3 3
Lines 175 185 +10
Branches 27 33 +6
==========================================
+ Hits 175 183 +8
- Misses 0 2 +2
Continue to review full report at Codecov.
|
Thanks. That's quite a lot of code for this tiny project. We may need a discussion about the display format tokens that we really need. PR pending |
I definitely agree. I broke all of them out into separate commits so it would be possible to remove any of the formats that aren't needed. |
The AM & PM would be nice to have. Why I couldn't even switch my projects over to using DayJS is because of the formatting options. I completely understand you want to keep it small though. |
Would there possibly be a way to extend the format function so that it could accept new display format options from the user, so users could include any extra format options they need without increasing the library size? Several users have mentioned specific display format options that they would use that are missing, but I've also seen several users say that they don't think any more are required since they don't personally use them. That leads me to believe that the solution that would make the most users happy would be finding a way to extend it so users who need the extra format option could have them by including them separately, but the library would stay compact for users who don't need them. Barring that, I think that at most 1-2 of the newly introduced formats should be merged in, for the sake of the size of the library. |
Think we might need a plugin system, like jQuery.
|
I completely agree with the library size, and the plugin system is a great idea. Anyway, we needed this sort of a formatting technics. I would love to see this get implemented soon. |
Returns the quarter of the year, as with Moment.js.
X displays the timestamp in seconds x displays the timestamp in milliseconds.
…conds This does not add SS,SSS, or SSSS, which perform the same function in MomentJS unless using strict mode, in which case only the first digit of milliseconds would be given.
Introduced a plugin system similar to jQuery.fn.extend, which takes an object as a paramter with the name of the function to override or create, and the method to use instead. Ex: //Plugin var plugin = { name: "MyPlugin", method: function() { console.log("my plugin") } } //Usage dayjs().extend(plugin); dayjs().MyPlugin(); Any plugins loaded will overwrite the global dayjs object (via protoype), which will in turn cause any new dayjs instaces to use the new plugin method. The number of additional format options was reduced to h/hh and a/A for 12 hour time and AM/PM.
f46da8d
to
dac9b7e
Compare
I started working on a plugin system. It works by taking in objects in the format of:
So to override the format method, you would do something like:
The plugins are then included in the core by doing:
This operates on the class prototype, so any calls to the function after the plugin is loaded will use the new function. I also reduced the additional formats in the core down to h/hh and a/A for 12 hour time and AM/PM. The rest of the new formats are included in the plugin. The downside to this plugin approach is that all of the code for the format function had to be copied into the advancedFormat plugin as well, but I doubt that the core format function will be extended too much in the future. Let me know what you think! |
Looks cool. But seems too many repeated code in this plugin system design (utils, old format func). this.$u = Utils 2 while extend an API like dayjs.extend(function(args) {
var oldFormat = this.format
var oldresult = this.format(args) // something like this "2018-01-01 Do" 'Do' is not supported in core.format
var result = pluginFormat(oldresult) // process 'Do' return "2018-01-01 1st"
return result
}) 3 global plugin dayjs.extend(advFromat) // instead of `dayjs().extend(advFromat)` |
I definitely agree with 1, I'll move the Utils to a member object so it will be accessible. I really like the approach in 2, since it reduced the amount of duplicated code. Since the current approach involves overwriting the method, I don't think directly calling the old method will work (but I'll need to test it). For example:
Instead, could we move the old functions into an
And then in the plugin:
For number 3, could you explain what you mean a bit more? Even though the extend method is being called on an instance, it's overwriting the original prototype, which is about as global as it gets (it will retro-actively replace the method in any existing instances).
That might not be what a user would expect to happen though, since it is called on an instance instead of the base model. |
@schiem Would you mind cherry-pick your commits about h/hh/a/A as well as related test file into a separate pr please? So that I could merge it before we finish our plugin system design. |
For number 3, my mistake sorry. I haven't noticed that you've overtired it's prototype. I thought it was an instance level change not global. |
@iamkun Absolutely, I'll move those commits over when I get home from work. One of the issues with using the
which means that the
But I'll have to dig around with that before I can say for sure that that approach will work. |
This adds many of the missing display formats referenced in #60 . It does add an additional ~30 lines of code, which is ~10% of the entire library size. I think that some of the options are worthwhile, but there are some that might not be needed.