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

Custom units #497

Open
jnawk opened this issue May 6, 2022 · 3 comments
Open

Custom units #497

jnawk opened this issue May 6, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@jnawk
Copy link

jnawk commented May 6, 2022

Feature request: Ability to define custom units
If this is already possible, some documentation on how to do so would be super.

@jonahsnider jonahsnider added the enhancement New feature or request label May 7, 2022
@jonahsnider jonahsnider self-assigned this May 7, 2022
@jonahsnider
Copy link
Owner

Custom units would be a useful feature to have.

I'd probably implement this by having some kind of API to create an instance of a Converter class which lets you register custom units at runtime somehow. With the current way Convert is written this would be a bit of a hassle to add since all the current code uses the one big lookup table for conversion ratios.

I'm pretty busy these days so unfortunately I can't make any guarantees on when I'll have time to add this feature.

@michaelessiet
Copy link

Any update on this?

@akshay-nm
Copy link

I ended up saving custom units in DB as I had such requirement.
type CustomUnit = { name: string; conversionFactor: number; baseUnit: string; measurementType: string }

So if a bag is 50kg
My custom unit looks like:
{ name: 'bag', conversionFactor: 50, baseUnit: 'kg', measurementType: 'mass' }

to convert to grams, I can do:
convert(customUnit.conversionFactor * value, customUnit.baseUnit).to(targetUnit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants