Skip to content

Commit 4bbfb78

Browse files
committed
Improve types in sidebar/helpers/group-organizations.js
1 parent 13ee975 commit 4bbfb78

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/sidebar/helpers/group-organizations.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { immutable } from '../util/immutable';
22

33
/**
44
* @typedef {import('../../types/api').Group} Group
5+
* @typedef {import('../../types/api').Organization} Organization
6+
*/
7+
8+
/**
9+
* @typedef {Organization & { groups: Group[] }} OrganizationWithGroups
510
*/
611

712
// TODO: Update when this is a property available on the API response
@@ -11,7 +16,7 @@ const DEFAULT_ORG_ID = '__default__';
1116
* Generate consistent object keys for organizations so that they
1217
* may be sorted
1318
*
14-
* @param {object} organization
19+
* @param {Organization} organization
1520
* @return {String}
1621
*/
1722
function orgKey(organization) {
@@ -26,7 +31,7 @@ function orgKey(organization) {
2631
* groups Array.
2732
*
2833
* @param {Group} group
29-
* @param {object} organization
34+
* @param {OrganizationWithGroups} organization
3035
* @return undefined - organization is mutated in place
3136
*/
3237
function addGroup(group, organization) {
@@ -45,11 +50,12 @@ function addGroup(group, organization) {
4550
* Iterate over groups and locate unique organizations. Slot groups into
4651
* their appropriate "parent" organizations.
4752
*
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"
5156
*/
5257
function organizations(groups) {
58+
/** @type {Record<string, OrganizationWithGroups>} */
5359
const orgs = {};
5460
groups.forEach(group => {
5561
// Ignore groups with undefined or non-object organizations
@@ -59,8 +65,7 @@ function organizations(groups) {
5965
const orgId = orgKey(group.organization);
6066
if (typeof orgs[orgId] === 'undefined') {
6167
// 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: [] };
6469
}
6570
addGroup(group, orgs[orgId]); // Add the current group to its organization's groups
6671
});
@@ -80,12 +85,12 @@ function organizations(groups) {
8085
* in each organization will have a logo property (if available on the
8186
* organization).
8287
*
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
8590
*/
8691
export function groupsByOrganization(groups) {
8792
const orgs = organizations(groups);
88-
const defaultOrganizationGroups = [];
93+
const defaultOrganizationGroups = /** @type {Group[]} */ ([]);
8994
const sortedGroups = [];
9095

9196
const sortedOrgKeys = Object.keys(orgs).sort();

0 commit comments

Comments
 (0)