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

Added math equation support for plus and minus signs. #6969

Conversation

mathieu-fournier
Copy link
Contributor

@mathieu-fournier mathieu-fournier commented Jul 12, 2024

Pull request for the issue feature #6935

Added initial support for parsing math equations. + and -

Plus and minus signs are supported.
Ex:
5 ft + 12 in + 1 yd -1 ft 6 in = 7.5 ft
(5 + 1 + 3 - 1.5)
Multiplication and division are not supported yet.

Special case + Refactoring

I noticed that the functions createQuantityFromParseTokens and getQuantityValueFromParseTokens where doing the same "tokens to value" logic. So I de-duplicated them.
There was one inconsistent output between these methods.
The case where only the unit label was specified. <- See line 601 in Parser.ts.
The output for the 2 methods where : 'FT' = 1 and 'FT' = 0
I opted to assume that the magnitude is 1.

Another special case, which I assume was a bug, is the parsing of 2'-6". <- See line 218 in the file Parsing.test.ts.
The minus sign was threated like a formatTraits fractionDash, but the format did not specified fractionDash, so it should've been 0 or error.
Now it's treated like a minus sign when fractionDash is NOT specified, and a fractionDash when fractionDah is specified.

@CLAassistant
Copy link

CLAassistant commented Jul 12, 2024

CLA assistant check
All committers have signed the CLA.

@aruniverse aruniverse requested review from hl662 and anmolshres98 July 15, 2024 13:55
core/quantity/src/Parser.ts Outdated Show resolved Hide resolved
correcting typeo 'substraction' -> 'subtraction'

Co-authored-by: Nam Le <[email protected]>
@anmolshres98
Copy link
Contributor

Added initial support for parsing math equations. + and -

Plus and minus signs are supported. Ex: 5 ft + 12 in + 1 yd -1 ft 6 in = 7.5 ft (5 + 1 + 3 - 1.5) Multiplication and division are not supported yet.

In cases like these where multiple metrics are supplied (yd, ft, in, m, cm, ...) how do you decide on which to go with?

@mathieu-fournier
Copy link
Contributor Author

Added initial support for parsing math equations. + and -

Plus and minus signs are supported. Ex: 5 ft + 12 in + 1 yd -1 ft 6 in = 7.5 ft (5 + 1 + 3 - 1.5) Multiplication and division are not supported yet.

In cases like these where multiple metrics are supplied (yd, ft, in, m, cm, ...) how do you decide on which to go with?

There are 2 cases :
1 - We use the specified unit given in the ParserSpec or Format parameter. (Usually the default unit displayed in the app, or a conversion to meters to work with the iModel.)
2 - If no default unit, we use the first unit encountered in the input string. (Like in the example above.)

Copy link
Member

@aruniverse aruniverse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to hold this PR and discuss motivations before merging this in

@mathieu-fournier mathieu-fournier requested a review from a team as a code owner July 26, 2024 16:24
mathieu-fournier and others added 4 commits July 26, 2024 15:14
Updated _allowMathematicEquations variable comment
@aruniverse aruniverse requested a review from anmolshres98 July 29, 2024 16:52
@anmolshres98 anmolshres98 requested a review from a team as a code owner July 29, 2024 19:02
Copy link
Member

@aruniverse aruniverse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, maybe this is just an issue of wording or mu interpretation,

should it be mathematic operations not equations ? if so all references to the boolean flag you added need to be updated

docs/changehistory/NextVersion.md Outdated Show resolved Hide resolved
docs/changehistory/NextVersion.md Outdated Show resolved Hide resolved
## Mathematical equations formatting

The formatter now supports parsing mathematical equations. Ex :
```Typescript
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the actual ts code for this? what does it look like?

Copy link
Contributor Author

@mathieu-fournier mathieu-fournier Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's in the tests.
It looks like this :

// Async version 
const quantityProps = await Parser.parseIntoQuantity("2 FT 6 in + 9", format, unitsProvider);

// Sync version
const parseResult = Parser.parseToQuantityValue("2 FT 6 in + 9", format, meterConversionSpecs);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consumers will not be looking at the tests, theyll look at the the changelog here, and this should probably have a ts snippet of the change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users will use the quantity formatter with the boolean flag.
They will not call these directly. Should I add an example anyway ?

docs/changehistory/NextVersion.md Outdated Show resolved Hide resolved
docs/changehistory/NextVersion.md Outdated Show resolved Hide resolved
@mathieu-fournier
Copy link
Contributor Author

Moved to #7019

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.

7 participants