Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
refactor: rename raw to record
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jan 17, 2021
1 parent 89bb405 commit 8bb7390
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 71 deletions.
58 changes: 29 additions & 29 deletions lib/es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
}))),
trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]],
wasQuoting: false,
wasRowDelimiter: false
wasRecordDelimiter: false
};
} // Implementation of `Transform._transform`

Expand Down Expand Up @@ -625,14 +625,14 @@ var Parser = /*#__PURE__*/function (_Transform) {
break;
}

if (this.state.wasRowDelimiter === true) {
if (this.state.wasRecordDelimiter === true) {
this.info.lines++;

if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) {
this.state.info = Object.assign({}, this.info);
}

this.state.wasRowDelimiter = false;
this.state.wasRecordDelimiter = false;
}

if (to_line !== -1 && this.info.lines > to_line) {
Expand All @@ -643,7 +643,7 @@ var Parser = /*#__PURE__*/function (_Transform) {


if (this.state.quoting === false && record_delimiter.length === 0) {
var record_delimiterCount = this.__autoDiscoverRowDelimiter(buf, pos);
var record_delimiterCount = this.__autoDiscoverRecordDelimiter(buf, pos);

if (record_delimiterCount) {
record_delimiter = this.options.record_delimiter;
Expand All @@ -656,8 +656,8 @@ var Parser = /*#__PURE__*/function (_Transform) {
rawBuffer.append(chr);
}

if ((chr === cr || chr === nl) && this.state.wasRowDelimiter === false) {
this.state.wasRowDelimiter = true;
if ((chr === cr || chr === nl) && this.state.wasRecordDelimiter === false) {
this.state.wasRecordDelimiter = true;
} // Previous char was a valid escape char
// treat the current char as a regular char

Expand Down Expand Up @@ -694,18 +694,18 @@ var Parser = /*#__PURE__*/function (_Transform) {

var isNextChrDelimiter = this.__isDelimiter(buf, pos + quote.length, nextChr);

var isNextChrRowDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRowDelimiter(buf, pos + quote.length) : this.__isRecordDelimiter(nextChr, buf, pos + quote.length); // Escape a quote
var isNextChrRecordDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRecordDelimiter(buf, pos + quote.length) : this.__isRecordDelimiter(nextChr, buf, pos + quote.length); // Escape a quote
// Treat next char as a regular character

if (escape !== null && this.__isEscape(buf, pos, chr) && this.__isQuote(buf, pos + escape.length)) {
pos += escape.length - 1;
} else if (!nextChr || isNextChrDelimiter || isNextChrRowDelimiter || isNextChrComment || isNextChrTrimable) {
} else if (!nextChr || isNextChrDelimiter || isNextChrRecordDelimiter || isNextChrComment || isNextChrTrimable) {
this.state.quoting = false;
this.state.wasQuoting = true;
pos += quote.length - 1;
continue;
} else if (relax === false) {
var err = this.__error(new CsvError('CSV_INVALID_CLOSING_QUOTE', ['Invalid Closing Quote:', "got \"".concat(String.fromCharCode(nextChr), "\""), "at line ".concat(this.info.lines), 'instead of delimiter, row delimiter, trimable character', '(if activated) or comment'], this.options, this.__context()));
var err = this.__error(new CsvError('CSV_INVALID_CLOSING_QUOTE', ['Invalid Closing Quote:', "got \"".concat(String.fromCharCode(nextChr), "\""), "at line ".concat(this.info.lines), 'instead of delimiter, record delimiter, trimable character', '(if activated) or comment'], this.options, this.__context()));

if (err !== undefined) return err;
} else {
Expand Down Expand Up @@ -750,12 +750,12 @@ var Parser = /*#__PURE__*/function (_Transform) {
} // Activate records emition if above from_line


if (this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1 : 0) >= from_line) {
if (this.state.enabled === false && this.info.lines + (this.state.wasRecordDelimiter === true ? 1 : 0) >= from_line) {
this.state.enabled = true;

this.__resetField();

this.__resetRow();
this.__resetRecord();

pos += recordDelimiterLength - 1;
continue;
Expand All @@ -764,7 +764,7 @@ var Parser = /*#__PURE__*/function (_Transform) {

if (errField !== undefined) return errField;

var errRecord = this.__onRow();
var errRecord = this.__onRecord();

if (errRecord !== undefined) return errRecord;
}
Expand Down Expand Up @@ -838,10 +838,10 @@ var Parser = /*#__PURE__*/function (_Transform) {

if (_errField2 !== undefined) return _errField2;

var _errRecord = this.__onRow();
var _errRecord = this.__onRecord();

if (_errRecord !== undefined) return _errRecord;
} else if (this.state.wasRowDelimiter === true) {
} else if (this.state.wasRecordDelimiter === true) {
this.info.empty_lines++;
} else if (this.state.commenting === true) {
this.info.comment_lines++;
Expand All @@ -851,14 +851,14 @@ var Parser = /*#__PURE__*/function (_Transform) {
this.state.previousBuf = buf.slice(pos);
}

if (this.state.wasRowDelimiter === true) {
if (this.state.wasRecordDelimiter === true) {
this.info.lines++;
this.state.wasRowDelimiter = false;
this.state.wasRecordDelimiter = false;
}
}
}, {
key: "__onRow",
value: function __onRow() {
key: "__onRecord",
value: function __onRecord() {
var _this$options2 = this.options,
columns = _this$options2.columns,
columns_duplicates_to_array = _this$options2.columns_duplicates_to_array,
Expand All @@ -875,15 +875,15 @@ var Parser = /*#__PURE__*/function (_Transform) {
record = _this$state2.record;

if (enabled === false) {
return this.__resetRow();
return this.__resetRecord();
} // Convert the first line into column names


var recordLength = record.length;

if (columns === true) {
if (isRecordEmpty(record)) {
this.__resetRow();
this.__resetRecord();

return;
}
Expand Down Expand Up @@ -919,14 +919,14 @@ var Parser = /*#__PURE__*/function (_Transform) {

if (skip_lines_with_empty_values === true) {
if (isRecordEmpty(record)) {
this.__resetRow();
this.__resetRecord();

return;
}
}

if (this.state.recordHasError === true) {
this.__resetRow();
this.__resetRecord();

this.state.recordHasError = false;
return;
Expand Down Expand Up @@ -1018,7 +1018,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
}
}

this.__resetRow();
this.__resetRecord();
}
}, {
key: "__firstLineToColumns",
Expand All @@ -1038,16 +1038,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
this.state.expectedRecordLength = normalizedHeaders.length;
this.options.columns = normalizedHeaders;

this.__resetRow();
this.__resetRecord();

return;
} catch (err) {
return err;
}
}
}, {
key: "__resetRow",
value: function __resetRow() {
key: "__resetRecord",
value: function __resetRecord() {
if (this.options.raw === true) {
this.state.rawBuffer.reset();
}
Expand Down Expand Up @@ -1197,7 +1197,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
recordDelimiterMaxLength = _this$state4.recordDelimiterMaxLength;
var numOfCharLeft = bufLen - i - 1;
var requiredLength = Math.max(needMoreDataSize, // Skip if the remaining buffer smaller than record delimiter
recordDelimiterMaxLength, // Skip if the remaining buffer can be row delimiter following the closing quote
recordDelimiterMaxLength, // Skip if the remaining buffer can be record delimiter following the closing quote
// 1 is for quote.length
quoting ? quote.length + recordDelimiterMaxLength : 0);
return numOfCharLeft < requiredLength;
Expand Down Expand Up @@ -1281,8 +1281,8 @@ var Parser = /*#__PURE__*/function (_Transform) {
return true;
}
}, {
key: "__autoDiscoverRowDelimiter",
value: function __autoDiscoverRowDelimiter(buf, pos) {
key: "__autoDiscoverRecordDelimiter",
value: function __autoDiscoverRecordDelimiter(buf, pos) {
var encoding = this.options.encoding;
var chr = buf[pos];

Expand Down
52 changes: 26 additions & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class Parser extends Transform {
recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 2 : Math.max(...options.record_delimiter.map( (v) => v.length)),
trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]],
wasQuoting: false,
wasRowDelimiter: false
wasRecordDelimiter: false
}
}
// Implementation of `Transform._transform`
Expand Down Expand Up @@ -508,12 +508,12 @@ class Parser extends Transform {
if(this.__needMoreData(pos, bufLen, end)){
break
}
if(this.state.wasRowDelimiter === true){
if(this.state.wasRecordDelimiter === true){
this.info.lines++
if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){
this.state.info = Object.assign({}, this.info)
}
this.state.wasRowDelimiter = false
this.state.wasRecordDelimiter = false
}
if(to_line !== -1 && this.info.lines > to_line){
this.state.stop = true
Expand All @@ -522,7 +522,7 @@ class Parser extends Transform {
}
// Auto discovery of record_delimiter, unix, mac and windows supported
if(this.state.quoting === false && record_delimiter.length === 0){
const record_delimiterCount = this.__autoDiscoverRowDelimiter(buf, pos)
const record_delimiterCount = this.__autoDiscoverRecordDelimiter(buf, pos)
if(record_delimiterCount){
record_delimiter = this.options.record_delimiter
}
Expand All @@ -531,8 +531,8 @@ class Parser extends Transform {
if(raw === true){
rawBuffer.append(chr)
}
if((chr === cr || chr === nl) && this.state.wasRowDelimiter === false ){
this.state.wasRowDelimiter = true
if((chr === cr || chr === nl) && this.state.wasRecordDelimiter === false ){
this.state.wasRecordDelimiter = true
}
// Previous char was a valid escape char
// treat the current char as a regular char
Expand Down Expand Up @@ -563,12 +563,12 @@ class Parser extends Transform {
const isNextChrTrimable = rtrim && this.__isCharTrimable(nextChr)
const isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos+quote.length, nextChr)
const isNextChrDelimiter = this.__isDelimiter(buf, pos+quote.length, nextChr)
const isNextChrRowDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRowDelimiter(buf, pos+quote.length) : this.__isRecordDelimiter(nextChr, buf, pos+quote.length)
const isNextChrRecordDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRecordDelimiter(buf, pos+quote.length) : this.__isRecordDelimiter(nextChr, buf, pos+quote.length)
// Escape a quote
// Treat next char as a regular character
if(escape !== null && this.__isEscape(buf, pos, chr) && this.__isQuote(buf, pos + escape.length)){
pos += escape.length - 1
}else if(!nextChr || isNextChrDelimiter || isNextChrRowDelimiter || isNextChrComment || isNextChrTrimable){
}else if(!nextChr || isNextChrDelimiter || isNextChrRecordDelimiter || isNextChrComment || isNextChrTrimable){
this.state.quoting = false
this.state.wasQuoting = true
pos += quote.length - 1
Expand All @@ -579,7 +579,7 @@ class Parser extends Transform {
'Invalid Closing Quote:',
`got "${String.fromCharCode(nextChr)}"`,
`at line ${this.info.lines}`,
'instead of delimiter, row delimiter, trimable character',
'instead of delimiter, record delimiter, trimable character',
'(if activated) or comment',
], this.options, this.__context())
)
Expand Down Expand Up @@ -627,16 +627,16 @@ class Parser extends Transform {
continue
}
// Activate records emition if above from_line
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0 ) >= from_line){
if(this.state.enabled === false && this.info.lines + (this.state.wasRecordDelimiter === true ? 1: 0 ) >= from_line){
this.state.enabled = true
this.__resetField()
this.__resetRow()
this.__resetRecord()
pos += recordDelimiterLength - 1
continue
}else{
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRow()
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
}
if(to !== -1 && this.info.records >= to){
Expand Down Expand Up @@ -711,9 +711,9 @@ class Parser extends Transform {
if(this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0){
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRow()
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
}else if(this.state.wasRowDelimiter === true){
}else if(this.state.wasRecordDelimiter === true){
this.info.empty_lines++
}else if(this.state.commenting === true){
this.info.comment_lines++
Expand All @@ -722,22 +722,22 @@ class Parser extends Transform {
}else{
this.state.previousBuf = buf.slice(pos)
}
if(this.state.wasRowDelimiter === true){
if(this.state.wasRecordDelimiter === true){
this.info.lines++
this.state.wasRowDelimiter = false
this.state.wasRecordDelimiter = false
}
}
__onRow(){
__onRecord(){
const {columns, columns_duplicates_to_array, encoding, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_lines_with_empty_values} = this.options
const {enabled, record} = this.state
if(enabled === false){
return this.__resetRow()
return this.__resetRecord()
}
// Convert the first line into column names
const recordLength = record.length
if(columns === true){
if(isRecordEmpty(record)){
this.__resetRow()
this.__resetRecord()
return
}
return this.__firstLineToColumns(record)
Expand Down Expand Up @@ -779,12 +779,12 @@ class Parser extends Transform {
}
if(skip_lines_with_empty_values === true){
if(isRecordEmpty(record)){
this.__resetRow()
this.__resetRecord()
return
}
}
if(this.state.recordHasError === true){
this.__resetRow()
this.__resetRecord()
this.state.recordHasError = false
return
}
Expand Down Expand Up @@ -858,7 +858,7 @@ class Parser extends Transform {
}
}
}
this.__resetRow()
this.__resetRecord()
}
__firstLineToColumns(record){
const {firstLineToHeaders} = this.state
Expand All @@ -878,13 +878,13 @@ class Parser extends Transform {
const normalizedHeaders = normalizeColumnsArray(headers)
this.state.expectedRecordLength = normalizedHeaders.length
this.options.columns = normalizedHeaders
this.__resetRow()
this.__resetRecord()
return
}catch(err){
return err
}
}
__resetRow(){
__resetRecord(){
if(this.options.raw === true){
this.state.rawBuffer.reset()
}
Expand Down Expand Up @@ -987,7 +987,7 @@ class Parser extends Transform {
needMoreDataSize,
// Skip if the remaining buffer smaller than record delimiter
recordDelimiterMaxLength,
// Skip if the remaining buffer can be row delimiter following the closing quote
// Skip if the remaining buffer can be record delimiter following the closing quote
// 1 is for quote.length
quoting ? (quote.length + recordDelimiterMaxLength) : 0,
)
Expand Down Expand Up @@ -1049,7 +1049,7 @@ class Parser extends Transform {
}
return true
}
__autoDiscoverRowDelimiter(buf, pos){
__autoDiscoverRecordDelimiter(buf, pos){
const {encoding} = this.options
const chr = buf[pos]
if(chr === cr){
Expand Down
Loading

0 comments on commit 8bb7390

Please sign in to comment.