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

Update math.js to include min, max, and clamp functions #339

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

34638a
Copy link

@34638a 34638a commented Feb 7, 2025

Include of simple math helpers for inline calculations. All support Number types of Int and Float, but not edge case numbers such as NaN, +-infinity, or undefined.

Min: given A and B return the smaller of the two on a standard number line.

Max: given A and B return the larger of the two on a standard number line.

Clamp: given test, min and max return the tested value if it falls within the range of min and max such that min < test < max is satisfied on a standard number line. If the constraint is not satisfied, return the closest bounding constraint.

Note for implementation
File: thirdParty.js in directory helpers/thirdParty.js will also need line 71 to be modified to include the string names of the new functions.
include: ['add', 'subtract', 'divide', 'multiply', 'floor', 'ceil', 'round', 'sum', 'avg', 'min', 'max', 'clamp']

What? Why?

Because these math functions are currently missing from the standard library.

How was it tested?

None required, mirror of existing function helper.add with math operations swapped for logical operations.
Uses inline ternary operators for state evaluation.

Edit:
Adding Of Testing Protocol in comment:

{{log (min 1 5)}}  {{! Resolved to 1}}
{{log (max 1 5)}}  {{! Resolved to 5}}
{{log (clamp 6 1 5)}}  {{! Resolved to 5}}
{{log (clamp -6 1 5)}}  {{! Resolved to 1}}
{{log (clamp 3 1 5)}}  {{! Resolved to 3}}

cc @bigcommerce/storefront-team

Include of simple math helpers for inline calculations. All support Number types of Int and Float, but not edge case numbers such as NaN, +-infinity, or undefined.

Min: given `A` and `B` return the smaller of the two on a standard number line.

Max: given `A` and `B` return the larger of the two on a standard number line.

Clamp: given `test`, `min` and `max` return the tested value if it falls within the range of `min` and `max` such that `min < test < max` is satisfied on a standard number line.  If the constraint is not satisfied, return the closest bounding constraint.
Amendment - Typo,

Testing Protocol
```
{{log (min 1 5)}} {{! Resolved to 1}}
{{log (max 1 5)}} {{! Resolved to 5}}
{{log (clamp 6 1 5)}} {{! Resolved to 5}}
{{log (clamp -6 1 5)}} {{! Resolved to 1}}
{{log (clamp 3 1 5)}} {{! Resolved to 3}}
```
@34638a
Copy link
Author

34638a commented Feb 12, 2025

Amended initial proposal notes with updated comments, and a testing protocol.

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.

1 participant