Skip to content

Commit

Permalink
Implement very basic pluralization for tab count message
Browse files Browse the repository at this point in the history
  • Loading branch information
robertknight committed Jan 22, 2024
1 parent 701d921 commit 2f6b57b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/sidebar/components/SelectionTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,23 @@ function SelectionTabs({

const showNotesUnavailableMessage = selectedTab === 'note' && noteCount === 0;

// Naive simple English pluralization
const pluralize = (count: number, singular: string, plural: string) => {
return count === 1 ? singular : plural;
};

const tabCountsSummaryPieces = [];
if (annotationCount > 0) {
tabCountsSummaryPieces.push(`${annotationCount} annotations`);
const term = pluralize(annotationCount, 'annotation', 'annotations');
tabCountsSummaryPieces.push(`${annotationCount} ${term}`);
}
if (noteCount > 0) {
tabCountsSummaryPieces.push(`${noteCount} notes`);
const term = pluralize(noteCount, 'note', 'notes');
tabCountsSummaryPieces.push(`${noteCount} ${term}`);
}
if (orphanCount > 0) {
tabCountsSummaryPieces.push(`${orphanCount} orphans`);
const term = pluralize(noteCount, 'orphan', 'orphans');
tabCountsSummaryPieces.push(`${orphanCount} ${term}`);
}
const tabCountsSummary = tabCountsSummaryPieces.join(', ');

Expand Down
8 changes: 8 additions & 0 deletions src/sidebar/components/test/SelectionTabs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ describe('SelectionTabs', () => {
},
message: '2 annotations, 3 notes, 4 orphans',
},
{
tabCounts: {
annotation: 1,
note: 1,
orphan: 1,
},
message: '1 annotation, 1 note, 1 orphan',
},
].forEach(({ tabCounts, message }) => {
it('reports annotation count to screen readers', () => {
const wrapper = createComponent({
Expand Down

0 comments on commit 2f6b57b

Please sign in to comment.