diff --git a/lib/utils/text-utils.js b/lib/utils/text-utils.js --- a/lib/utils/text-utils.js +++ b/lib/utils/text-utils.js @@ -4,7 +4,7 @@ nouns: $ReadOnlyArray, maxNumberOfNouns: number = 3, ): string { - if (nouns.length === 0) { + if (nouns.length === 0 || maxNumberOfNouns <= 0) { return ''; } else if (nouns.length === 1) { return nouns[0]; diff --git a/lib/utils/text-utils.test.js b/lib/utils/text-utils.test.js new file mode 100644 --- /dev/null +++ b/lib/utils/text-utils.test.js @@ -0,0 +1,24 @@ +// @flow + +import { pluralize } from './text-utils.js'; + +test('pluralize', () => { + expect(pluralize([])).toBe(''); + + expect(pluralize(['a'])).toBe('a'); + expect(pluralize(['a', 'b'])).toBe('a and b'); + expect(pluralize(['a', 'b', 'c'])).toBe('a, b, and c'); + expect(pluralize(['a', 'b', 'c', 'd'])).toBe('a, b, and 2 others'); + + expect(pluralize(['cat', 'dog', 'sheep'], 3)).toBe('cat, dog, and sheep'); + expect(pluralize(['cat', 'dog', 'sheep'], 2)).toBe('cat and 2 others'); + expect(pluralize(['cat', 'dog', 'sheep'], 1)).toBe('3 users'); + expect(pluralize(['cat', 'dog', 'sheep'], 0)).toBe(''); + + expect(pluralize(['cat', 'dog', 'sheep', 'moose'])).toBe( + 'cat, dog, and 2 others', + ); + expect(pluralize(['cat', 'dog', 'sheep', 'moose'], 5)).toBe( + 'cat, dog, sheep, and moose', + ); +});