@@ -2,6 +2,11 @@ import { immutable } from '../util/immutable';
2
2
3
3
/**
4
4
* @typedef {import('../../types/api').Group } Group
5
+ * @typedef {import('../../types/api').Organization } Organization
6
+ */
7
+
8
+ /**
9
+ * @typedef {Organization & { groups: Group[] } } OrganizationWithGroups
5
10
*/
6
11
7
12
// TODO: Update when this is a property available on the API response
@@ -11,7 +16,7 @@ const DEFAULT_ORG_ID = '__default__';
11
16
* Generate consistent object keys for organizations so that they
12
17
* may be sorted
13
18
*
14
- * @param {object } organization
19
+ * @param {Organization } organization
15
20
* @return {String }
16
21
*/
17
22
function orgKey ( organization ) {
@@ -26,7 +31,7 @@ function orgKey(organization) {
26
31
* groups Array.
27
32
*
28
33
* @param {Group } group
29
- * @param {object } organization
34
+ * @param {OrganizationWithGroups } organization
30
35
* @return undefined - organization is mutated in place
31
36
*/
32
37
function addGroup ( group , organization ) {
@@ -45,11 +50,12 @@ function addGroup(group, organization) {
45
50
* Iterate over groups and locate unique organizations. Slot groups into
46
51
* their appropriate "parent" organizations.
47
52
*
48
- * @param {Array< Group> } groups
49
- * @return {object } - A collection of all unique organizations, containing
50
- * their groups. Keyed by each org's "orgKey"
53
+ * @param {Group[] } groups
54
+ * @return {Record<string, OrganizationWithGroups> } - A collection of all unique
55
+ * organizations, containing their groups. Keyed by each org's "orgKey"
51
56
*/
52
57
function organizations ( groups ) {
58
+ /** @type {Record<string, OrganizationWithGroups> } */
53
59
const orgs = { } ;
54
60
groups . forEach ( group => {
55
61
// Ignore groups with undefined or non-object organizations
@@ -59,8 +65,7 @@ function organizations(groups) {
59
65
const orgId = orgKey ( group . organization ) ;
60
66
if ( typeof orgs [ orgId ] === 'undefined' ) {
61
67
// First time we've seen this org
62
- orgs [ orgId ] = Object . assign ( { } , group . organization ) ;
63
- orgs [ orgId ] . groups = [ ] ; // Will hold this org's groups
68
+ orgs [ orgId ] = { ...group . organization , groups : [ ] } ;
64
69
}
65
70
addGroup ( group , orgs [ orgId ] ) ; // Add the current group to its organization's groups
66
71
} ) ;
@@ -80,12 +85,12 @@ function organizations(groups) {
80
85
* in each organization will have a logo property (if available on the
81
86
* organization).
82
87
*
83
- * @param {Array< Group> } groups
84
- * @return {Array<object> } - groups sorted by which organization they're in
88
+ * @param {Group[] } groups
89
+ * @return {Group[] } - groups sorted by which organization they're in
85
90
*/
86
91
export function groupsByOrganization ( groups ) {
87
92
const orgs = organizations ( groups ) ;
88
- const defaultOrganizationGroups = [ ] ;
93
+ const defaultOrganizationGroups = /** @type { Group[] } */ ( [ ] ) ;
89
94
const sortedGroups = [ ] ;
90
95
91
96
const sortedOrgKeys = Object . keys ( orgs ) . sort ( ) ;
0 commit comments