@@ -5342,9 +5342,20 @@ class Parser extends Transform {
53425342 throw new Error ( `Invalid Option: objname must be a non empty string` ) ;
53435343 }
53445344 // Great, nothing to do
5345- } else {
5345+ } else if ( typeof options . objname === 'number' ) ; else {
53465346 throw new Error ( `Invalid Option: objname must be a string or a buffer, got ${ options . objname } ` ) ;
53475347 }
5348+ if ( options . objname !== undefined ) {
5349+ if ( typeof options . objname === 'number' ) {
5350+ if ( options . columns !== false ) {
5351+ throw Error ( 'Invalid Option: objname index cannot be combined with columns or be defined as a field' ) ;
5352+ }
5353+ } else { // A string or a buffer
5354+ if ( options . columns === false ) {
5355+ throw Error ( 'Invalid Option: objname field must be combined with columns or be defined as an index' ) ;
5356+ }
5357+ }
5358+ }
53485359 // Normalize option `on_record`
53495360 if ( options . on_record === undefined || options . on_record === null ) {
53505361 options . on_record = undefined ;
@@ -5882,6 +5893,7 @@ class Parser extends Transform {
58825893 }
58835894 this . info . records ++ ;
58845895 if ( from === 1 || this . info . records >= from ) {
5896+ const { objname} = this . options ;
58855897 // With columns, records are object
58865898 if ( columns !== false ) {
58875899 const obj = { } ;
@@ -5899,55 +5911,45 @@ class Parser extends Transform {
58995911 obj [ columns [ i ] . name ] = record [ i ] ;
59005912 }
59015913 }
5902- const { objname} = this . options ;
59035914 // Without objname (default)
5904- if ( objname === undefined ) {
5905- if ( raw === true || info === true ) {
5906- const err = this . __push ( Object . assign (
5907- { record : obj } ,
5908- ( raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ) ,
5909- ( info === true ? { info : this . __infoRecord ( ) } : { } )
5910- ) ) ;
5911- if ( err ) {
5912- return err ;
5913- }
5914- } else {
5915- const err = this . __push ( obj ) ;
5916- if ( err ) {
5917- return err ;
5918- }
5915+ if ( raw === true || info === true ) {
5916+ const extRecord = Object . assign (
5917+ { record : obj } ,
5918+ ( raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ) ,
5919+ ( info === true ? { info : this . __infoRecord ( ) } : { } )
5920+ ) ;
5921+ const err = this . __push (
5922+ objname === undefined ? extRecord : [ obj [ objname ] , extRecord ]
5923+ ) ;
5924+ if ( err ) {
5925+ return err ;
59195926 }
5920- // With objname (default)
59215927 } else {
5922- if ( raw === true || info === true ) {
5923- const err = this . __push ( Object . assign (
5924- { record : [ obj [ objname ] , obj ] } ,
5925- raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ,
5926- info === true ? { info : this . __infoRecord ( ) } : { }
5927- ) ) ;
5928- if ( err ) {
5929- return err ;
5930- }
5931- } else {
5932- const err = this . __push ( [ obj [ objname ] , obj ] ) ;
5933- if ( err ) {
5934- return err ;
5935- }
5928+ const err = this . __push (
5929+ objname === undefined ? obj : [ obj [ objname ] , obj ]
5930+ ) ;
5931+ if ( err ) {
5932+ return err ;
59365933 }
59375934 }
59385935 // Without columns, records are array
59395936 } else {
59405937 if ( raw === true || info === true ) {
5941- const err = this . __push ( Object . assign (
5938+ const extRecord = Object . assign (
59425939 { record : record } ,
59435940 raw === true ? { raw : this . state . rawBuffer . toString ( encoding ) } : { } ,
59445941 info === true ? { info : this . __infoRecord ( ) } : { }
5945- ) ) ;
5942+ ) ;
5943+ const err = this . __push (
5944+ objname === undefined ? extRecord : [ record [ objname ] , extRecord ]
5945+ ) ;
59465946 if ( err ) {
59475947 return err ;
59485948 }
59495949 } else {
5950- const err = this . __push ( record ) ;
5950+ const err = this . __push (
5951+ objname === undefined ? record : [ record [ objname ] , record ]
5952+ ) ;
59515953 if ( err ) {
59525954 return err ;
59535955 }
0 commit comments