Current section

Files

Jump to
jsonlogic src jsonlogic@internal@util.erl
Raw

src/jsonlogic@internal@util.erl

-module(jsonlogic@internal@util).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/jsonlogic/internal/util.gleam").
-export([comparison_reduce/2, chain_reduce/2, chain_reduce_result/2, modulo/2, divide/2, float_to_dynamic/1, flatten/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/jsonlogic/internal/util.gleam", 9).
?DOC(false).
-spec comparison_reduce(list(float()), fun((float(), float()) -> boolean())) -> {ok,
boolean()} |
{error, jsonlogic@error:evaluation_error()}.
comparison_reduce(Values, Operator) ->
case Values of
[First | Rest] ->
_pipe = gleam@list:fold_until(
Rest,
{First, true},
fun(Previous, Current) ->
case Operator(erlang:element(1, Previous), Current) of
true ->
{continue, {Current, true}};
false ->
{stop, {Current, false}}
end
end
),
(fun(Res) -> {ok, erlang:element(2, Res)} end)(_pipe);
_ ->
{error, invalid_arguments_error}
end.
-file("src/jsonlogic/internal/util.gleam", 27).
?DOC(false).
-spec chain_reduce(list(float()), fun((float(), float()) -> float())) -> {ok,
float()} |
{error, jsonlogic@error:evaluation_error()}.
chain_reduce(Values, Operator) ->
case Values of
[First | Rest] ->
{ok, gleam@list:fold(Rest, First, Operator)};
_ ->
{error, invalid_arguments_error}
end.
-file("src/jsonlogic/internal/util.gleam", 37).
?DOC(false).
-spec chain_reduce_result(
list(float()),
fun((float(), float()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()})
) -> {ok, float()} | {error, jsonlogic@error:evaluation_error()}.
chain_reduce_result(Values, Operator) ->
case Values of
[First | Rest] ->
gleam@list:fold_until(
Rest,
{ok, First},
fun(Previous, Current) ->
Previous@2 = case Previous of
{ok, Previous@1} -> Previous@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/util"/utf8>>,
function => <<"chain_reduce_result"/utf8>>,
line => 44,
value => _assert_fail,
start => 1158,
'end' => 1192,
pattern_start => 1169,
pattern_end => 1181})
end,
case Operator(Previous@2, Current) of
{ok, Result} ->
{continue, {ok, Result}};
{error, Error} ->
{stop, {error, Error}}
end
end
);
_ ->
{error, invalid_arguments_error}
end.
-file("src/jsonlogic/internal/util.gleam", 54).
?DOC(false).
-spec modulo(float(), float()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()}.
modulo(A, B) ->
_pipe = gleam@float:modulo(
gleam@float:absolute_value(A),
gleam@float:absolute_value(B)
),
_pipe@1 = gleam@result:map(_pipe, fun(Value) -> case {A < +0.0, B < +0.0} of
{true, false} ->
gleam@float:negate(Value);
{_, _} ->
Value
end end),
gleam@result:try_recover(_pipe@1, fun(_) -> {error, na_n_error} end).
-file("src/jsonlogic/internal/util.gleam", 65).
?DOC(false).
-spec divide(float(), float()) -> {ok, float()} |
{error, jsonlogic@error:evaluation_error()}.
divide(A, B) ->
_pipe = gleam@float:divide(A, B),
gleam@result:try_recover(_pipe, fun(_) -> {error, na_n_error} end).
-file("src/jsonlogic/internal/util.gleam", 80).
?DOC(false).
-spec is_zero(float()) -> boolean().
is_zero(Value) ->
case {Value =:= +0.0, Value =:= -0.0} of
{true, _} ->
true;
{_, true} ->
true;
{_, _} ->
false
end.
-file("src/jsonlogic/internal/util.gleam", 70).
?DOC(false).
-spec float_to_dynamic(float()) -> gleam@dynamic:dynamic_().
float_to_dynamic(Value) ->
Truncated = erlang:trunc(Value),
Is_zero = is_zero(Value),
case Value =:= erlang:float(Truncated) of
true ->
gleam_stdlib:identity(Truncated);
false when Is_zero ->
gleam_stdlib:identity(0);
false ->
gleam_stdlib:identity(Value)
end.
-file("src/jsonlogic/internal/util.gleam", 87).
?DOC(false).
-spec flatten(list(gleam@dynamic:dynamic_())) -> list(gleam@dynamic:dynamic_()).
flatten(Values) ->
gleam@list:flat_map(
Values,
fun(Value) ->
Decoded@1 = case gleam@dynamic@decode:run(
Value,
gleam@dynamic@decode:one_of(
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1}
),
[gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1},
fun gleam@list:wrap/1
)]
)
) of
{ok, Decoded} -> Decoded;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"jsonlogic/internal/util"/utf8>>,
function => <<"flatten"/utf8>>,
line => 89,
value => _assert_fail,
start => 2420,
'end' => 2591,
pattern_start => 2431,
pattern_end => 2442})
end,
Decoded@1
end
).