Skip to content

Commit 2e5cfd5

Browse files
committed
fix: Dont break when non-string is passed to truncate
1 parent 4300607 commit 2e5cfd5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [core] ref: Move SDKInformation integration into core prepareEvent method
1313
- [core] ref: Move debug initialization as the first step
1414
- [node] fix: Make handlers types compatibile with Express
15+
- [utils] fix: Dont break when non-string is passed to truncate
1516

1617
## 4.0.4
1718

packages/utils/src/string.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isString } from './is';
2+
13
/**
24
* Encodes given object into url-friendly format
35
*
@@ -6,8 +8,8 @@
68
* @returns string Encoded
79
*/
810

9-
export function truncate(str: string, max: number): string {
10-
if (max === 0) {
11+
export function truncate(str: string, max: number = 0): string {
12+
if (max === 0 || !isString(str)) {
1113
return str;
1214
}
1315
return str.length <= max ? str : `${str.substr(0, max)}\u2026`;

packages/utils/test/string.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,19 @@ describe('truncate()', () => {
77
expect(truncate('lol', 3)).toEqual('lol');
88
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));
99
});
10+
11+
test('it instantly returns input when non-string is passed', () => {
12+
// @ts-ignore
13+
expect(truncate(2)).toEqual(2);
14+
// @ts-ignore
15+
expect(truncate(undefined, 123)).toEqual(undefined);
16+
// @ts-ignore
17+
expect(truncate(null)).toEqual(null);
18+
const obj = {};
19+
// @ts-ignore
20+
expect(truncate(obj, '42')).toEqual(obj);
21+
const arr: any[] = [];
22+
// @ts-ignore
23+
expect(truncate(arr)).toEqual(arr);
24+
});
1025
});

0 commit comments

Comments
 (0)