fmenezes/json-stringify-date

7 digit strings are improperly detected as a date

Closed this issue · 2 comments

Describe the bug
This is a regression of #54 where 4 digit strings have been improperly detected as a date. In 165afc2 an additional check has been introduced that the string must have at least 6 characters.

In my current situation the string "2022338" in an object will get parsed as a date.

To Reproduce

const x = jsonStringifyDate.parse('{"str":"2022338"}');
console.log(x.str); // PRINTS: Sun Dec 04 2022 00:00:00 GMT+0100 (Central European Standard Time)

The interesting part here is, that Dec 4ᵗʰ is the 338ᵗʰ day of the year. So "2022367" would not parse as a date. See moment.js - Supported ISO 8601 strings for details.

Expected behavior
Expect it to parse it as a string and not a date.

In my case the value represents a customer number but could also be i.e. a matriculation number (in Austria 8 digits).

Sugested solution
Don't allow the "Basic (short)" date formats allowed by ISO 8601 to be parsed as date (optionally provide a setting to enable these formats which is disabled by default).
If short date formats are disabled test all strings > 4 and <= 8 characters if they have a - as 5ᵗʰ character.

@mfrieling thanks for the bug report, I'll look into it

Closing as the problem can be solved individually using the fnCheck function by either checking the key name or the value itself from #125. This even allows more fine-grained control over the parsing process.