Skip to content

Commit 6bb7c83

Browse files
Merge pull request #52 from jmespath-community/functions
Add function docs
2 parents 0a093c1 + c4e3ac5 commit 6bb7c83

26 files changed

+927
-0
lines changed

functions/abs.yml

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: abs
2+
topic:
3+
args:
4+
required:
5+
- name: value
6+
type: [number]
7+
desc:
8+
optional: []
9+
returns:
10+
type: number
11+
desc:
12+
desc: |
13+
Returns the absolute value of the provided argument. The signature indicates
14+
that a number is returned, and that the input argument `$value` **must**
15+
resolve to a number, otherwise a `invalid-type` error is triggered.
16+
17+
Below is a worked example. Given:
18+
19+
```
20+
{"foo": -1, "bar": "2"}
21+
```
22+
23+
Evaluating `abs(foo)` works as follows:
24+
25+
26+
1. Evaluate the input argument against the current data:
27+
28+
```
29+
search(foo, {"foo": -1, "bar": "2"}) -> -1
30+
```
31+
32+
33+
2. Validate the type of the resolved argument. In this case
34+
`-1` is of type `number` so it passes the type check.
35+
36+
37+
3. Call the function with the resolved argument:
38+
39+
```
40+
abs(-1) -> 1
41+
```
42+
43+
44+
4. The value of `1` is the resolved value of the function expression
45+
`abs(foo)`.
46+
47+
Below is the same steps for evaluating `abs(bar)`:
48+
49+
50+
1. Evaluate the input argument against the current data:
51+
52+
```
53+
search(bar, {"foo": -1, "bar": "2"}) -> "2"
54+
```
55+
56+
57+
2. Validate the type of the resolved argument. In this case
58+
`"2"` is of type `string` so we immediately indicate that
59+
an `invalid-type` error occurred.
60+
61+
As a final example, here is the steps for evaluating `abs(to_number(bar))`:
62+
63+
64+
1. Evaluate the input argument against the current data:
65+
66+
```
67+
search(to_number(bar), {"foo": -1, "bar": "2"})
68+
```
69+
70+
71+
2. In order to evaluate the above expression, we need to evaluate
72+
`to_number(bar)`:
73+
74+
```
75+
search(bar, {"foo": -1, "bar": "2"}) -> "2"
76+
# Validate "2" passes the type check for to_number, which it does.
77+
to_number("2") -> 2
78+
```
79+
80+
Note that to_number is defined below.
81+
82+
83+
3. Now we can evaluate the original expression:
84+
85+
```
86+
search(to_number(bar), {"foo": -1, "bar": "2"}) -> 2
87+
```
88+
89+
90+
4. Call the function with the final resolved value:
91+
92+
```
93+
abs(2) -> 2
94+
```
95+
96+
97+
5. The value of `2` is the resolved value of the function expression
98+
`abs(to_number(bar))`.
99+
examples:
100+
- context: None
101+
args: [1]
102+
returns: 1
103+
- context: None
104+
args: [-1]
105+
returns: 1
106+
- context: None
107+
args: ["abc"]
108+
returns: "<error: invalid-type>"

functions/avg.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: avg
2+
topic:
3+
args:
4+
required:
5+
- name: elements
6+
type: ['array[number]']
7+
desc:
8+
optional: []
9+
returns:
10+
type: number
11+
desc:
12+
desc: |
13+
Returns the average of the elements in the provided array.
14+
15+
An empty array will produce a return value of null.
16+
examples:
17+
- context: [10, 15, 20]
18+
args: [@]
19+
returns: 15
20+
- context: [10, false, 20]
21+
args: [@]
22+
returns: "<error: invalid-type>"
23+
- context: [false]
24+
args: [@]
25+
returns: "<error: invalid-type>"
26+
- context: false
27+
args: [@]
28+
returns: "<error: invalid-type>"

functions/ceil.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: ceil
2+
topic:
3+
args:
4+
required:
5+
- name: value
6+
type: [number]
7+
desc:
8+
optional: []
9+
returns:
10+
type: number
11+
desc:
12+
desc: |
13+
Returns the next highest integer value by rounding up if necessary.
14+
examples:
15+
- context: None
16+
args: [1.001]
17+
returns: 2
18+
- context: None
19+
args: [1.9]
20+
returns: 2
21+
- context: None
22+
args: [1]
23+
returns: 1
24+
- context: None
25+
args: [abc]
26+
returns: null

