Skip to content

Commit

Permalink
Add Cldr.Unit.Range.to_string/3. Closes #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Mar 22, 2023
1 parent 6194bb4 commit 200f535
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 97 deletions.
2 changes: 1 addition & 1 deletion lib/cldr/protocol/enumerate.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defimpl Enumerable, for: Cldr.Unit.Range do

@impl Enumerable
def count(%{first: first, last: last}) do
count =
Expand All @@ -22,6 +21,7 @@ defimpl Enumerable, for: Cldr.Unit.Range do
case Cldr.Unit.convert(unit, range.first.unit) do
{:ok, converted} ->
{:ok, converted.value >= range.first.value and converted.value <= range.last.value}

_other ->
{:ok, false}
end
Expand Down
7 changes: 5 additions & 2 deletions lib/cldr/unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,11 @@ defmodule Cldr.Unit do

def maybe_new(a, b, options) do
new(a, Decimal.new(b), options)
rescue Decimal.Error ->
{:error, {Cldr.InvalidUnit, "Could not resolve a new unit from Cldr.Unit.new(#{inspect b}, #{inspect a})"}}
rescue
Decimal.Error ->
{:error,
{Cldr.InvalidUnit,
"Could not resolve a new unit from Cldr.Unit.new(#{inspect(b)}, #{inspect(a)})"}}
end

@doc """
Expand Down
8 changes: 4 additions & 4 deletions lib/cldr/unit/conversion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ defmodule Cldr.Unit.Conversion do
maybe_integer(any)
end

def sub(a, %Decimal{} = b) do
def sub(a, %Decimal{} = b) do
Decimal.sub(a, b)
|> maybe_integer()
end
Expand All @@ -402,7 +402,6 @@ defmodule Cldr.Unit.Conversion do
maybe_integer(b)
end


def mult(_any, 0) do
0
end
Expand Down Expand Up @@ -478,8 +477,9 @@ defmodule Cldr.Unit.Conversion do

def maybe_integer(%Decimal{} = a) do
Decimal.to_integer(a)
rescue FunctionClauseError ->
a
rescue
FunctionClauseError ->
a
end

def maybe_integer(a) when is_integer(a) do
Expand Down
9 changes: 3 additions & 6 deletions lib/cldr/unit/conversion/conversions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ defmodule Cldr.Unit.Conversions do
{unit, conversion}

{unit, %{factor: factor} = conversion} when is_binary(factor) ->
{unit,
%{conversion | factor: Decimal.new(factor)}}
{unit, %{conversion | factor: Decimal.new(factor)}}

{unit, %{factor: factor} = conversion} ->
{unit,
%{conversion | factor: Decimal.div(factor.numerator, factor.denominator)}}
{unit, %{conversion | factor: Decimal.div(factor.numerator, factor.denominator)}}
end)
|> Enum.map(fn
{unit, %{offset: offset} = conversion} when is_number(offset) ->
Expand All @@ -29,8 +27,7 @@ defmodule Cldr.Unit.Conversions do
{unit, %{conversion | offset: Decimal.new(offset)}}

{unit, %{offset: offset} = conversion} ->
{unit,
%{conversion | offset: Decimal.div(offset.numerator, offset.denominator)}}
{unit, %{conversion | offset: Decimal.div(offset.numerator, offset.denominator)}}
end)
|> Enum.map(fn
{unit, %{base_unit: base_unit} = conversion} ->
Expand Down
Loading

0 comments on commit 200f535

Please sign in to comment.