-module(nitro_conv). % N2Z Formatter: JSON, BERT % author Maxim Sokhatsky -include_lib("nitro/include/nitro.hrl"). -compile([export_all, nowarn_export_all]). % todo export % JOIN join([],_) -> []; join([Item], _Delim) -> [Item]; join([Item|Items], Delim) -> [Item, Delim | join(Items,Delim)]. f(S) -> f(S, []). f(S, Args) -> lists:flatten(io_lib:format(S, Args)). replace([], _, _) -> []; replace(String, S1, S2) when erlang:is_list(String), erlang:is_list(S1), erlang:is_list(S2) -> Length = erlang:length(S1), case string:substr(String, 1, Length) of S1 -> S2 ++ replace(string:substr(String, Length + 1), S1, S2); _ -> [erlang:hd(String)|replace(erlang:tl(String), S1, S2)] end. coalesce({_, Y}) -> Y; coalesce(false) -> undefined; coalesce([]) -> undefined; coalesce([H]) -> H; coalesce([undefined|T]) -> coalesce(T); coalesce([[]|T]) -> coalesce(T); coalesce([H|_]) -> H. indexof(Key, Fields) -> indexof(Key, Fields, 2). indexof(_Key, [], _N) -> undefined; indexof(Key, [Key|_T], N) -> N; indexof(Key, [_|T], N) -> indexof(Key, T, N + 1). config(App, Key, Default) -> application:get_env(App,Key,Default). os_env(Key) -> os_env(Key, ""). os_env(Key, Default) -> case os:getenv(Key) of false -> Default; V -> V end. % base64 encode/decode pickle(Data) -> base64:encode(erlang:term_to_binary({Data, os:timestamp()}, [compressed])). depickle(PickledData) -> try {Data, _PickleTime} = erlang:binary_to_term(base64:decode(nitro:to_binary(PickledData))), Data catch _:_ -> undefined end.