@@ -10,64 +10,64 @@ import { Transform } from 'stream';
1010 * @param {Boolean } opts.skipEmpty - if line is empty string, skip it (default: false)
1111 */
1212class ReadlineTransform extends Transform {
13- constructor ( options ) {
14- const opts = options || { } ;
15- opts . objectMode = true ;
16- super ( opts ) ;
17- this . _brRe = opts . breakMatcher || / \r ? \n / ;
18- this . _ignoreEndOfBreak = 'ignoreEndOfBreak' in opts ? Boolean ( opts . ignoreEndOfBreak ) : true ;
19- this . _skipEmpty = Boolean ( opts . skipEmpty ) ;
20- this . _buf = null ;
21- }
22-
23- _transform ( chunk , encoding , cb ) {
24- let str ;
25- if ( Buffer . isBuffer ( chunk ) || encoding === 'buffer' ) {
26- str = chunk . toString ( 'utf8' ) ;
27- } else {
28- str = chunk ;
13+ constructor ( options ) {
14+ const opts = options || { } ;
15+ opts . objectMode = true ;
16+ super ( opts ) ;
17+ this . _brRe = opts . breakMatcher || / \r ? \n / ;
18+ this . _ignoreEndOfBreak = 'ignoreEndOfBreak' in opts ? Boolean ( opts . ignoreEndOfBreak ) : true ;
19+ this . _skipEmpty = Boolean ( opts . skipEmpty ) ;
20+ this . _buf = null ;
2921 }
3022
31- try {
32- if ( this . _buf !== null ) {
33- this . _buf += str ;
34- } else {
35- this . _buf = str ;
36- }
23+ _transform ( chunk , encoding , cb ) {
24+ let str ;
25+ if ( Buffer . isBuffer ( chunk ) || encoding === 'buffer' ) {
26+ str = chunk . toString ( 'utf8' ) ;
27+ } else {
28+ str = chunk ;
29+ }
3730
38- const lines = this . _buf . split ( this . _brRe ) ;
39- const lastIndex = lines . length - 1 ;
40- for ( let i = 0 ; i < lastIndex ; i ++ ) {
41- this . _writeItem ( lines [ i ] ) ;
42- }
31+ try {
32+ if ( this . _buf !== null ) {
33+ this . _buf += str ;
34+ } else {
35+ this . _buf = str ;
36+ }
4337
44- const lastLine = lines [ lastIndex ] ;
45- if ( lastLine . length ) {
46- this . _buf = lastLine ;
47- } else if ( ! this . _ignoreEndOfBreak ) {
48- this . _buf = '' ;
49- } else {
50- this . _buf = null ;
51- }
52- cb ( ) ;
53- } catch ( err ) {
54- cb ( err ) ; // invalid data type;
38+ const lines = this . _buf . split ( this . _brRe ) ;
39+ const lastIndex = lines . length - 1 ;
40+ for ( let i = 0 ; i < lastIndex ; i ++ ) {
41+ this . _writeItem ( lines [ i ] ) ;
42+ }
43+
44+ const lastLine = lines [ lastIndex ] ;
45+ if ( lastLine . length ) {
46+ this . _buf = lastLine ;
47+ } else if ( ! this . _ignoreEndOfBreak ) {
48+ this . _buf = '' ;
49+ } else {
50+ this . _buf = null ;
51+ }
52+ return cb ( ) ;
53+ } catch ( err ) {
54+ return cb ( err ) ; // invalid data type;
55+ }
5556 }
56- }
5757
58- _flush ( cb ) {
59- if ( this . _buf !== null ) {
60- this . _writeItem ( this . _buf ) ;
61- this . _buf = null ;
58+ _flush ( cb ) {
59+ if ( this . _buf !== null ) {
60+ this . _writeItem ( this . _buf ) ;
61+ this . _buf = null ;
62+ }
63+ cb ( ) ;
6264 }
63- cb ( ) ;
64- }
6565
66- _writeItem ( line ) {
67- if ( line . length > 0 || ! this . _skipEmpty ) {
68- this . push ( line ) ;
66+ _writeItem ( line ) {
67+ if ( line . length > 0 || ! this . _skipEmpty ) {
68+ this . push ( line ) ;
69+ }
6970 }
70- }
7171}
7272
7373export default ReadlineTransform ;
0 commit comments