Skip to content

Commit 015b936

Browse files
committed
feat(csv-parse): objname index
1 parent 7498b44 commit 015b936

10 files changed

Lines changed: 475 additions & 385 deletions

File tree

packages/csv-parse/dist/cjs/index.cjs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/csv-parse/dist/cjs/sync.cjs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/csv-parse/dist/esm/index.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5338,9 +5338,20 @@ class Parser extends Transform {
53385338
throw new Error(`Invalid Option: objname must be a non empty string`);
53395339
}
53405340
// Great, nothing to do
5341-
}else {
5341+
}else if(typeof options.objname === 'number');else {
53425342
throw new Error(`Invalid Option: objname must be a string or a buffer, got ${options.objname}`);
53435343
}
5344+
if(options.objname !== undefined){
5345+
if(typeof options.objname === 'number'){
5346+
if(options.columns !== false){
5347+
throw Error('Invalid Option: objname index cannot be combined with columns or be defined as a field');
5348+
}
5349+
}else { // A string or a buffer
5350+
if(options.columns === false){
5351+
throw Error('Invalid Option: objname field must be combined with columns or be defined as an index');
5352+
}
5353+
}
5354+
}
53445355
// Normalize option `on_record`
53455356
if(options.on_record === undefined || options.on_record === null){
53465357
options.on_record = undefined;
@@ -5878,6 +5889,7 @@ class Parser extends Transform {
58785889
}
58795890
this.info.records++;
58805891
if(from === 1 || this.info.records >= from){
5892+
const {objname} = this.options;
58815893
// With columns, records are object
58825894
if(columns !== false){
58835895
const obj = {};
@@ -5895,55 +5907,45 @@ class Parser extends Transform {
58955907
obj[columns[i].name] = record[i];
58965908
}
58975909
}
5898-
const {objname} = this.options;
58995910
// Without objname (default)
5900-
if(objname === undefined){
5901-
if(raw === true || info === true){
5902-
const err = this.__push(Object.assign(
5903-
{record: obj},
5904-
(raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}),
5905-
(info === true ? {info: this.__infoRecord()}: {})
5906-
));
5907-
if(err){
5908-
return err;
5909-
}
5910-
}else {
5911-
const err = this.__push(obj);
5912-
if(err){
5913-
return err;
5914-
}
5911+
if(raw === true || info === true){
5912+
const extRecord = Object.assign(
5913+
{record: obj},
5914+
(raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}),
5915+
(info === true ? {info: this.__infoRecord()}: {})
5916+
);
5917+
const err = this.__push(
5918+
objname === undefined ? extRecord : [obj[objname], extRecord]
5919+
);
5920+
if(err){
5921+
return err;
59155922
}
5916-
// With objname (default)
59175923
}else {
5918-
if(raw === true || info === true){
5919-
const err = this.__push(Object.assign(
5920-
{record: [obj[objname], obj]},
5921-
raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {},
5922-
info === true ? {info: this.__infoRecord()}: {}
5923-
));
5924-
if(err){
5925-
return err;
5926-
}
5927-
}else {
5928-
const err = this.__push([obj[objname], obj]);
5929-
if(err){
5930-
return err;
5931-
}
5924+
const err = this.__push(
5925+
objname === undefined ? obj : [obj[objname], obj]
5926+
);
5927+
if(err){
5928+
return err;
59325929
}
59335930
}
59345931
// Without columns, records are array
59355932
}else {
59365933
if(raw === true || info === true){
5937-
const err = this.__push(Object.assign(
5934+
const extRecord = Object.assign(
59385935
{record: record},
59395936
raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {},
59405937
info === true ? {info: this.__infoRecord()}: {}
5941-
));
5938+
);
5939+
const err = this.__push(
5940+
objname === undefined ? extRecord : [record[objname], extRecord]
5941+
);
59425942
if(err){
59435943
return err;
59445944
}
59455945
}else {
5946-
const err = this.__push(record);
5946+
const err = this.__push(
5947+
objname === undefined ? record : [record[objname], record]
5948+
);
59475949
if(err){
59485950
return err;
59495951
}

0 commit comments

Comments
 (0)