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

Quantity parser returns NaN instead of an Error in some edge cases. #7487

Open
mathieu-fournier opened this issue Dec 13, 2024 · 2 comments
Open
Assignees

Comments

@mathieu-fournier
Copy link
Contributor

When parsing two dots .. or two comas ,, the length formatter return NaN instead of an error.
I am parsing using the engineering length parser, in imperial.
I suggest that whenever we parse a NaN value, we convert the result to an error instead.

@rschili
Copy link
Contributor

rschili commented Dec 17, 2024

By error, do you mean returning a ParseError?
I agree, for parseRatioFormat this is already what we do. We should streamline the behavior across the code.

We also still have the split between throwing a QuantityError and returning (not throwing) a ParseError.
The reasoning there is: QuanityError is for unexpected things, like misconfiguration or broken metadata while ParseError is for expected things (we expect parsing to fail frequently with unvalidated inputs). I think keeping it like that makes sense.

@naronchen
Copy link
Contributor

naronchen commented Dec 20, 2024

Hi,

Im not sure which parsing got used here. Currently there are 2 parsing methods in Parser.ts, the async parseIntoQuantity and the sync parseQuantityString.

they both use Parser.parseQuantitySpecification to parse token and there was an error in there handling dots which I addressed in the pr. Now there shouldnt be any NaN value being returned.

The problem with returning an error on async parseIntoQuantity is that it returns a QuantityProps

export interface QuantityProps {
  readonly magnitude: number;
  readonly unit: UnitProps;
  readonly isValid: boolean;
}

it doesnt contain info on what the ParseError is. It does tell whether the returned Quantity is valid. (it did in the call stack calls getQuantityValueFromParseTokens but it didnt return the ParseQuantityResult returned right here

Maybe we can change the return type of the async method to match with the sync one. The sync one returns a QuantityParseResult

export type QuantityParseResult = ParsedQuantity | ParseQuantityError;

export interface ParsedQuantity {
  ok: true;
  value: number; // the magnitude
}

export interface ParseQuantityError {
  ok: false;
  error: ParseError;
}

After the fix in the pr, the sync method returns ParseError.NoValueOrUnitFoundInString when parsing strings with only dots or comas.
The async method just returns an empty quantity right now, but it seemed more reasonable to me that the async method should return the same parse result as the sync one.

Im just not sure how this might affect things cause its changing the return type of a public api.

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

No branches or pull requests

4 participants