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

Fix inconsistent unit parsing #6599

Merged
merged 10 commits into from
May 29, 2024
Merged

Conversation

a-gagnon
Copy link
Contributor

@a-gagnon a-gagnon commented Apr 9, 2024

Problem:

The current Parser implementation is inconsistent when it comes to handling invalid tokens. When the parser spec contains a unit definition such as units: [{ label: "ft", name: "Units.FT" }])`, we would expect that an invalid token would simply convert the value based off that unit as a fallback, but it doesn't.

This is what parsing returns (output is always meters):
"4.0 ft" ==> 1.2192
"1.234 m" ==> 1.234 Even if we're in feet, there is a known unit that uses the m token (meters), so it interprets it correctly.
"4.0" == > 1.2192 Unitless will use the parser's spec unit definition which is Units.FT to convert the value.
"4.0 abcdef" ==> 4.0 This should have outputted 1.2192. The abcdef token does not relate to any known unit. This should be treated as unitless which would have applied the conversion factor properly.

Fix:

Make sure we consider the unit definition when we fail to find any unit matching the token.

Parsing returns properly for all existing cases, including this one:
"4.0 abcdef" ==> 1.2192

Misc:

When we're parsing two tokens and the unit comes first (eg. currency conversion), just swap the entries in the 'token' array rather than duplicate the logic/code.

@aruniverse aruniverse requested a review from hl662 April 15, 2024 14:58
@raplemie
Copy link
Contributor

@ColinKerr @calebmshafer Any chance this could get reviewed at some point ? Thanks.

@aruniverse aruniverse merged commit a942b50 into master May 29, 2024
16 checks passed
@aruniverse aruniverse deleted the bugfix/inconsistent-unit-parsing branch May 29, 2024 23:18
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

4 participants