Skip to content

Commit 6351eb2

Browse files
committed
Documented the group_by() function.
1 parent 3f3222a commit 6351eb2

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

functions/group_by.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: group_by
2+
topic: collections
3+
args:
4+
required:
5+
- name: elements
6+
type: ['array[object]']
7+
desc: ''
8+
- name: expr
9+
type: [expression->string]
10+
desc: ''
11+
optional: []
12+
returns:
13+
type: object
14+
desc: ''
15+
desc: |
16+
Groups an array of objects `$elements` using an expression `$expr` as the group key.
17+
The `$expr` expression is applied to each element in the array `$elements` and the
18+
resulting value is used as a group key.
19+
20+
The result is an object whose keys are the unique set of string keys and whose respective values are an array of objects array matching the group criteria.
21+
22+
Objects that do not match the group criteria are discarded from the output.
23+
This includes objects for which applying the `$expr` expression evaluates to `null`.
24+
25+
If the result of applying the `$expr` expression against the current array element
26+
results in type other than `string` or `null`, a type error MUST be raised.
27+
examples:
28+
group_by:
29+
context:
30+
items:
31+
- spec:
32+
nodeName: node_01
33+
other: values_01
34+
- spec:
35+
nodeName: node_02
36+
other: values_02
37+
- spec:
38+
nodeName: node_03
39+
other: values_03
40+
- spec:
41+
nodeName: node_01
42+
other: values_04
43+
args: [items, '&nodeName']
44+
returns:
45+
nodeName:
46+
node_01:
47+
- spec:
48+
nodeName: node_01
49+
other: values_01
50+
- spec:
51+
nodeName: node_01
52+
other: values_04
53+
node_02:
54+
- spec:
55+
nodeName: node_02
56+
other: values_02
57+
node_03:
58+
- spec:
59+
nodeName: node_03
60+
other: values_03
61+
group_by_discards_null_keyed_objects:
62+
context: &data
63+
array:
64+
- b: true
65+
name: one
66+
- b: false
67+
name: two
68+
- b: false
69+
args: [array, '&name']
70+
returns: [{age: 10, age_str: '10', bool: true, name: 3}, {age: 20, age_str: '20',
71+
bool: true, name: a, extra: foo}, {age: 30, age_str: '30', bool: true, name: c},
72+
{age: 40, age_str: '40', bool: false, name: b, extra: bar}, {age: 50, age_str: '50',
73+
bool: false, name: d}]
74+
group_by_errs_invalid_type:
75+
context: *data
76+
args: [array, '&b']
77+
error: invalid-type

jep-018-grouping.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This JEP introduces a new `group_by()` function.
1717
### group_by
1818

1919
```
20-
object group_by(array[object] $elements, expression->expression->string) $expr)
20+
object group_by(array[object] $elements, expression->string) $expr)
2121
```
2222

2323
Groups an array of objects `$elements` using an expression `$expr` as the group key.

0 commit comments

Comments
 (0)