File tree 5 files changed +20
-4
lines changed
5 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -17,9 +17,10 @@ declare const detectNewline: {
17
17
/**
18
18
Detect the dominant newline character of a string.
19
19
20
- @returns Returns detected newline or `\n` when no newline character is found.
20
+ @returns The detected newline or `\n` when no newline character is found or the input is not a string .
21
21
*/
22
22
graceful ( string : string ) : '\r\n' | '\n' ;
23
+ graceful ( string ?: unknown ) : '\n' ;
23
24
} ;
24
25
25
26
export = detectNewline ;
Original file line number Diff line number Diff line change @@ -18,4 +18,4 @@ const detectNewline = string => {
18
18
} ;
19
19
20
20
module . exports = detectNewline ;
21
- module . exports . graceful = string => detectNewline ( string ) || '\n' ;
21
+ module . exports . graceful = string => ( typeof string === 'string' && detectNewline ( string ) ) || '\n' ;
Original file line number Diff line number Diff line change @@ -3,3 +3,8 @@ import detectNewline = require('.');
3
3
4
4
expectType < '\r\n' | '\n' | undefined > ( detectNewline ( 'foo\nbar\nbaz\r\n' ) ) ;
5
5
expectType < '\r\n' | '\n' > ( detectNewline . graceful ( 'foo\nbar\nbaz\r\n' ) ) ;
6
+ expectType < '\n' > ( detectNewline . graceful ( null ) ) ;
7
+ expectType < '\n' > ( detectNewline . graceful ( undefined ) ) ;
8
+ expectType < '\n' > ( detectNewline . graceful ( ) ) ;
9
+ expectType < '\n' > ( detectNewline . graceful ( 1 ) ) ;
10
+ expectType < '\n' > ( detectNewline . graceful ( [ 'foo' , 'bar' ] ) ) ;
Original file line number Diff line number Diff line change @@ -26,9 +26,9 @@ detectNewline('foo\nbar\nbaz\r\n');
26
26
27
27
Returns the detected newline or ` undefined ` when no newline character is found.
28
28
29
- ### detectNewline.graceful(string )
29
+ ### detectNewline.graceful(unknown )
30
30
31
- Returns the detected newline or ` \n ` when no newline character is found.
31
+ Returns the detected newline or ` \n ` when no newline character is found or the input is not a string .
32
32
33
33
34
34
## Related
Original file line number Diff line number Diff line change @@ -7,5 +7,15 @@ test('main', t => {
7
7
t . is ( detectNewline ( 'foo\nbar\nbaz\r\n' ) , '\n' ) ;
8
8
t . is ( detectNewline ( 'foo\nbar\r\n' ) , '\n' ) ;
9
9
t . is ( detectNewline ( 'foo' ) , undefined ) ;
10
+ } ) ;
11
+
12
+ test ( 'graceful' , t => {
10
13
t . is ( detectNewline . graceful ( 'foo' ) , '\n' ) ;
14
+ t . is ( detectNewline . graceful ( 'foo\r\nbar\r\nbaz\n' ) , '\r\n' ) ;
15
+ t . is ( detectNewline . graceful ( null ) , '\n' ) ;
16
+ t . is ( detectNewline . graceful ( undefined ) , '\n' ) ;
17
+ t . is ( detectNewline . graceful ( ) , '\n' ) ;
18
+ t . is ( detectNewline . graceful ( 0 ) , '\n' ) ;
19
+ t . is ( detectNewline . graceful ( true ) , '\n' ) ;
20
+ t . is ( detectNewline . graceful ( [ 'foo' , 'bar' ] ) , '\n' ) ;
11
21
} ) ;
You can’t perform that action at this time.
0 commit comments