Skip to content

Commit 8ea17ee

Browse files
authored
fix: Guard truncate from unexpected input (#2079)
1 parent 45cc6ec commit 8ea17ee

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

packages/utils/src/string.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { isRegExp } from './is';
88
* @returns string Encoded
99
*/
1010
export function truncate(str: string, max: number = 0): string {
11-
if (max === 0) {
11+
// tslint:disable-next-line:strict-type-predicates
12+
if (typeof str !== 'string' || max === 0) {
1213
return str;
1314
}
1415
return str.length <= max ? str : `${str.substr(0, max)}...`;

packages/utils/test/string.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { keysToEventMessage, truncate } from '../src/string';
22

33
describe('truncate()', () => {
44
test('it works as expected', () => {
5+
expect(truncate(null, 3)).toEqual(null);
56
expect(truncate('lolol', 3)).toEqual('lol...');
67
expect(truncate('lolol', 10)).toEqual('lolol');
78
expect(truncate('1'.repeat(1000), 300)).toHaveLength(303);

0 commit comments

Comments
 (0)