@@ -5629,9 +5629,20 @@ class Parser extends Transform {
5629
5629
throw new Error ( `Invalid Option: objname must be a non empty string` ) ;
5630
5630
}
5631
5631
// Great, nothing to do
5632
- } else {
5632
+ } else if ( typeof options . objname === 'number' ) ; else {
5633
5633
throw new Error ( `Invalid Option: objname must be a string or a buffer, got ${ options . objname } ` ) ;
5634
5634
}
5635
+ if ( options . objname !== undefined ) {
5636
+ if ( typeof options . objname === 'number' ) {
5637
+ if ( options . columns !== false ) {
5638
+ throw Error ( 'Invalid Option: objname index cannot be combined with columns or be defined as a field' ) ;
5639
+ }
5640
+ } else { // A string or a buffer
5641
+ if ( options . columns === false ) {
5642
+ throw Error ( 'Invalid Option: objname field must be combined with columns or be defined as an index' ) ;
5643
+ }
5644
+ }
5645
+ }
5635
5646
// Normalize option `on_record`
5636
5647
if ( options . on_record === undefined || options . on_record === null ) {
5637
5648
options . on_record = undefined ;
@@ -5790,7 +5801,6 @@ class Parser extends Transform {
5790
5801
error : undefined ,
5791
5802
enabled : options . from_line === 1 ,
5792
5803
escaping : false ,
5793
- // escapeIsQuote: options.escape === options.quote,
5794
5804
escapeIsQuote : isBuffer$1 ( options . escape ) && isBuffer$1 ( options . quote ) && Buffer . compare ( options . escape , options . quote ) === 0 ,
5795
5805
// columns can be `false`, `true`, `Array`
5796
5806
expectedRecordLength : Array . isArray ( options . columns ) ? options . columns . length : undefined ,
@@ -6169,6 +6179,7 @@ class Parser extends Transform {
6169
6179
}
6170
6180
this . info . records ++ ;
6171
6181
if ( from === 1 || this . info . records >= from ) {
6182
+ const { objname} = this . options ;
6172
6183
// With columns, records are object
6173
6184
if ( columns !== false ) {
6174
6185
const obj = { } ;
@@ -6186,55 +6197,45 @@ class Parser extends Transform {
6186
6197
obj [ columns [ i ] . name ] = record [ i ] ;
6187
6198
}
6188
6199
}
6189
- const { objname} = this . options ;
6190
6200
// Without objname (default)
6191
- if ( objname === undefined ) {
6192
- if ( raw === true || info === true ) {
6193
- const err = this . __push ( Object . assign (
6194
- { record : obj } ,
6195
- ( raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ) ,
6196
- ( info === true ? { info : this . __infoRecord ( ) } : { } )
6197
- ) ) ;
6198
- if ( err ) {
6199
- return err ;
6200
- }
6201
- } else {
6202
- const err = this . __push ( obj ) ;
6203
- if ( err ) {
6204
- return err ;
6205
- }
6201
+ if ( raw === true || info === true ) {
6202
+ const extRecord = Object . assign (
6203
+ { record : obj } ,
6204
+ ( raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ) ,
6205
+ ( info === true ? { info : this . __infoRecord ( ) } : { } )
6206
+ ) ;
6207
+ const err = this . __push (
6208
+ objname === undefined ? extRecord : [ obj [ objname ] , extRecord ]
6209
+ ) ;
6210
+ if ( err ) {
6211
+ return err ;
6206
6212
}
6207
- // With objname (default)
6208
6213
} else {
6209
- if ( raw === true || info === true ) {
6210
- const err = this . __push ( Object . assign (
6211
- { record : [ obj [ objname ] , obj ] } ,
6212
- raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ,
6213
- info === true ? { info : this . __infoRecord ( ) } : { }
6214
- ) ) ;
6215
- if ( err ) {
6216
- return err ;
6217
- }
6218
- } else {
6219
- const err = this . __push ( [ obj [ objname ] , obj ] ) ;
6220
- if ( err ) {
6221
- return err ;
6222
- }
6214
+ const err = this . __push (
6215
+ objname === undefined ? obj : [ obj [ objname ] , obj ]
6216
+ ) ;
6217
+ if ( err ) {
6218
+ return err ;
6223
6219
}
6224
6220
}
6225
6221
// Without columns, records are array
6226
6222
} else {
6227
6223
if ( raw === true || info === true ) {
6228
- const err = this . __push ( Object . assign (
6224
+ const extRecord = Object . assign (
6229
6225
{ record : record } ,
6230
6226
raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ,
6231
6227
info === true ? { info : this . __infoRecord ( ) } : { }
6232
- ) ) ;
6228
+ ) ;
6229
+ const err = this . __push (
6230
+ objname === undefined ? extRecord : [ record [ objname ] , extRecord ]
6231
+ ) ;
6233
6232
if ( err ) {
6234
6233
return err ;
6235
6234
}
6236
6235
} else {
6237
- const err = this . __push ( record ) ;
6236
+ const err = this . __push (
6237
+ objname === undefined ? record : [ record [ objname ] , record ]
6238
+ ) ;
6238
6239
if ( err ) {
6239
6240
return err ;
6240
6241
}
0 commit comments