UNPKG

@roadiehq/catalog-backend-module-okta

Version:

A set of Backstage catalog providers for Okta

53 lines (50 loc) 1.42 kB
'use strict'; class GroupTree { groupDict = {}; rootNodes = []; constructor(groups) { for (const group of groups) { this.groupDict[group.metadata.name] = { group, children: [] }; } for (const group of groups) { const parentName = group.spec.parent === group.metadata.name ? void 0 : group.spec.parent; if (parentName) { this.groupDict[parentName].children.push( this.groupDict[group.metadata.name] ); } } for (const group of Object.values(this.groupDict)) { if (!this.hasParent(group)) { this.rootNodes.push(group); } } } getGroups(opts) { const { pruneEmptyMembers } = opts; const result = []; for (const group of Object.values(this.groupDict)) { if (pruneEmptyMembers) { if (this.hasMembers(group)) { result.push(group.group); } } else { result.push(group.group); } } return result; } hasMembers(node) { let hasMembers = node.group.spec.members?.length !== 0; for (const child of node.children) { const childHasMembers = this.hasMembers(child); hasMembers = hasMembers || childHasMembers; } return hasMembers; } hasParent(node) { return node.group.spec.parent === node.group.metadata.name ? false : !!node.group.spec.parent; } } exports.GroupTree = GroupTree; //# sourceMappingURL=GroupTree.cjs.js.map