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

Preparse postformat + implementation in ar locale + tests #1255

Merged
merged 6 commits into from
Jan 2, 2021

Conversation

VehpuS
Copy link
Contributor

@VehpuS VehpuS commented Dec 6, 2020

Added a plugin to implement the preparse / postformat functions for locales such as "ar", based on the moment implementation. I added tests for both the plugin and the locale changes based on the moment tests.

This should solve #416

Do note that the relative time implementation for the ar locale does not match the moment one, so the tests for the locale are different (to focus on coverage and not correctness tests - since I am not an arabic speaker and could not fix the relative time implementation in the scope of this change).

@codecov
Copy link

codecov bot commented Dec 6, 2020

Codecov Report

Merging #1255 (e565267) into dev (9544ed2) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##               dev     #1255   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          174       175    +1     
  Lines         1653      1697   +44     
  Branches       374       387   +13     
=========================================
+ Hits          1653      1697   +44     
Impacted Files Coverage Δ
src/constant.js 100.00% <100.00%> (ø)
src/index.js 100.00% <100.00%> (ø)
src/locale/ar.js 100.00% <100.00%> (ø)
src/plugin/customParseFormat/index.js 100.00% <100.00%> (ø)
src/plugin/duration/index.js 100.00% <100.00%> (ø)
src/plugin/preParsePostFormat/index.js 100.00% <100.00%> (ø)
src/plugin/relativeTime/index.js 100.00% <100.00%> (ø)
src/plugin/timezone/index.js 100.00% <100.00%> (ø)
src/locale/he.js 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9544ed2...e565267. Read the comment docs.

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 23, 2020

@iamkun just making sure: is there something that needs fixing / changing before this can be committed?

@iamkun
Copy link
Owner

iamkun commented Dec 24, 2020

First of all, thanks for this PR.

One thing I did not get clearly is, seems it changed the original code too much to support this feature.

And I'm afraid I did not know this issue much.

Any more detail you can give us, please? Or is it better for us to leave it as a separate plugin package?

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 24, 2020

Hi @iamkun - the changes to the original code are because I did a merge commit to my branch following a change you've made to the dev branch (commit acd8736). I did it to ensure my code will merge well with the branch. I can try to create a new pull request without the merge commit if it's confusing - I was only trying to ensure tests pass with the most recent code. I don't think it should mess anything up frankly...

The only manual change I made to existing (plugin) code was to the relative time plugin, where I made an inner logic overridable through the prototype chain to support postFormatting (see fromToBase in the relativeTime changes).

Let me know how you want me to proceed :)

@iamkun
Copy link
Owner

iamkun commented Dec 24, 2020

Ok, I see.

How should we use this plugin?

As far as I can see, if I want to get the correct ar locale in relative time, should we enable preParsePostFormat plugin first?

It's a little bit wired to me, to be honest.

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 24, 2020

Ok, I see.

How should we use this plugin?

As far as I can see, if I want to get the correct ar locale in relative time, should we enable preParsePostFormat plugin first?

It's a little bit wired to me, to be honest.

The plugin works fine with or without the relative time component. It is not a requirement.

@iamkun
Copy link
Owner

iamkun commented Dec 24, 2020

Then what's this plugin used for? I've checked the code and think it might be related to ar locale relative time format string.

Correct me if I am wrong.

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 24, 2020

Now I understand the confusion.

Preparse / postformat is a feature in moment that lets you process the input before the parser and process the string output after the formatter.

In the AR locale specifically, it is used to support Arabic numerals (see the last two commits). I needed to include support for the relative time plugin because in moment it is also processed by this logic.

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 24, 2020

It is also used in other locales an I personally used it in a custom locale for work on moment

@iamkun
Copy link
Owner

iamkun commented Dec 24, 2020

Thanks for this background information.

As I am not familiar with the concept, could you please give some reference of moment.js in this topic we've talked about?

This could give me a more detailed understanding of this PR.

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 24, 2020

In the docs: https://momentjs.com/docs/#/i18n/locale-data/

In the code: https://github.com/moment/moment/blob/b7ec8e2ec068e03de4f832f28362675bb9e02261/moment.js
(search for preparse / postformat - there aren't many uses in the code)

Moment.js test (on which I based the dayjs tests - commit
0211f07): https://github.com/moment/moment/blob/develop/src/test/moment/preparse_postformat.js

@iamkun
Copy link
Owner

iamkun commented Dec 25, 2020

THX. I'll take a look at the doc you've provied.

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 30, 2020

THX. I'll take a look at the doc you've provied.

Did you get a chance to look at the references?

export default (option, dayjsClass, dayjsFactory) => {
// This plugin depends on other plugins - so I will import them here
// equivalent to dayjsClass.extend(duration)
localeData(option, dayjsClass, dayjsFactory)
Copy link
Owner

Choose a reason for hiding this comment

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

we don't have to import other plugins like this.

Just listing the dependent plugin in the doc.

@iamkun
Copy link
Owner

iamkun commented Dec 31, 2020

Yes, it looks good to me. If I want to get the correct ar locale result, should I use it in this way

dayjs.extend(localeData)
dayjs.extend(preParsePostFormat)
dayjs.loclae('ar')

dayjs().formNow()

@VehpuS
Copy link
Contributor Author

VehpuS commented Dec 31, 2020

Yes, it looks good to me. If I want to get the correct ar locale result, should I use it in this way

dayjs.extend(localeData)
dayjs.extend(preParsePostFormat)
dayjs.loclae('ar')

dayjs().formNow()

You actually don't need to extend localeData, the plugin takes care of it for you.

The ar tests I've added demonstrate the usage of the plugin (a5b9200).

@iamkun
Copy link
Owner

iamkun commented Jan 1, 2021

Yes, you can see the comment above, I don't think it's a good idea to import localeData plugin into this plugin

@VehpuS
Copy link
Contributor Author

VehpuS commented Jan 1, 2021

Yes, you can see the comment above, I don't think it's a good idea to import localeData plugin into this plugin

I don't mind fixing this (it's quite simple) but I think it's a bad idea for two reasons:

  1. The plugin cannot function without loading localeData - so it either becomes irrelevant without the other plugin or requires replicating logic from the localeData plugin to read the additional data from the locale file.
  2. If we require the user to write something every time they use the plugin, isn't it preferable to save them the trouble?

Let me know what you think - if you insist I will of course conform to your decision :)

Best regards and happy new year!

@iamkun
Copy link
Owner

iamkun commented Jan 1, 2021

Cause by design, a plugin could be dependent on another one.

https://day.js.org/docs/en/plugin/advanced-format

Check format Week of year as an example here.

@VehpuS
Copy link
Contributor Author

VehpuS commented Jan 1, 2021

Cause by design, a plugin could be dependent on another one.

https://day.js.org/docs/en/plugin/advanced-format

Check format Week of year as an example here.

As I said - it's a pretty simple fix. I've made it and tests seem to still pass. Let me know if you need me to make any additional changes.

@iamkun
Copy link
Owner

iamkun commented Jan 2, 2021

LGTM. Let get this version released and see if there's any improvement that we could do.

@iamkun iamkun merged commit f2e4790 into iamkun:dev Jan 2, 2021
@VehpuS VehpuS mentioned this pull request Jan 2, 2021
iamkun pushed a commit that referenced this pull request Jan 3, 2021
# [1.10.0](v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](#1266)) ([fd229fa](fd229fa))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](#1255)) ([f2e4790](f2e4790))
* add type support for plural forms of units ([#1289](#1289)) ([de49bb1](de49bb1))
* escape last period to match only milliseconds ([#1239](#1239)) ([#1295](#1295)) ([64037e6](64037e6))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](#1298)) ([f63375d](f63375d)), closes [#598](#598) [#313](#313)
@iamkun
Copy link
Owner

iamkun commented Jan 3, 2021

🎉 This PR is included in version 1.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun iamkun added the released label Jan 3, 2021
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this pull request May 10, 2022
# [1.10.0](iamkun/dayjs@v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](iamkun/dayjs#1266)) ([fd229fa](iamkun/dayjs@fd229fa))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](iamkun/dayjs#1255)) ([f2e4790](iamkun/dayjs@f2e4790))
* add type support for plural forms of units ([#1289](iamkun/dayjs#1289)) ([de49bb1](iamkun/dayjs@de49bb1))
* escape last period to match only milliseconds ([#1239](iamkun/dayjs#1239)) ([#1295](iamkun/dayjs#1295)) ([64037e6](iamkun/dayjs@64037e6))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](iamkun/dayjs#1298)) ([f63375d](iamkun/dayjs@f63375d)), closes [#598](iamkun/dayjs#598) [#313](iamkun/dayjs#313)
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this pull request May 10, 2022
# [1.10.0](iamkun/dayjs@v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](iamkun/dayjs#1266)) ([fd229fa](iamkun/dayjs@fd229fa))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](iamkun/dayjs#1255)) ([f2e4790](iamkun/dayjs@f2e4790))
* add type support for plural forms of units ([#1289](iamkun/dayjs#1289)) ([de49bb1](iamkun/dayjs@de49bb1))
* escape last period to match only milliseconds ([#1239](iamkun/dayjs#1239)) ([#1295](iamkun/dayjs#1295)) ([64037e6](iamkun/dayjs@64037e6))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](iamkun/dayjs#1298)) ([f63375d](iamkun/dayjs@f63375d)), closes [#598](iamkun/dayjs#598) [#313](iamkun/dayjs#313)
Copy link

@ahmadconnectedmotion ahmadconnectedmotion left a comment

Choose a reason for hiding this comment

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

LGTM

allmoviestvshowslistsfilmography28 added a commit to allmoviestvshowslistsfilmography28/dayjs that referenced this pull request Sep 12, 2024
# [1.10.0](iamkun/dayjs@v1.9.8...v1.10.0) (2021-01-03)

### Bug Fixes

* add ordinal to localeData plugin ([#1266](iamkun/dayjs#1266)) ([33cd889](iamkun/dayjs@33cd889))
* add preParsePostFormat plugin & update Arabic [ar] locale ([#1255](iamkun/dayjs#1255)) ([1d5566d](iamkun/dayjs@1d5566d))
* add type support for plural forms of units ([#1289](iamkun/dayjs#1289)) ([05d8ea8](iamkun/dayjs@05d8ea8))
* escape last period to match only milliseconds ([#1239](iamkun/dayjs#1239)) ([#1295](iamkun/dayjs#1295)) ([7bbfe0a](iamkun/dayjs@7bbfe0a))

### Features

* add ES6 Module Support, package.json module point to "esm/index.js" ([#1298](iamkun/dayjs#1298)) ([85bd4a1](iamkun/dayjs@85bd4a1)), closes [#598](iamkun/dayjs#598) [#313](iamkun/dayjs#313)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants