numbro icon indicating copy to clipboard operation
numbro copied to clipboard

Specify and document a precedence when a postfix can have serveral meanings

Open chaosflaws opened this issue 3 years ago • 2 comments

While working on https://github.com/BenjaminVanRyseghem/numbro/pull/590, I came across ambiguities in the unformatter.

Initially, I tried to use B as the abbreviation for "trillion", as that would be "Billion" in German. This, however, made some tests fail, as B is also a valid postfix in the byte unformatter, representing bytes.

Obviously, numbro cannot solve this problem completely, as strings like "1B" are inherently ambiguous in German. To migitate the problem, however, it would be possible to:

  • define an order of execution/precedence on the formatters (which kind of formatted number does the unformatter try to recognize first/second/...?) and add this to the docs (http://numbrojs.com/format.html#unformat)
  • make the unformatter take a second argument specifying the type of value in question (number, currency, byte, etc.)

chaosflaws avatar Mar 17 '21 09:03 chaosflaws

Thanks for the report.

Indeed, the unformatter is quite ambiguous and fails more that I dare to admit :smile:

I would go for the second approach:

  • it matches better with how format works
  • it could ultimately solves all the issues
  • it's more future-proof

My 2cts :smile:

BenjaminVanRyseghem avatar Mar 17 '21 10:03 BenjaminVanRyseghem

I don't think the two approaches are mutually exclusive. Having a unformat function that only takes the input as argument might be useful where the someone has no clue about what kind of numeric thing has been input. We should just make sure the user knows what he can expect from that function.

Anyways, I am sorry I probably won't have time to tackle this (as a matter of fact, I only came here because I need that language file fix two layers up the stream...).

chaosflaws avatar Mar 17 '21 10:03 chaosflaws