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 @@ -24,8 +24,10 @@ function trimText(text: string, maxLength: number): string { if (text.length <= maxLength) { return text; + } else if (maxLength <= 3) { + return text.substring(0, maxLength); } - return text.substr(0, maxLength - 3) + '...'; + return `${text.substring(0, maxLength - 3)}...`; } function 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 } from './text-utils.js'; +import { pluralize, trimText } from './text-utils.js'; describe('pluralize(nouns, maxNumberOfNouns)', () => { it('should return an empty string when no words are given', () => { @@ -49,3 +49,56 @@ ); }); }); + +test('trimText', () => { + expect(trimText('', 0)).toBe(''); + expect(trimText('', 1)).toBe(''); + expect(trimText('', 2)).toBe(''); + expect(trimText('', 3)).toBe(''); + expect(trimText('', 4)).toBe(''); + + expect(trimText('a', 0)).toBe(''); + expect(trimText('a', 0).length).toBeLessThanOrEqual(0); + expect(trimText('a', 1)).toBe('a'); + expect(trimText('a', 1).length).toBeLessThanOrEqual(1); + expect(trimText('a', 2)).toBe('a'); + expect(trimText('a', 2).length).toBeLessThanOrEqual(2); + expect(trimText('a', 3)).toBe('a'); + expect(trimText('a', 3).length).toBeLessThanOrEqual(3); + expect(trimText('a', 4)).toBe('a'); + expect(trimText('a', 4).length).toBeLessThanOrEqual(4); + + expect(trimText('ab', 0)).toBe(''); + expect(trimText('ab', 0).length).toBeLessThanOrEqual(0); + expect(trimText('ab', 1)).toBe('a'); + expect(trimText('ab', 1).length).toBeLessThanOrEqual(1); + expect(trimText('ab', 2)).toBe('ab'); + expect(trimText('ab', 2).length).toBeLessThanOrEqual(2); + expect(trimText('ab', 3)).toBe('ab'); + expect(trimText('ab', 3).length).toBeLessThanOrEqual(3); + expect(trimText('ab', 4)).toBe('ab'); + expect(trimText('ab', 4).length).toBeLessThanOrEqual(4); + + expect(trimText('abc', 0)).toBe(''); + expect(trimText('abc', 0).length).toBeLessThanOrEqual(0); + expect(trimText('abc', 1)).toBe('a'); + expect(trimText('abc', 1).length).toBeLessThanOrEqual(1); + expect(trimText('abc', 2)).toBe('ab'); + expect(trimText('abc', 2).length).toBeLessThanOrEqual(2); + expect(trimText('abc', 3)).toBe('abc'); + expect(trimText('abc', 3).length).toBeLessThanOrEqual(3); + expect(trimText('abc', 4)).toBe('abc'); + expect(trimText('abc', 4).length).toBeLessThanOrEqual(4); + + expect(trimText('the quick brown fox jumps', 0)).toBe(''); + expect(trimText('the quick brown fox jumps', 1)).toBe('t'); + expect(trimText('the quick brown fox jumps', 2)).toBe('th'); + expect(trimText('the quick brown fox jumps', 3)).toBe('the'); + expect(trimText('the quick brown fox jumps', 4)).toBe('t...'); + expect(trimText('the quick brown fox jumps', 25)).toBe( + 'the quick brown fox jumps', + ); + expect(trimText('the quick brown fox jumps', 400)).toBe( + 'the quick brown fox jumps', + ); +});