alisinabh
Numero - a micro library for converting non-english digits in Elixir
Hi everyone.
A few days ago i was developing a JSON API on phoenix which receives users phone number from client and starts sending messages to that phone number. There was a little problem in production however.
The problem was some people used Arabic or Persian keyboards to enter their phone number. And the phone number was not passed correctly into sending message API. So i’ve developed a micro library called Numero to tackle this problem in elixir.
Here is Numero: https://hex.pm/packages/numero
Currently it supports Persian, Arabic and NKO digits.
Most Liked
NobbZ
I’d not use that guarded version at all… Unicode has a lot of possible digits:
http://www.fileformat.info/info/unicode/category/Nd/list.htm
One doesn’t really have to iterate that list over and over again when using when char in @digits. Instead it should roughly like this:
@digits ~c[1234567890…] # include them all!
Enum.each(@digits, fn digit ->
defp do_digit_only?(<<unquote(digit)::utf8, rest::binary>>), do: do_digit_only?(rest)
end
defp dp_digit_only?(<<_::utf8, _::binary>>), do: false
defp do_digit_only?(""), do: true
alisinabh
Numero 0.2.0 is released.
Hi everyone. Numero 0.2.0 is released.
In this version i have added two new functions.
-
is_digit_only?/1which checks if all of a string is numerical chars. -
remove_non_digits/2which removes all non numerical chars form a given string with ability to have exceptions for some chars (optional).
I hope it will be useful for you guys.
Thanks.
Eiji
@alisinabh: Here are my ideas:
- Look at Naming Conventions: Trailing question mark (foo?)
- I think that some things could be written simpler
For example:
defmodule Example do
@digits String.graphemes("0123456789")
def digit_only?(""), do: false
def digit_only?(string), do: do_digit_only?(string)
defp do_digit_only?(""), do: true
defp do_digit_only?(<<char::binary-size(1), rest::binary>>) when char in @digits, do: do_digit_only?(rest)
defp do_digit_only?(_), do: false
end
alisinabh
Thank you @Eiji for reminding me my problem with function naming. I should remove is_ from next minor version.
This example looks nice and clean but the reason i’ve done it with char list was so that i could determine utf-8 digits too (but now that is see my code i just forgotten to support utf-8 numbers
). like “۱” which is 1 in Farsi.
Dealing with binary operations is a UTF-8 string is a bit hard. and maybe ugly.
For example if i wanted to do as you did on numbers i should have done this:
defmodule Example do
@digits String.graphemes("0123456789")
def digit_only?(""), do: false
def digit_only?(string), do: do_digit_only?(string)
defp do_digit_only?(""), do: true
defp do_digit_only?(<<char::binary-size(1), rest::binary>>) when char in @digits, do: do_digit_only?(rest)
defp do_digit_only?(<<char::binary-size(2), rest::binary>>) when char in @digits, do: do_digit_only?(rest)
defp do_digit_only?(_), do: false
end
Which i think may lead to some unwanted exceptions.
Any ideas on how to deal with them?
alisinabh
Thank you very much. 
This is so helpful. I will try that soon.







