Browse Source

nitro:to_integer

221V 3 years ago
parent
commit
270d1c8769
2 changed files with 9 additions and 2 deletions
  1. 8 1
      src/nitro.erl
  2. 1 1
      src/nitro_conv.erl

+ 8 - 1
src/nitro.erl

@@ -38,11 +38,11 @@ coalesce([[]|T]) -> coalesce(T);
 coalesce([H|_]) -> H.
 
 
+% to_list to_atom to_binary to_integer
 
 -define(IS_STRING(Term),
   (erlang:is_list(Term) andalso Term /= [] andalso erlang:is_integer(erlang:hd(Term)))).
 
-
 to_list(L) when ?IS_STRING(L) -> L;
 to_list(L) when erlang:is_list(L) ->
   SubLists = [inner_to_list(X) || X <- L],
@@ -69,6 +69,13 @@ to_binary(F) when erlang:is_float(F) -> erlang:float_to_binary(F, [{decimals, 9}
 to_binary(L) when erlang:is_list(L) ->  erlang:iolist_to_binary(L);
 to_binary(X) when erlang:is_tuple(X) ->  erlang:term_to_binary(X).
 
+to_integer(A) when erlang:is_atom(A) -> to_integer(erlang:atom_to_list(A));
+to_integer(B) when erlang:is_binary(B) -> to_integer(erlang:binary_to_list(B));
+to_integer(I) when erlang:is_integer(I) -> I;
+to_integer([]) -> 0;
+to_integer(L) when erlang:is_list(L) -> erlang:list_to_integer(L);
+to_integer(F) when erlang:is_float(F) -> erlang:round(F).
+
 
 -ifndef(PICKLER).
 -define(PICKLER, (application:get_env(n2z, pickler, nitro_conv))).

+ 1 - 1
src/nitro_conv.erl

@@ -7,7 +7,7 @@
 -compile([export_all, nowarn_export_all]). % todo export
 
 
-% WF to_atom to_list to_binary
+% to_atom to_list to_binary
 
 -define(IS_STRING(Term), (erlang:is_list(Term) andalso Term /= [] andalso erlang:is_integer(erlang:hd(Term)))).