File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -115,10 +115,14 @@ const transform = function (original_options = {}) {
115115 this . state . bufBytesStart += bomLength ;
116116 buf = buf . slice ( bomLength ) ;
117117 // Renormalize original options with the new encoding
118- this . options = normalize_options ( {
118+ const options = normalize_options ( {
119119 ...this . original_options ,
120120 encoding : encoding ,
121121 } ) ;
122+ // Properties are merged with the existing options instance
123+ for ( const key in options ) {
124+ this . options [ key ] = options [ key ] ;
125+ }
122126 // Options will re-evaluate the Buffer with the new encoding
123127 ( { comment, escape, quote } = this . options ) ;
124128 break ;
Original file line number Diff line number Diff line change @@ -650,7 +650,7 @@ const normalize_options = function (opts) {
650650 // Normalize option `to`
651651 if ( options . to === undefined || options . to === null ) {
652652 options . to = - 1 ;
653- } else {
653+ } else if ( options . to !== - 1 ) {
654654 if ( typeof options . to === "string" && / \d + / . test ( options . to ) ) {
655655 options . to = parseInt ( options . to ) ;
656656 }
@@ -669,7 +669,7 @@ const normalize_options = function (opts) {
669669 // Normalize option `to_line`
670670 if ( options . to_line === undefined || options . to_line === null ) {
671671 options . to_line = - 1 ;
672- } else {
672+ } else if ( options . to_line !== - 1 ) {
673673 if ( typeof options . to_line === "string" && / \d + / . test ( options . to_line ) ) {
674674 options . to_line = parseInt ( options . to_line ) ;
675675 }
Original file line number Diff line number Diff line change @@ -127,4 +127,18 @@ describe("Option `bom`", function () {
127127 parser . write ( Buffer . from ( [ 239 , 187 ] ) ) ;
128128 parser . end ( ) ;
129129 } ) ;
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+ } ) ;
130144} ) ;
Original file line number Diff line number Diff line change @@ -8,9 +8,9 @@ describe("Option `to`", function () {
88 parse ( "" , { to : "10" } , ( ) => { } ) ;
99 parse ( "" , { to : null } , ( ) => { } ) ;
1010 ( ( ) => {
11- parse ( "" , { to : - 1 } , ( ) => { } ) ;
11+ parse ( "" , { to : - 2 } , ( ) => { } ) ;
1212 } ) . 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 " ,
1414 ) ;
1515 ( ( ) => {
1616 parse ( "" , { to : 0 } , ( ) => { } ) ;
Original file line number Diff line number Diff line change @@ -9,9 +9,9 @@ describe("Option `to_line`", function () {
99 parse ( "" , { to_line : null } , ( ) => { } ) ;
1010 parse ( "" , { to_line : undefined } , ( ) => { } ) ;
1111 ( ( ) => {
12- parse ( "" , { to_line : - 1 } , ( ) => { } ) ;
12+ parse ( "" , { to_line : - 2 } , ( ) => { } ) ;
1313 } ) . 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 " ,
1515 ) ;
1616 ( ( ) => {
1717 parse ( "" , { to_line : 0 } , ( ) => { } ) ;
You can’t perform that action at this time.
0 commit comments