Skip to content

Commit

Permalink
Management sections: Allow easy sub-section lookup (#10651)
Browse files Browse the repository at this point in the history
* Allow sub-section lookup using `/` separator

* Adding assertion to check against the more explicit method

* Conforming to JS style guide

* Better wording
  • Loading branch information
ycombinator authored Mar 2, 2017
1 parent cbe26ea commit 7cbc22b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/ui/public/management/__tests__/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ describe('ManagementSection', () => {
it('returns undefined if un-registered', () => {
expect(section.getSection('unknown')).to.be(undefined);
});

it('returns sub-sections specified via a /-separated path', () => {
section.getSection('about').register('time');
expect(section.getSection('about/time')).to.be.a(ManagementSection);
expect(section.getSection('about/time')).to.be(section.getSection('about').getSection('time'));
});

it('returns undefined if a sub-section along a /-separated path does not exist', () => {
expect(section.getSection('about/damn/time')).to.be(undefined);
});
});

describe('items', () => {
Expand Down
13 changes: 12 additions & 1 deletion src/ui/public/management/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ export default class ManagementSection {
*/

getSection(id) {
return this.items.byId[id];
if (!id) {
return;
}

const sectionPath = id.split('/');
return sectionPath.reduce((currentSection, nextSection) => {
if (!currentSection) {
return;
}

return currentSection.items.byId[nextSection];
}, this);
}
}

0 comments on commit 7cbc22b

Please sign in to comment.