After a while in the oven, I’ve just published
ianmackenzie/elm-units 1.0! From the README:
elm-unitsis useful if you want to store, pass around, convert between, compare, or do arithmetic on:
- Durations (seconds, milliseconds, hours…)
- Angles (degrees, radians, turns…)
- Lengths (meters, feet, inches, miles, light years…)
- Temperatures (Celsius, Fahrenheit, kelvins)
- Pixels (whole or partial)
- Speeds (pixels per second, miles per hour…) or any other rate of change
- Any of the other built-in quantity types: areas, accelerations, masses, forces, pressures, currents, voltages…
- Or even values in your own custom units, such as ‘number of tiles’ in a tile-based game
It is aimed especially at engineering/scientific/technical applications but is designed to be generic enough to work well for other fields such as games and finance.
elm-units lets you replace raw
Int values with type-safe
Angle etc. values, helping avoid units mismatches (seconds vs milliseconds, degrees vs radians etc.) without sacrificing much in the way of flexibility or efficiency. Check out the README for a more thorough introduction!
It should already be stable and featureful enough to start using in Elm apps, particularly those with a scientific/engineering focus. I’d like to spend a while gathering feedback on the API, which may lead to some breaking changes and a 2.0 release, but after that I’m hoping to keep the API extremely stable (likely only have major version changes when Elm itself changes). My hope is that once this happens, other packages will be able to confidently use the
elm-units types as part of their public APIs - I’m really excited about the possibilities that would come from having a standard, type-safe, “units-safe” way of passing around
Angles etc. between functions in different packages (where appropriate - in many cases keeping things simple with just a
Float will be the right choice).
Big thanks to all those who helped with initial development of
- @joelq and @adeschamps for in-depth and insightful discussions on Slack
- Everyone at the Elm NYC Meetup and in the #api-design Slack channel for their pre-release feedback
- @tiagorlampert and @mdevlamynck for making PRs to an Elm package that hadn’t even been published yet!
(Sincere apologies to anyone I’ve missed.)
Finally, although I think the most fundamental unit/quantity types are already in place, there will certainly be more added over time - I have a list of ones I’d like to add in issue #6, and in fact there’s already a pull request underway to add a
Volume module. Let me know if there are others you would find useful, and feel free to open a pull request if you’re willing to take a stab at implementing one of them yourself! (Although please leave a comment on the issue before you do, so we can avoid duplicate work.)
Happy to answer questions, take feedback, field requests for new functionality etc. =)