Skip to content

Commit

Permalink
Add Cldr.Unit.parse_unit_name/2
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Oct 8, 2022
1 parent c466afa commit 0c4260f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 46 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Cldr_Units v3.14.0

This is the changelog for Cldr_units v3.14.0 released on October 8th, 2022. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_units/tags)

### Enhancements

* Add `Cldr.Unit.parse_unit_name/2` to parse a string as unit name. Also adds `MyApp.Cldr.parse_unit_name/2` as well as the `!` versions of these functions. Thanks to @Awlexus for the PR. Closes #31.

## Cldr_Units v3.13.3

This is the changelog for Cldr_units v3.13.3 released on August 3rd, 2022. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_units/tags)
Expand Down
47 changes: 25 additions & 22 deletions lib/cldr/unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ defmodule Cldr.Unit do
end
end

@doc since: "3.13.4"
@doc """
Parse a string to find a matching unit-atom.
Expand Down Expand Up @@ -649,34 +648,38 @@ defmodule Cldr.Unit do
iex> Cldr.Unit.parse_unit_name "kg"
{:ok, :kilogram}
iex> Cldr.Unit.parse "w"
iex> Cldr.Unit.parse_unit_name "w"
{:ok, :watt}
iex> Cldr.Unit.parse "w", only: :duration
iex> Cldr.Unit.parse_unit_name "w", only: :duration
{:ok, :week}
iex> Cldr.Unit.parse "m", only: [:year, :month, :day]
iex> Cldr.Unit.parse_unit_name "m", only: [:year, :month, :day]
{:ok, :month}
iex> Cldr.Unit.parse "tages", locale: "de"
iex> Cldr.Unit.parse_unit_name "tages", locale: "de"
{:ok, :day}
iex> Cldr.Unit.parse "tag", locale: "de"
iex> Cldr.Unit.parse_unit_name "tag", locale: "de"
{:ok, :day}
iex> Cldr.Unit.parse("millispangels")
iex> Cldr.Unit.parse_unit_name("millispangels")
{:error, {Cldr.UnknownUnitError, "Unknown unit was detected at \\"spangels\\""}}
"""
@doc since: "3.14.0"

@spec parse_unit_name(binary, Keyword.t()) :: {:ok, atom} | {:error, {module(), binary()}}
def parse_unit_name(unit_name_string, options \\ []) do
{locale, backend} = Cldr.locale_and_backend_from(options)

with {:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, strings} <- Module.concat([backend, :Unit]).unit_strings_for(locale),
units = resolve_unit_alias(unit_name_string, strings),
{:ok, {only, except, _}} <- get_filter_options(options) do
unit_matching_filter(unit_name_string, units, only, except)
{:ok, {only, except, _}} <- get_filter_options(options),
{:ok, unit} = unit_matching_filter(unit_name_string, units, only, except),
{:ok, unit, _base_conversion} <- validate_unit(unit) do
{:ok, unit}
end
end

Expand Down Expand Up @@ -830,8 +833,7 @@ defmodule Cldr.Unit do
{:error, {exception, message}} -> raise exception, message
end
end

@doc since: "3.13.4"

@doc """
Parse a string to find a matching unit-atom.
Expand Down Expand Up @@ -884,28 +886,30 @@ defmodule Cldr.Unit do
## Examples
iex> Cldr.Unit.parse_unit_name "kg"
iex> Cldr.Unit.parse_unit_name! "kg"
:kilogram
iex> Cldr.Unit.parse "w"
iex> Cldr.Unit.parse_unit_name! "w"
:watt
iex> Cldr.Unit.parse "w", only: :duration
:week}
iex> Cldr.Unit.parse_unit_name! "w", only: :duration
:week
iex> Cldr.Unit.parse "m", only: [:year, :month, :day]
:month}
iex> Cldr.Unit.parse_unit_name! "m", only: [:year, :month, :day]
:month
iex> Cldr.Unit.parse "tages", locale: "de"
:day}
iex> Cldr.Unit.parse_unit_name! "tages", locale: "de"
:day
iex> Cldr.Unit.parse "tag", locale: "de"
iex> Cldr.Unit.parse_unit_name! "tag", locale: "de"
:day
iex> Cldr.Unit.parse("millispangels")
iex> Cldr.Unit.parse_unit_name!("millispangels")
** (Cldr.UnknownUnitError) Unknown unit was detected at "spangels"
"""
@doc since: "3.14.0"

@spec parse_unit_name!(binary) :: atom() | no_return()
def parse_unit_name!(unit_name_string, options \\ []) do
case parse_unit_name(unit_name_string, options) do
Expand All @@ -914,7 +918,6 @@ defmodule Cldr.Unit do
end
end


defp resolve_unit_alias(unit, strings) do
unit = String.trim(unit)
Map.get(strings, unit, unit)
Expand Down
32 changes: 16 additions & 16 deletions lib/cldr/unit/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -596,22 +596,22 @@ defmodule Cldr.Unit.Backend do
iex> #{inspect(__MODULE__)}.parse_unit_name "kg"
{:ok, :kilogram}
iex> #{inspect(__MODULE__)}.parse "w"
iex> #{inspect(__MODULE__)}.parse_unit_name "w"
{:ok, :watt}
iex> #{inspect(__MODULE__)}.parse "w", only: :duration
iex> #{inspect(__MODULE__)}.parse_unit_name "w", only: :duration
{:ok, :week}
iex> #{inspect(__MODULE__)}.parse "m", only: [:year, :month, :day]
iex> #{inspect(__MODULE__)}.parse_unit_name "m", only: [:year, :month, :day]
{:ok, :month}
iex> #{inspect(__MODULE__)}.parse "tages", locale: "de"
iex> #{inspect(__MODULE__)}.parse_unit_name "tages", locale: "de"
{:ok, :day}
iex> #{inspect(__MODULE__)}.parse "tag", locale: "de"
iex> #{inspect(__MODULE__)}.parse_unit_name "tag", locale: "de"
{:ok, :day}
iex> #{inspect(__MODULE__)}.parse("millispangels")
iex> #{inspect(__MODULE__)}.parse_unit_name("millispangels")
{:error, {Cldr.UnknownUnitError, "Unknown unit was detected at \\"spangels\\""}}
"""
Expand Down Expand Up @@ -731,25 +731,25 @@ defmodule Cldr.Unit.Backend do
## Examples
iex> #{inspect(__MODULE__)}.parse_unit_name "kg"
iex> #{inspect(__MODULE__)}.parse_unit_name! "kg"
:kilogram
iex> #{inspect(__MODULE__)}.parse "w"
iex> #{inspect(__MODULE__)}.parse_unit_name! "w"
:watt
iex> #{inspect(__MODULE__)}.parse "w", only: :duration
:week}
iex> #{inspect(__MODULE__)}.parse_unit_name! "w", only: :duration
:week
iex> #{inspect(__MODULE__)}.parse "m", only: [:year, :month, :day]
:month}
iex> #{inspect(__MODULE__)}.parse_unit_name! "m", only: [:year, :month, :day]
:month
iex> #{inspect(__MODULE__)}.parse "tages", locale: "de"
:day}
iex> #{inspect(__MODULE__)}.parse_unit_name! "tages", locale: "de"
:day
iex> #{inspect(__MODULE__)}.parse "tag", locale: "de"
iex> #{inspect(__MODULE__)}.parse_unit_name! "tag", locale: "de"
:day
iex> #{inspect(__MODULE__)}.parse("millispangels")
iex> #{inspect(__MODULE__)}.parse_unit_name!("millispangels")
** (Cldr.UnknownUnitError) Unknown unit was detected at "spangels"
"""
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.Units.Mixfile do
use Mix.Project

@version "3.13.3"
@version "3.14.0"

