diff --git a/packages/csv-parse/dist/cjs/index.cjs b/packages/csv-parse/dist/cjs/index.cjs index 224656b82..4ae799e76 100644 --- a/packages/csv-parse/dist/cjs/index.cjs +++ b/packages/csv-parse/dist/cjs/index.cjs @@ -5342,9 +5342,20 @@ class Parser extends Transform { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5882,6 +5893,7 @@ class Parser extends Transform { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5899,55 +5911,45 @@ class Parser extends Transform { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/cjs/sync.cjs b/packages/csv-parse/dist/cjs/sync.cjs index 93809fb83..c6575eb18 100644 --- a/packages/csv-parse/dist/cjs/sync.cjs +++ b/packages/csv-parse/dist/cjs/sync.cjs @@ -5342,9 +5342,20 @@ class Parser extends Transform { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5882,6 +5893,7 @@ class Parser extends Transform { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5899,55 +5911,45 @@ class Parser extends Transform { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/esm/index.js b/packages/csv-parse/dist/esm/index.js index d3bf40ca4..54b14af1d 100644 --- a/packages/csv-parse/dist/esm/index.js +++ b/packages/csv-parse/dist/esm/index.js @@ -5338,9 +5338,20 @@ class Parser extends Transform { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5878,6 +5889,7 @@ class Parser extends Transform { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5895,55 +5907,45 @@ class Parser extends Transform { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/esm/sync.js b/packages/csv-parse/dist/esm/sync.js index cd5218aae..a279ad05c 100644 --- a/packages/csv-parse/dist/esm/sync.js +++ b/packages/csv-parse/dist/esm/sync.js @@ -5338,9 +5338,20 @@ class Parser extends Transform { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5878,6 +5889,7 @@ class Parser extends Transform { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5895,55 +5907,45 @@ class Parser extends Transform { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/iife/index.js b/packages/csv-parse/dist/iife/index.js index 8e1e2686d..0e4c7a930 100644 --- a/packages/csv-parse/dist/iife/index.js +++ b/packages/csv-parse/dist/iife/index.js @@ -5341,9 +5341,20 @@ var csv_parse = (function (exports) { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5881,6 +5892,7 @@ var csv_parse = (function (exports) { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5898,55 +5910,45 @@ var csv_parse = (function (exports) { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/iife/sync.js b/packages/csv-parse/dist/iife/sync.js index 761bafad4..bd0e51a53 100644 --- a/packages/csv-parse/dist/iife/sync.js +++ b/packages/csv-parse/dist/iife/sync.js @@ -5341,9 +5341,20 @@ var csv_parse_sync = (function (exports) { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5881,6 +5892,7 @@ var csv_parse_sync = (function (exports) { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5898,55 +5910,45 @@ var csv_parse_sync = (function (exports) { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/umd/index.js b/packages/csv-parse/dist/umd/index.js index ddd60fd35..5a7b75ab8 100644 --- a/packages/csv-parse/dist/umd/index.js +++ b/packages/csv-parse/dist/umd/index.js @@ -5344,9 +5344,20 @@ throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5884,6 +5895,7 @@ } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5901,55 +5913,45 @@ obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/dist/umd/sync.js b/packages/csv-parse/dist/umd/sync.js index 834bf1ddd..a3767ba15 100644 --- a/packages/csv-parse/dist/umd/sync.js +++ b/packages/csv-parse/dist/umd/sync.js @@ -5344,9 +5344,20 @@ throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do - }else { + }else if(typeof options.objname === 'number');else { throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else { // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -5884,6 +5895,7 @@ } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -5901,55 +5913,45 @@ obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else { - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else { - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else { - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else { if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else { - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/lib/index.js b/packages/csv-parse/lib/index.js index 8b42615ee..2205ee17b 100644 --- a/packages/csv-parse/lib/index.js +++ b/packages/csv-parse/lib/index.js @@ -323,9 +323,25 @@ class Parser extends Transform { throw new Error(`Invalid Option: objname must be a non empty string`); } // Great, nothing to do + }else if(typeof options.objname === 'number'){ + // if(options.objname.length === 0){ + // throw new Error(`Invalid Option: objname must be a non empty string`); + // } + // Great, nothing to do }else{ throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`); } + if(options.objname !== undefined){ + if(typeof options.objname === 'number'){ + if(options.columns !== false){ + throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field'); + } + }else{ // A string or a buffer + if(options.columns === false){ + throw Error('Invalid Option: objname field must be combined with columns or be defined as an index'); + } + } + } // Normalize option `on_record` if(options.on_record === undefined || options.on_record === null){ options.on_record = undefined; @@ -877,6 +893,7 @@ class Parser extends Transform { } this.info.records++; if(from === 1 || this.info.records >= from){ + const {objname} = this.options; // With columns, records are object if(columns !== false){ const obj = {}; @@ -894,55 +911,45 @@ class Parser extends Transform { obj[columns[i].name] = record[i]; } } - const {objname} = this.options; // Without objname (default) - if(objname === undefined){ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: obj}, - (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.__infoRecord()}: {}) - )); - if(err){ - return err; - } - }else{ - const err = this.__push(obj); - if(err){ - return err; - } + if(raw === true || info === true){ + const extRecord = Object.assign( + {record: obj}, + (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), + (info === true ? {info: this.__infoRecord()}: {}) + ); + const err = this.__push( + objname === undefined ? extRecord : [obj[objname], extRecord] + ); + if(err){ + return err; } - // With objname (default) }else{ - if(raw === true || info === true){ - const err = this.__push(Object.assign( - {record: [obj[objname], obj]}, - raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.__infoRecord()}: {} - )); - if(err){ - return err; - } - }else{ - const err = this.__push([obj[objname], obj]); - if(err){ - return err; - } + const err = this.__push( + objname === undefined ? obj : [obj[objname], obj] + ); + if(err){ + return err; } } // Without columns, records are array }else{ if(raw === true || info === true){ - const err = this.__push(Object.assign( + const extRecord = Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, info === true ? {info: this.__infoRecord()}: {} - )); + ); + const err = this.__push( + objname === undefined ? extRecord : [record[objname], extRecord] + ); if(err){ return err; } }else{ - const err = this.__push(record); + const err = this.__push( + objname === undefined ? record : [record[objname], record] + ); if(err){ return err; } diff --git a/packages/csv-parse/test/option.objname.coffee b/packages/csv-parse/test/option.objname.coffee index e56d1fce0..9835f78ab 100644 --- a/packages/csv-parse/test/option.objname.coffee +++ b/packages/csv-parse/test/option.objname.coffee @@ -3,78 +3,145 @@ import { parse } from '../lib/index.js' describe 'Option `objname`', -> - it 'validation', -> - parse '', objname: 'sth', (->) - parse '', objname: Buffer.from('sth'), (->) - parse '', objname: null, (->) - parse '', objname: undefined, (->) - (-> - parse '', objname: '', (->) - ).should.throw 'Invalid Option: objname must be a non empty string' - (-> - parse '', objname: Buffer.from(''), (->) - ).should.throw 'Invalid Option: objname must be a non empty buffer' - (-> - parse '', objname: true, (->) - ).should.throw 'Invalid Option: objname must be a string or a buffer, got true' + describe 'validation', -> - it 'convert a buffer to a column name', (next) -> - parse """ - a,b,c - """, objname: Buffer.from('h1'), columns: ['h1', 'h2', 'h3'], (err, data) -> - data.should.eql( - 'a': - 'h1': 'a' - 'h2': 'b' - 'h3': 'c' - ) unless err - next err + it 'basic', -> + parse '', objname: 'sth', columns: true, (->) + parse '', objname: Buffer.from('sth'), columns: true, (->) + parse '', objname: 1, (->) + parse '', objname: null, (->) + parse '', objname: undefined, (->) + (-> + parse '', objname: '', (->) + ).should.throw 'Invalid Option: objname must be a non empty string' + (-> + parse '', objname: Buffer.from(''), (->) + ).should.throw 'Invalid Option: objname must be a non empty buffer' + (-> + parse '', objname: true, (->) + ).should.throw 'Invalid Option: objname must be a string or a buffer, got true' + + it 'field require columns', -> + (-> + parse '', objname: 'field', (->) + ).should.throw [ + 'Invalid Option:' + 'objname field must be combined with columns' + 'or be defined as an index' + ].join ' ' + + it 'index incompatible with columns', -> + (-> + parse '', objname: 1, columns: true, (->) + ).should.throw [ + 'Invalid Option:' + 'objname index cannot be combined with columns' + 'or be defined as a field' + ].join ' ' + + describe 'map to a field', -> + + it 'convert a buffer to a column name', (next) -> + parse ''' + a,b,c + ''', objname: Buffer.from('h1'), columns: ['h1', 'h2', 'h3'], (err, data) -> + data.should.eql( + 'a': + 'h1': 'a' + 'h2': 'b' + 'h3': 'c' + ) unless err + next err + + it 'should print object of objects with properties using value of given column from columns', (next) -> + parse ''' + a,b,c + d,e,f + ''', objname: "FIELD_1", columns: ['FIELD_1', 'FIELD_2', 'FIELD_3'], (err, data) -> + return next err if err + data.should.eql + 'a': + 'FIELD_1': 'a' + 'FIELD_2': 'b' + 'FIELD_3': 'c' + 'd': + 'FIELD_1': 'd' + 'FIELD_2': 'e' + 'FIELD_3': 'f' + next err + + it 'should print object of objects with properties using value of given column from header record', (next) -> + parse ''' + FIELD_1,FIELD_2,FIELD_3 + a,b,c + d,e,f + ''', objname: "FIELD_1", columns: true, (err, data) -> + return next err if err + data.should.eql + 'a': + 'FIELD_1': 'a' + 'FIELD_2': 'b' + 'FIELD_3': 'c' + 'd': + 'FIELD_1': 'd' + 'FIELD_2': 'e' + 'FIELD_3': 'f' + next() + + it 'combined with info', (next) -> + parse ''' + FIELD_1,FIELD_2,FIELD_3 + a,b,c + d,e,f + ''', objname: 'FIELD_2', columns: true, info: true, (err, data) -> + return next err if err + data.should.match + 'b': + record: + 'FIELD_1': 'a' + 'FIELD_2': 'b' + 'FIELD_3': 'c' + info: + bytes: 30 + lines: 2 + 'e': + record: + 'FIELD_1': 'd' + 'FIELD_2': 'e' + 'FIELD_3': 'f' + info: + bytes: 35 + lines: 3 + next() + describe 'map to an index', -> - it 'should print object of objects with properties using value of given column from columns', (next) -> - parse """ - 20322051544,1979,8.8017226E7,ABC,45,2000-01-01 - 28392898392,1974,8.8392926E7,DEF,23,2050-11-27 - """, objname: "FIELD_1", columns: ["FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5", "FIELD_6"], (err, data) -> - data.should.eql( - "20322051544": - "FIELD_1":"20322051544" - "FIELD_2":"1979" - "FIELD_3":"8.8017226E7" - "FIELD_4":"ABC" - "FIELD_5":"45" - "FIELD_6":"2000-01-01" - , - "28392898392": - "FIELD_1":"28392898392" - "FIELD_2":"1974" - "FIELD_3": "8.8392926E7" - "FIELD_4":"DEF" - "FIELD_5":"23" - "FIELD_6":"2050-11-27" - ) unless err - next err + it 'get value associated with index', (next) -> + parse ''' + a,b,c + d,e,f + ''', objname: 1, (err, data) -> + return next err if err + data.should.eql + 'b': [ 'a', 'b', 'c' ] + 'e': [ 'd', 'e', 'f' ] + next() - it 'should print object of objects with properties using value of given column from header record', (next) -> - parse """ - FIELD_1,FIELD_2,FIELD_3,FIELD_4,FIELD_5,FIELD_6 - 20322051544,1979,8.8017226E7,ABC,45,2000-01-01 - 28392898392,1974,8.8392926E7,DEF,23,2050-11-27 - """, objname: "FIELD_1", columns: true, (err, data) -> - return next err if err - data.should.eql "20322051544": - "FIELD_1":"20322051544" - "FIELD_2":"1979" - "FIELD_3":"8.8017226E7" - "FIELD_4":"ABC" - "FIELD_5":"45" - "FIELD_6":"2000-01-01" - , - "28392898392": - "FIELD_1":"28392898392" - "FIELD_2":"1974" - "FIELD_3": "8.8392926E7" - "FIELD_4":"DEF" - "FIELD_5":"23" - "FIELD_6":"2050-11-27" - next() + it 'combined with info', (next) -> + parse ''' + a,b,c + d,e,f + ''', objname: 1, info: true, (err, data) -> + return next err if err + data.should.match + 'b': + record: [ 'a', 'b', 'c' ] + info: + bytes: 6 + lines: 1 + 'e': + record: [ 'd', 'e', 'f' ] + info: + bytes: 11 + lines: 2 + next()