abs
Returns the absolute value of the provided argument.
abs(value: number) -> number Description
Returns the absolute value of the provided argument. The signature indicates
that a number is returned, and that the input argument $value must
resolve to a number, otherwise an invalid-type error is triggered.
Below is a worked example. Given:
{"foo": -1, "bar": "2"}
Evaluating abs(foo) works as follows:
- Evaluate the input argument against the current data:
search(foo, {"foo": -1, "bar": "2"}) → -1
Validate the type of the resolved argument. In this case
-1is of typenumberso it passes the type check.Call the function with the resolved argument:
abs(-1) → 1
- The value of
1is the resolved value of the function expressionabs(foo).
Below is the same steps for evaluating abs(bar):
- Evaluate the input argument against the current data:
search(bar, {"foo": -1, "bar": "2"}) → "2"
- Validate the type of the resolved argument. In this case
"2"is of typestringso we immediately indicate that aninvalid-typeerror occurred.
As a final example, here is the steps for evaluating abs(to_number(bar)):
- Evaluate the input argument against the current data:
search(to_number(bar), {"foo": -1, "bar": "2"}) → "…"
- In order to evaluate the above expression, we need to evaluate
to_number(bar):
search(bar, {"foo": -1, "bar": "2"}) → "2"
# Validate "2" passes the type check for to_number, which it does.
to_number("2") → 2
See to_number for its definition.
- Now we can evaluate the original expression:
search(to_number(bar), {"foo": -1, "bar": "2"}) → 2
- Call the function with the final resolved value:
abs(2) → 2
- The value of
2is the resolved value of the function expressionabs(to_number(bar)).
Arguments
Required
| Name | Type | Description |
|---|---|---|
value |
number |
positive or negative number |
Returns
Type: number
positive magnitude of input value
Examples
search(abs(1), null) → 1
search(abs(-1), null) → 1
search(abs(abc), null) → ERROR: invalid-type
Given the following JSON input:
{
"foo": -1,
"zero": 0,
"numbers": [
-1,
3,
4,
5
],
"array": [
-1,
3,
4,
5,
"a",
"100"
],
"strings": [
"a",
"b",
"c"
],
"decimals": [
1.01,
1.2,
-1.5
],
"str": "Str",
"false": false,
"empty_list": [],
"empty_hash": {},
"objects": {
"foo": "bar",
"bar": "baz"
},
"null_key": null
}
| Expression | Result |
|---|---|
abs(foo) |
1 |
abs(str) |
ERROR: invalid-type |
abs(array[1]) |
3 |
abs(false) |
ERROR: invalid-type |
abs(-24) |
24 |
abs(1, 2) |
ERROR: invalid-arity |
abs() |
ERROR: invalid-arity |