Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3530139
D4329.id13741.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D4329.id13741.diff
View Options
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
@@ -40,7 +40,7 @@
return text;
}
}
- return pluralize(nouns, 1);
+ return trimText(pluralize(nouns, 1), maxLength);
}
export { pluralize, trimText, pluralizeAndTrim };
diff --git a/lib/utils/text-utils.test.js b/lib/utils/text-utils.test.js
--- a/lib/utils/text-utils.test.js
+++ b/lib/utils/text-utils.test.js
@@ -1,6 +1,6 @@
// @flow
-import { pluralize, trimText } from './text-utils.js';
+import { pluralize, trimText, pluralizeAndTrim } from './text-utils.js';
describe('pluralize(nouns, maxNumberOfNouns)', () => {
it('should return an empty string when no words are given', () => {
@@ -114,3 +114,48 @@
expect(trimText('abc', 3).length).toBeLessThanOrEqual(3);
});
});
+
+describe('pluralizeAndTrim(text, maxLength)', () => {
+ it('should return an empty string when nouns is an empty array', () => {
+ expect(pluralizeAndTrim([], 0)).toBe('');
+ expect(pluralizeAndTrim([], 10)).toBe('');
+ });
+
+ it('should return an emptyString when maxLength is 0', () => {
+ expect(pluralizeAndTrim(['a', 'b', 'c', 'd'], 0)).toBe('');
+ });
+
+ it('should return the first element of the array when the array has only one element', () => {
+ expect(pluralizeAndTrim(['a'], 10)).toBe('a');
+ expect(pluralizeAndTrim(['a'], 0)).toBe('');
+ });
+
+ it('should return "X and Y" when (nouns.length === 2) and ("X and Y".length <= maxLength)', () => {
+ expect(pluralizeAndTrim(['a', 'b'], 10)).toBe('a and b');
+ });
+
+ it('should return "2 users" when (nouns.length === 2) and ("X and Y".length > maxLength)', () => {
+ expect(pluralizeAndTrim(['spongebob', 'squarepants'], 10)).toBe('2 users');
+ });
+
+ it('should return "X, Y, and Z" when there are three nouns and all three will fit within maxLength', () => {
+ expect(pluralizeAndTrim(['cat', 'dog', 'sheep'], 20)).toBe(
+ 'cat, dog, and sheep',
+ );
+ });
+
+ it('should return "X and 2 others" when there are three nouns but only two will fit within maxLength', () => {
+ expect(pluralizeAndTrim(['cat', 'dog', 'sheep'], 16)).toBe(
+ 'cat and 2 others',
+ );
+ });
+
+ it('should return "3 users" when there are three nouns but only one will fit within maxLength', () => {
+ expect(pluralizeAndTrim(['cat', 'dog', 'sheep'], 10)).toBe('3 users');
+ });
+
+ it('should trim the pluralized string to fit within maxLength', () => {
+ expect(pluralizeAndTrim(['cat', 'dog', 'sheep'], 4)).toBe('3...');
+ expect(pluralizeAndTrim(['a', 'b', 'c', 'd'], 6)).toBe('4 u...');
+ });
+});
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Dec 26, 12:15 AM (11 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2703458
Default Alt Text
D4329.id13741.diff (2 KB)
Attached To
Mode
D4329: [lib] Write some tests for `text-utils: pluralizeAndTrim(...)`
Attached
Detach File
Event Timeline
Log In to Comment