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

Add support to define custom units #112

Closed
wants to merge 1 commit into from

Conversation

phil-mitchell
Copy link

Introduce a new static method to provide runtime addition of units and prefixes. Conflicts are created at the user's peril, but it provides a path to add custom units without forking the repo and modifying source code.

fixes #96

@phil-mitchell
Copy link
Author

I'm not sure it's clear that I don't have write access, so somebody will have to merge this if it's okay.

- `denominator` is an array containing the set of units that can be specified as a denominator for this unit

`unitDefinition` for prefixes is sligly different.
- `aliases` is an array of aliases for the unit, e.g. `["m","meter","meters","metre","metres"]`
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think "meter" is a good example for a prefix.

```

Defining new units should be done carefully and tested thoroughly as it can
introduce conflicts while parsing other units.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not throw an error if you find a collision?

Comment on lines +382 to +389
`unitDefinition` is an array of definition values. For units it is:
- `aliases` is an array of aliases for the unit, e.g. `["m","meter","meters","metre","metres"]`
- `conversion` is a decimal number that can be multiplied to the number to
convert to the base unit
- `kind` is the kind of the unit. If you specify an unknown kind here, be
sure to include a unit with `isBase` set to true for doing conversions
- `numerator` is an array containing the set of units that can be specified as a numerator for this unit
- `denominator` is an array containing the set of units that can be specified as a denominator for this unit
Copy link
Contributor

Choose a reason for hiding this comment

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

Why an array? Why not build the array yourself internally out of an object that's passed in? Less chance of accidentally swapping order that way.

if (isBase) {
if (BASE_UNITS.indexOf(unitDef) === -1) {
BASE_UNITS.push(unitDef);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

else throw error? silent failures are difficult to debug.

PS - What happens if oldDef was a base unit? Does this leave the new one in the expected state?

@@ -1476,6 +1476,27 @@ describe("js-quantities", function() {
});
});

describe("Qty.defineUnit", function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

unit/alias collisions throwing errors, and tests for that would be nice

maybe also a way to remove an existing unit

philippspohn added a commit to philippspohn/js-quantities that referenced this pull request Mar 23, 2023
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.

Defining a new unit
2 participants