Skip to content

Commit

Permalink
feat(csv-parse): objname index
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 5, 2021
1 parent 7498b44 commit 015b936
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 385 deletions.
72 changes: 37 additions & 35 deletions packages/csv-parse/dist/cjs/index.cjs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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;
}
Expand Down
72 changes: 37 additions & 35 deletions packages/csv-parse/dist/cjs/sync.cjs
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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;
}
Expand Down
72 changes: 37 additions & 35 deletions packages/csv-parse/dist/esm/index.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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;
}
Expand Down

0 comments on commit 015b936

Please sign in to comment.