File tree Expand file tree Collapse file tree 5 files changed +25
-7
lines changed Expand file tree Collapse file tree 5 files changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -115,10 +115,14 @@ const transform = function (original_options = {}) {
115
115
this . state . bufBytesStart += bomLength ;
116
116
buf = buf . slice ( bomLength ) ;
117
117
// Renormalize original options with the new encoding
118
- this . options = normalize_options ( {
118
+ const options = normalize_options ( {
119
119
...this . original_options ,
120
120
encoding : encoding ,
121
121
} ) ;
122
+ // Properties are merged with the existing options instance
123
+ for ( const key in options ) {
124
+ this . options [ key ] = options [ key ] ;
125
+ }
122
126
// Options will re-evaluate the Buffer with the new encoding
123
127
( { comment, escape, quote } = this . options ) ;
124
128
break ;
Original file line number Diff line number Diff line change @@ -650,7 +650,7 @@ const normalize_options = function (opts) {
650
650
// Normalize option `to`
651
651
if ( options . to === undefined || options . to === null ) {
652
652
options . to = - 1 ;
653
- } else {
653
+ } else if ( options . to !== - 1 ) {
654
654
if ( typeof options . to === "string" && / \d + / . test ( options . to ) ) {
655
655
options . to = parseInt ( options . to ) ;
656
656
}
@@ -669,7 +669,7 @@ const normalize_options = function (opts) {
669
669
// Normalize option `to_line`
670
670
if ( options . to_line === undefined || options . to_line === null ) {
671
671
options . to_line = - 1 ;
672
- } else {
672
+ } else if ( options . to_line !== - 1 ) {
673
673
if ( typeof options . to_line === "string" && / \d + / . test ( options . to_line ) ) {
674
674
options . to_line = parseInt ( options . to_line ) ;
675
675
}
Original file line number Diff line number Diff line change @@ -127,4 +127,18 @@ describe("Option `bom`", function () {
127
127
parser . write ( Buffer . from ( [ 239 , 187 ] ) ) ;
128
128
parser . end ( ) ;
129
129
} ) ;
130
+
131
+ it ( "options instance is updated and not re-created (see #460)" , async function ( ) {
132
+ // https://github.com/adaltas/node-csv/issues/460
133
+ const parser = parse ( {
134
+ columns : true ,
135
+ bom : true ,
136
+ from_line : 2 ,
137
+ } ) ;
138
+ parser . write ( Buffer . from ( "\ufeffa,b\n" ) ) ;
139
+ parser . write ( Buffer . from ( "1,2\n" ) ) ;
140
+ parser . write ( Buffer . from ( "3,4\n" ) ) ;
141
+ parser . end ( ) ;
142
+ parser . options ?. columns . should . eql ( [ { name : "1" } , { name : "2" } ] ) ;
143
+ } ) ;
130
144
} ) ;
Original file line number Diff line number Diff line change @@ -8,9 +8,9 @@ describe("Option `to`", function () {
8
8
parse ( "" , { to : "10" } , ( ) => { } ) ;
9
9
parse ( "" , { to : null } , ( ) => { } ) ;
10
10
( ( ) => {
11
- parse ( "" , { to : - 1 } , ( ) => { } ) ;
11
+ parse ( "" , { to : - 2 } , ( ) => { } ) ;
12
12
} ) . should . throw (
13
- "Invalid Option: to must be a positive integer greater than 0, got -1 " ,
13
+ "Invalid Option: to must be a positive integer greater than 0, got -2 " ,
14
14
) ;
15
15
( ( ) => {
16
16
parse ( "" , { to : 0 } , ( ) => { } ) ;
Original file line number Diff line number Diff line change @@ -9,9 +9,9 @@ describe("Option `to_line`", function () {
9
9
parse ( "" , { to_line : null } , ( ) => { } ) ;
10
10
parse ( "" , { to_line : undefined } , ( ) => { } ) ;
11
11
( ( ) => {
12
- parse ( "" , { to_line : - 1 } , ( ) => { } ) ;
12
+ parse ( "" , { to_line : - 2 } , ( ) => { } ) ;
13
13
} ) . should . throw (
14
- "Invalid Option: to_line must be a positive integer greater than 0, got -1 " ,
14
+ "Invalid Option: to_line must be a positive integer greater than 0, got -2 " ,
15
15
) ;
16
16
( ( ) => {
17
17
parse ( "" , { to_line : 0 } , ( ) => { } ) ;
You can’t perform that action at this time.
0 commit comments