Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed May 20, 2017
1 parent 37eb210 commit ef226b2
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/jesse_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@
%%% API
%% @doc Returns an empty list if the given value is ?not_found.
-spec empty_if_not_found(Value :: any()) -> any().
empty_if_not_found(?not_found) -> [];
empty_if_not_found(Value) -> Value.
empty_if_not_found(?not_found) ->
[];
empty_if_not_found(Value) ->
Value.

%% @doc Checks if the given value is json `array'.
%% This check is needed since objects in `jsx' are lists (proplists).
-spec is_array(Value :: any()) -> boolean().
is_array(Value) when is_list(Value) -> not is_json_object(Value);
is_array(_) -> false.
is_array(Value)
when is_list(Value) ->
not is_json_object(Value);
is_array(_) ->
false.

%% @doc A naive check if the given data is a json object.
%% Supports two main formats of json representation:
Expand All @@ -52,18 +57,32 @@ is_array(_) -> false.
%% 3) jsx format (`[{binary() | atom(), any()}]')
%% Returns `true' if the given data is an object, otherwise `false' is returned.
-spec is_json_object(Value :: any()) -> boolean().
is_json_object({struct, Value}) when is_list(Value) -> true;
is_json_object({Value}) when is_list(Value) -> true;
is_json_object({struct, Value})
when is_list(Value) ->
true;
is_json_object({Value})
when is_list(Value) ->
true;
%% handle `jsx' empty objects
is_json_object([{}]) -> true;
is_json_object([{}]) ->
true;
%% very naive check. checks only the first element.
is_json_object([{Key, _Value} | _])
when is_binary(Key) orelse is_atom(Key)
andalso Key =/= struct -> true;
?IF_MAPS(is_json_object(Map) when erlang:is_map(Map) -> true;)
is_json_object(_) -> false.
andalso Key =/= struct ->
true;
?IF_MAPS(
is_json_object(Map)
when erlang:is_map(Map) ->
true;
)
is_json_object(_) ->
false.

%% @doc Checks if the given value is json `null'.
-spec is_null(Value :: any()) -> boolean().
is_null(null) -> true;
is_null(_Value) -> false.
is_null(null) ->
true;
is_null(_Value) ->
false.

0 comments on commit ef226b2

Please sign in to comment.