alisinabh

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

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

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?/1 which checks if all of a string is numerical chars.
  • remove_non_digits/2 which 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

Eiji

@alisinabh: Here are my ideas:

  1. Look at Naming Conventions: Trailing question mark (foo?)
  2. 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

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 :frowning:). 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

alisinabh

Thank you very much. :slight_smile:

This is so helpful. I will try that soon.

Where Next?

Popular in Libraries Top

tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
danschultzer
In short Plug n’ play OAuth 2.0 provider library. Just set up a resource owner schema with Ecto (your user schema), install the dependen...
New
asiniy
Hey there! I wrote a download elixir package which does exactly what its name about - an easy way to download files. I saw solutions ...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: https://github.com/tmbb/phoenix_ws Phoenix channels are a great...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
Azolo
Hey everyone, I just released WebSockex which is a Elixir WebSocket client. WebSockex strives to work as a OTP special process, be RFC6...
New

Other popular topics Top

William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

Sub Categories:

We're in Beta

About us Mission Statement