Open piotr-musialek-skyrise opened 3 years ago
Added #36, but it conflicts with current test case '27,681 ns'
=== 27681ns
, not 27.681ns
.
Extending support to any local formats would be a breaking change.
Need additional input - which semantic for 27,681
is anticipated.
Decimal separator solution introduces dynamic regex, hoped to avoid that, but seems unavoidable.
I wonder if manual workaround would do @piotr-musialek-skyrise?
const normDur = str => str.replace(/(\d)\,(\d+[^\d,.])/g, '$1.$2')
parseDuration(normDur('1,500 seconds')) // === 1500
In my opinion it is close to impossible to parse numbers without explicitly telling which decimal separator to use. My example had zeroes at the end, so it gave away a little which is correct, but think about the example like 1,505
. If comma is a decimal separator this should be a little over one and a half. If period is a decimal separator it's over a thousand. Which is it? It's a 50/50 guess.
Therefore I am suggesting a switch/option or something like that. A period should be default, but it would be nice to change it.
Also the workaround you posted seems good enough for me to use it right now, so thanks for that :)
EDIT: I case anyone else would want a workaround, this regex worked for me instead:
str.replace(/(\d),(\d+[^,.])/g, '$1.$2')
As long as a locale is provided or somehow set by the caller, it seems possible to support localized number parsing.
Here is an example:
https://observablehq.com/@mbostock/localized-number-parsing
I found this from the following StackOverflow answer:
How can I set what is treated as a decimal separator? I can easily add support for units in more languages, but half of the world uses comma as a decimal separator and I can't seem to find a way to switch it.
Right now:
parse('1.500 seconds')
- resulting in one and a half secondparse('1,500 seconds')
- resulting in one and a half thousand secondsI need:
parse('1,500 seconds')
- resulting in one and a half secondI understand that it's impossible to parse it both ways, but at least what we could have is a way to override the decimal separator based on our language we use in our apps. Maybe similar to how we add language specific units?
parse['decimalSeparator'] = ',';