2024-05-25 20:03:45 +02:00
|
|
|
defmodule PhilomenaQuery.Parse.Helpers do
|
|
|
|
@moduledoc false
|
|
|
|
|
2019-11-02 19:34:25 +01:00
|
|
|
# Apparently, it's too hard for the standard library to to parse a number
|
|
|
|
# as a float if it doesn't contain a decimal point. WTF
|
|
|
|
def to_number(term) do
|
|
|
|
{float_val, _} = :string.to_float(term)
|
|
|
|
{int_val, _} = :string.to_integer(term)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
is_float(float_val) ->
|
|
|
|
float_val
|
|
|
|
|
|
|
|
is_integer(int_val) ->
|
|
|
|
int_val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_int(term) do
|
|
|
|
{int, _} = :string.to_integer(term)
|
|
|
|
|
2021-03-17 23:06:04 +01:00
|
|
|
case int in -2_147_483_648..2_147_483_647 do
|
|
|
|
true -> int
|
|
|
|
_false -> 0
|
|
|
|
end
|
2019-11-02 19:34:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def range([center, deviation]) do
|
|
|
|
[center - deviation, center + deviation]
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|