File tree Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 12
12
- [ core] ref: Move SDKInformation integration into core prepareEvent method
13
13
- [ core] ref: Move debug initialization as the first step
14
14
- [ node] fix: Make handlers types compatibile with Express
15
+ - [ utils] fix: Dont break when non-string is passed to truncate
15
16
16
17
## 4.0.4
17
18
Original file line number Diff line number Diff line change
1
+ import { isString } from './is' ;
2
+
1
3
/**
2
4
* Encodes given object into url-friendly format
3
5
*
6
8
* @returns string Encoded
7
9
*/
8
10
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 ) ) {
11
13
return str ;
12
14
}
13
15
return str . length <= max ? str : `${ str . substr ( 0 , max ) } \u2026` ;
Original file line number Diff line number Diff line change @@ -7,4 +7,19 @@ describe('truncate()', () => {
7
7
expect ( truncate ( 'lol' , 3 ) ) . toEqual ( 'lol' ) ;
8
8
expect ( truncate ( new Array ( 1000 ) . join ( 'f' ) , 0 ) ) . toEqual ( new Array ( 1000 ) . join ( 'f' ) ) ;
9
9
} ) ;
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
+ } ) ;
10
25
} ) ;
You can’t perform that action at this time.
0 commit comments