def project do
[
Expand Down
14 changes: 7 additions & 7 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
%{
"benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"},
"cldr_utils": {:hex, :cldr_utils, "2.18.0", "674a2941abfd501a338b02244985feb7e526b1b41417b305c45a682e1f7b07b0", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "b435777ae33e8e86cfbeb96437881cf751d5062f366dacdae2e6548f83257a52"},
"cldr_utils": {:hex, :cldr_utils, "2.19.1", "5a7bcd2f2fd432c548e494e850bba8a9e838f1b10202f682ea1d9809d74eff31", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "fbd10f79363e70f3d893ab21e195f444ca87c2c80120b5911761491da4489620"},
"coerce": {:hex, :coerce, "1.0.1", "211c27386315dc2894ac11bc1f413a0e38505d808153367bd5c6e75a4003d096", [:mix], [], "hexpm", "b44a691700f7a1a15b4b7e2ff1fa30bebd669929ac8aa43cffe9e2f8bf051cf1"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
"digital_token": {:hex, :digital_token, "0.4.0", "2ad6894d4a40be8b2890aad286ecd5745fa473fa5699d80361a8c94428edcd1f", [:mix], [{:cldr_utils, "~> 2.17", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "a178edf61d1fee5bb3c34e14b0f4ee21809ee87cade8738f87337e59e5e66e26"},
"earmark": {:hex, :earmark, "1.4.6", "f14260802d8998f30e1654189a0d1699c4aa7d099eb4dad904eb5f41283a70b2", [:mix], [], "hexpm", "c1653a90f63400d029b783a098f4d70a40418ddff14da4cc156a0fee9e72e8d6"},
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"earmark_parser": {:hex, :earmark_parser, "1.4.28", "0bf6546eb7cd6185ae086cbc5d20cd6dbb4b428aad14c02c49f7b554484b4586", [:mix], [], "hexpm", "501cef12286a3231dc80c81352a9453decf9586977f917a96e619293132743fb"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_cldr": {:hex, :ex_cldr, "2.33.0", "fd1f3752164ace38b2d7986fd948bf49d1037ee1ad7007a2349c5411e482f33f", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:cldr_utils, "~> 2.18", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}], "hexpm", "a46ced45b47fc4ebafbb7ffdaf496a3b34fe6bd78ce4cadfbc6920eb3d190ae7"},
"ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.14.1", "87102f426439264229854ded5b723a617bc194ca01dd53fa85afc28399faee1d", [:mix], [{:ex_cldr, "~> 2.27", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "da0a864ed43ade93c44d62170ea9c157c5abb20a01cc0270ac47cd51e045de00"},
"ex_cldr": {:hex, :ex_cldr, "2.33.2", "8adc4df3985e7f5d1d55cbbf72f993569de20eff5012ff3ea9412753961d4c00", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:cldr_utils, "~> 2.18", [hex: :cldr_utils, repo: "hexpm", optional: false]}, {:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:gettext, "~> 0.19", [hex: :gettext, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: true]}], "hexpm", "fd81a7147b4ed86c0c44c0251444cb8d1defccc7b33b89067ca1635f23e9fbf8"},
"ex_cldr_currencies": {:hex, :ex_cldr_currencies, "2.14.2", "354b48134faa011d58ae2e89be69b7de607d81fcc74c7ac684c5fb77b20186f5", [:mix], [{:ex_cldr, "~> 2.27", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "c970533103cdc97b1dedc2fead2209c0f5ae0aee0f1e504fdea5be5ee1466cd1"},
"ex_cldr_lists": {:hex, :ex_cldr_lists, "2.10.0", "4d4c9877da2d0417fd832907d69974e8328969f75fafc79b05ccf85f549f6281", [:mix], [{:ex_cldr_numbers, "~> 2.25", [hex: :ex_cldr_numbers, repo: "hexpm", optional: false]}, {:ex_doc, "~> 0.18", [hex: :ex_doc, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "adc040cde7b97f7fd7c0b35dd69ddb6fcf607303ae6355bb1851deae1f8b0652"},
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.27.2", "d1400a0502fb66ab3abcce3d10d5d11efcfc786eafe1c442ea2ddf834670d743", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:digital_token, "~> 0.3 or ~> 1.0", [hex: :digital_token, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.28", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.13", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "f682f46193e1793a3a98cc636314e9bea483da0925eb47935a39c4feef79e364"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"ex_cldr_numbers": {:hex, :ex_cldr_numbers, "2.27.3", "d6c1f1fb27ada01a77a7e0cf4ef0c59ad122a23829b6fd601820ac783d1bc9a5", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:digital_token, "~> 0.3 or ~> 1.0", [hex: :digital_token, repo: "hexpm", optional: false]}, {:ex_cldr, "~> 2.28", [hex: :ex_cldr, repo: "hexpm", optional: false]}, {:ex_cldr_currencies, "~> 2.14", [hex: :ex_cldr_currencies, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "2550dccc5232b90507e78a9fdee49d453249681e85d874f7d6703059be426724"},
"ex_doc": {:hex, :ex_doc, "0.28.5", "3e52a6d2130ce74d096859e477b97080c156d0926701c13870a4e1f752363279", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d2c4b07133113e9aa3e9ba27efb9088ba900e9e51caa383919676afdf09ab181"},
"jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
Expand Down

0 comments on commit 0c4260f

Please sign in to comment.