Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Date parsing problem when Day's format code is defined before a literal d ('d') #5620

Closed
flaviocysne opened this issue Mar 10, 2016 · 6 comments

Comments

@flaviocysne
Copy link

flaviocysne commented Mar 10, 2016

Bug description:

When using a format where Day's format code is defined before a literal d, the parser is replacing the very last d (format code or literal) with all parsed content.

Ex.:
format string: d 'd'
parsing result: d '10'

format string: 'd' d
parsing result: d 10

Link to minimally-working plunker that reproduces the issue:

http://plnkr.co/edit/ZoWfeb?p=preview

Version of Angular, UIBS, and Bootstrap

Angular: 1.5.0

UIBS: 1.2.4

Bootstrap: 3.3.6

@wesleycho
Copy link
Contributor

This appears to be a legitimate bug.

@wesleycho
Copy link
Contributor

wesleycho commented Apr 6, 2016

I did some investigation into this a week or so ago - this will be a difficult bug to fix. How we construct the formatter is wrong for going from date with format to formatted date. I could not think of a way to do this right when I took a look at this since we need to create a more robust parsing mechanism than what we have available.

This will require some dedicated time to fix this, and I will look into this at some point, but if this is of any urgency for anyone, I highly recommend trying to dedicate resources to fix this right, as I cannot guarantee how fast this will get fixed by me.

@wesleycho
Copy link
Contributor

After typing this out, I have a mental solution sketched out if someone wants to implement.

In the dateparser createParser function, an array of objects needs to be constructed and returned in the object of formatters (so the object returned contains keys regex, map, and formatters). Each formatter object will have information about start index, end index, action (string values of regex or literal), and either the parser for regex/map/etc., or nothing (in the case of a literal).

The formatting of the string will then use this new construction, which will then slice off the string from the original format string each time and apply the appropriate measure (if literal, just append to new format string, if regex, then apply regex replacing).

This approach should fix this bug correctly, instead of the current hacky implementation.

@flaviocysne
Copy link
Author

flaviocysne commented Apr 25, 2016

I created a PEG.js expression from uibDateparser's format codes (to test it, paste the code at http://pegjs.org/online). It generates a matrix for the input format. I'm still trying to reduce the result to an array, but I could not figure it out how yet.
I hope it'll be useful to solve this issue.
I'll try to take a look at the suggested function after work.

https://gist.github.com/flaviocysne/eb2b8197d00dc1adbaa8ceeb481dfa30

@flaviocysne
Copy link
Author

As explained at Array.prototype.reduce() - JavaScript | MDN, the result of the PEG.js parser created (see my previous comment) could be reduced to an ordinary array using Array.prototype.concat() function.

Extracted from the mentioned link:

var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
  return a.concat(b);
}, []);
// flattened is [0, 1, 2, 3, 4, 5]

@flaviocysne
Copy link
Author

@wesleycho I updated the code on gist and added the Javascript created by PEG.js online tool. I also added more examples on http://plnkr.co/edit/ZoWfeb?p=preview (as mentioned in Issue #5665).

I'll try to merge createParser function with the parser.js created by PEG.js.

Could give me some advice on a better way to merge those two codes or, if the answer would be to not merge them, how to start porting PEG.js code to better feet uibDateParser code rules?

I'm not an experienced Javascript developer, although I know some aspects of the language.

Best regards.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants