fix(customParseFormat): match MMM/MMMM against locale month names - #3157
Open
xianjianlf2 wants to merge 1 commit into
Open
fix(customParseFormat): match MMM/MMMM against locale month names#3157xianjianlf2 wants to merge 1 commit into
xianjianlf2 wants to merge 1 commit into
Conversation
The generic `matchWord` regex used for the MMM/MMMM tokens excludes the
`-_:/,()` separators and whitespace, but not `.`. So parsing an input
such as `dayjs('29.Jul.2021', 'DD.MMM.YYYY')` made the month token greedily
capture `Jul.` (including the following separator dot), which matches no
month name and produced an Invalid Date — a regression from older versions
and a divergence from moment, which parses it correctly.
Rather than blindly adding `.` to the exclusion list (which would break
locales whose short month names legitimately contain a dot, e.g. `gl`'s
`xan.`), MMM/MMMM now match against the actual locale month names
(longest first). The captured value is therefore exactly the month name,
so it no longer swallows a trailing separator, while dotted month names
keep working.
Closes iamkun#2818
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When parsing with
customParseFormat, the generic "word" regex used for theMMM/MMMMmonth-name tokens excludes the-_:/,()separators and whitespace, but not.. As a result, an input likedayjs('29.Jul.2021', 'DD.MMM.YYYY')caused the month token to greedily captureJul.(swallowing the trailing separator dot), which matches no month name and produced anInvalid Date. This is a regression from older versions and a divergence from moment, which parses the same input correctly.Fix
Rather than naively adding
.to the exclusion list (which would break locales whose short month names legitimately contain a dot, e.g.gl'sxan.),MMM/MMMMnow match against the actual locale month names, sorted longest-first. Because the captured value is now exactly one of the real month names, it can no longer swallow a trailing separator, while dotted month names continue to parse correctly.Changes in
src/plugin/customParseFormat/index.js:escapeRegExphelper.matchMonthName(token)which builds a regex from the active locale'smonths(forMMMM) ormonthsShort(forMMM, falling back to the first 3 chars ofmonths), escaped and joined longest-first.MMM/MMMMtokens instead of the generic word regex.Testing
Added tests in
test/plugin/customParseFormat.test.js:29.Jul.2021withDD.MMM.YYYYparses to2021-07-29and matches moment.29.July.2021withDD.MMMM.YYYYparses to2021-07-29and matches moment.gllocale short month containing a dot (xan.) parses correctly with both dot and space separators (29.xan..2021/29 xan. 2021→2021-01-29).Closes #2818