functions/contains.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: contains
2+
topic:
3+
args:
4+
required:
5+
- name: subject,
6+
type: [array, string]
7+
desc:
8+
- name: search
9+
type: [any]
10+
desc:
11+
optional: []
12+
returns:
13+
type: boolean
14+
desc:
15+
desc: |
16+
Returns `true` if the given `$subject` contains the provided `$search`
17+
string.
18+
19+
If `$subject` is an array, this function returns true if one of the elements
20+
in the array is equal to the provided `$search` value.
21+
22+
If the provided `$subject` is a string, this function returns true if
23+
the string contains the provided `$search` argument.
24+
examples:
25+
- context: None
26+
args: [foobar, foo]
27+
returns: true
28+
- context: None
29+
args: [foobar, not]
30+
returns: false
31+
- context: None
32+
args: [foobar, bar]
33+
returns: true
34+
- context: None
35+
args: [false, bar]
36+
returns: "<error: invalid-type>"
37+
- context: None
38+
args: [foobar, 123]
39+
returns: false
40+
- context: ["a", "b"]
41+
args: [@, a]
42+
returns: true
43+
- context: ["a"]
44+
args: [@, a]
45+
returns: true
46+
- context: ["a"]
47+
args: [@, b]
48+
returns: false
49+
- context: ["foo", "bar"]
50+
args: [@, foo]
51+
returns: true
52+
- context: ["foo", "bar"]
53+
args: [@, b]
54+
returns: false

functions/ends_with.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: ends_with
2+
topic:
3+
args:
4+
required:
5+
- name: subject,
6+
type: [string]
7+
desc:
8+
- name: prefix
9+
type: [string]
10+
desc:
11+
optional: []
12+
returns:
13+
type: boolean
14+
desc:
15+
desc: |
16+
Returns `true` if the `$subject` ends with the `$prefix`, otherwise this
17+
function returns `false`.
18+
examples:
19+
- context: foobarbaz
20+
args: [@, baz]
21+
returns: true
22+
- context: foobarbaz
23+
args: [@, foo]
24+
returns: false
25+
- context: foobarbaz
26+
args: [@, z]
27+
returns: true

functions/floor.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: floor
2+
topic:
3+
args:
4+
required:
5+
- name: value
6+
type: [number]
7+
desc:
8+
optional: []
9+
returns:
10+
type: number
11+
desc:
12+
desc: |
13+
Returns the next lowest integer value by rounding down if necessary.
14+
examples:
15+
- context: None
16+
args: [1.001]
17+
returns: 1
18+
- context: None
19+
args: [1.9]
20+
returns: 1
21+
- context: None
22+
args: [1]
23+
returns: 1

functions/join.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: join
2+
topic:
3+
args:
4+
required:
5+
- name: glue
6+
type: [string]
7+
desc:
8+
- name: stringsarray
9+
type: ['array[string]']
10+
desc:
11+
optional: []
12+
returns:
13+
type: string
14+
desc:
15+
desc: |
16+
Returns all of the elements from the provided `$stringsarray` array joined
17+
together using the `$glue` argument as a separator between each.
18+
examples:
19+
- context: ["a", "b"]
20+
args: [", ", @]
21+
returns: "a, b"
22+
- context: ["a", "b"]
23+
args: ["", @]
24+
returns: "ab"
25+
- context: ["a", false, "b"]
26+
args: [", ", @]
27+
returns: "<error: invalid-type>"
28+
- context: [false]
29+
args: [", ", @]
30+
returns: "<error: invalid-type>"

functions/keys.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: keys
2+
topic:
3+
args:
4+
required:
5+
- name: obj
6+
type: [object]
7+
desc:
8+
optional: []
9+
returns:
10+
type: array
11+
desc:
12+
desc: |
13+
Returns an array containing the keys of the provided object.
14+
Note that because JSON hashes are inheritently unordered, the
15+
keys associated with the provided object `obj` are inheritently
16+
unordered. Implementations are not required to return keys in
17+
any specific order.
18+
examples:
19+
- context: {"foo": "baz", "bar": "bam"}
20+
args: [@]
21+
returns: ["foo", "bar"]
22+
- context: {}
23+
args: [@]
24+
returns: []
25+
- context: false
26+
args: [@]
27+
returns: "<error: invalid-type>"
28+
- context: [b, a, c]
29+
args: [@]
30+
returns: "<error: invalid-type>"

functions/length.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: length
2+
topic:
3+
args:
4+
required:
5+
- name: subject
6+
type: [string, array, object]
7+
desc:
8+
optional: []
9+
returns:
10+
type: number
11+
desc:
12+
desc: |
13+
Returns the length of the given argument using the following types rules:
14+
15+
16+
1. string: returns the number of code points in the string
17+
18+
19+
2. array: returns the number of elements in the array
20+
21+
22+
3. object: returns the number of key-value pairs in the object
23+
examples:
24+
- context: None
25+
args: [abc]
26+
returns: 3
27+
- context: None
28+
args: [@]
29+
returns: 7
30+
- context: None
31+
args: [not_there]
32+
returns: "<error: invalid-type>"
33+
- context: ["a", "b", "c"]
34+
args: [@]
35+
returns: 3
36+
- context: []
37+
args: [@]
38+
returns: 0
39+
- context: {}
40+
args: [@]
41+
returns: 0
42+
- context: {"foo": "bar", "baz": "bam"}
43+
args: [@]
44+
returns: 2

0 commit comments

Comments
 (0)