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

More precise parsing with an array of formats #1094

Merged
merged 2 commits into from
Sep 16, 2013
Merged

More precise parsing with an array of formats #1094

merged 2 commits into from
Sep 16, 2013

Conversation

Timrael
Copy link
Contributor

@Timrael Timrael commented Sep 15, 2013

Commit 1:
When moment parses string with a few formats, only last part of unparsed string has influence on format penalty. For example, for string '11-02-10', formats 'MM DD YY' and 'DD-MM-YY' have same penalty, and it's 0. But as you can see, they have different separators ' ' and '-', that should have influence on penalty.
Now moment can distinguish those separators, and our formats ('MM DD YY' and 'DD-MM-YY') will have penalty 2 and 0 respectively.

Commit 2:
In gettings regex for token, special RegExp chars, such as point, slash, etc, should be escaped to prevent positive matching with other separators. For example, for string '11-02-10', format 'MM.DD.YY' has 0 penalty, because RegExp('.') positive mathes with '-'.
Now moment escapes the chars and our format will have penalty 2.

@ichernev
Copy link
Contributor

Looks good. But how do you make sure that if the format is 'MM-DD-YY' and the string is '01-01-01' there is no penalty?

@Timrael
Copy link
Contributor Author

Timrael commented Sep 16, 2013

It looks how many chars was unparsed and increases penalty by this number. In your case there is 0 unparsed chars.

MM => 01
- => -
DD => 01
- => -
YY => 01

@ichernev
Copy link
Contributor

Ah right, non formatting tokens are matched by the formatting tokens regexp :)

OK, this looks pretty good! Merging!

ichernev added a commit that referenced this pull request Sep 16, 2013
More precise parsing with an array of formats
@ichernev ichernev merged commit 77d5773 into moment:develop Sep 16, 2013
@icambron icambron mentioned this pull request Sep 17, 2013
@icambron icambron mentioned this pull request Sep 27, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants