elixir-cldr / cldr_units

Unit formatting (volume, area, length, ...) functions for the Common Locale Data Repository (CLDR)
Other
16 stars 13 forks source link

Parse unit without value #30

Closed Awlexus closed 1 year ago

Awlexus commented 1 year ago

I'm trying to parse some user input where the user can only enter the unit without a value, but I couldn't found a function that - similar to Cldr.Unit.parse/2 - also considers aliases and other forms, like "kg" for "kilogram". Is there I function I might have overlooked? I've tried the following functions, but so far had to settle for Cldr.Unit.parse("1" <> "user_input") to get the unit

Edit: If such a function does not exist, would you be open for a PR?

kipcole9 commented 1 year ago

@Awlexus thanks for the report, and for finding value in the library. Its probably my favourite of the ex_cldr family :-)

Very reasonable request. I think to differentiate it I will call is something like parse_unit_name/2 to make it clear the result is a name, not a unit struct.

I'm definitely open to a PR but I also know some of this code is quite difficult to understand because of how it is rules driven. I won't be able to get to this until the weekend - hopefully that's ok?

Awlexus commented 1 year ago

Thanks for the fast reply! parse_unit_name/2 sounds good.

I've already taken a look how Cldr.Unit.parse/2 is implemented and I think it will be straightforward to derive a function that will only parse the unit name from that, so i'll give it a try tomorrow 👍

kipcole9 commented 1 year ago

I've published ex_cldr_units version 3.14.0 with the following changelog entry:

Enhancements

Thanks for the PR and the collaboration!