`can_parse?` check function?
BryantLuu opened this issue · comments
I have an endpoint that receives strings and I was hoping there was a function in the Decimal
the library that would return a boolean if it could be parsed.
decimal?
is a type check.
D.decimal?("3.5") -> false
Does it make sense to have a can_parse?
function that would behave like:
D.can_parse?("3.5") -> true
D.can_parse?("Foo") -> false
There is a Decimal.parse/1
function that can be easily wrapped to return a boolean.
However, it’s common to use the parsed value if it was correct, so maybe using Decimal.parse/1
is a better option for your use case 🙂
I don't think it's a common enough use case and it's trivial to implement yourself:
def can_parse?(string) do
case Decimal.parse(string) do
{:ok, _} -> true
:error -> false
end
end
so I don't think it belongs to the package. Thanks!
@fertapric Thanks for the speedy reply!
@wojtekmach That's what I'm currently doing, but I thought it would be cleaner to have it in the package if it was a common enough use case. Thanks for considering :)