csv-parse
Advanced tools
Comparing version 4.12.0 to 4.13.0
@@ -13,2 +13,17 @@ | ||
## Version 4.13.0 | ||
New features: | ||
* encoding: auto-detect from the bom | ||
* encoding: new option | ||
* bom: multi bom encoding | ||
Fixes & enhancements: | ||
* delimiter: fix buffer size computation | ||
* quote: compatibility with buffer size | ||
* api: partial cache for needMoreData | ||
* escape: support multiple characters | ||
* quote: support multiple characters | ||
* api: fix internal argument name | ||
## Version 4.12.0 | ||
@@ -15,0 +30,0 @@ |
@@ -98,2 +98,6 @@ // Original definitions in https://github.com/DefinitelyTyped/DefinitelyTyped by: David Muller <https://github.com/davidm77> | ||
/** | ||
* Set the source and destination encoding, a value of `null` returns buffer instead of strings. | ||
*/ | ||
encoding?: string | null; | ||
/** | ||
* Set the escape character, one character only, defaults to double quotes. | ||
@@ -233,3 +237,3 @@ */ | ||
constructor(code: CsvErrorCode, message: string | string[], ...contexts: any[]); | ||
constructor(code: CsvErrorCode, message: string | string[], options?: Options, ...contexts: any[]); | ||
} | ||
@@ -236,0 +240,0 @@ |
@@ -72,5 +72,16 @@ "use strict"; | ||
var cr = 13; | ||
var space = 32; | ||
var bom_utf8 = Buffer.from([239, 187, 191]); | ||
var space = 32; // const bom_utf8 = Buffer.from([239, 187, 191]) | ||
var boms = { | ||
// Note, the following are equals: | ||
// Buffer.from("\ufeff") | ||
// Buffer.from([239, 187, 191]) | ||
// Buffer.from('EFBBBF', 'hex') | ||
'utf8': Buffer.from([239, 187, 191]), | ||
// Note, the following are equals: | ||
// Buffer.from "\ufeff", 'utf16le | ||
// Buffer.from([255, 254]) | ||
'utf16le': Buffer.from([255, 254]) | ||
}; | ||
var Parser = /*#__PURE__*/function (_Transform) { | ||
@@ -88,397 +99,426 @@ _inherits(Parser, _Transform); | ||
_this = _super.call(this, _objectSpread(_objectSpread({}, { | ||
// const encoding = opts.encoding | ||
// delete opts.encoding | ||
_this = _super.call(this, _objectSpread(_objectSpread(_objectSpread({}, { | ||
readableObjectMode: true | ||
}), opts)); | ||
var options = {}; // Merge with user options | ||
}), opts), {}, { | ||
encoding: null | ||
})); | ||
_this.__originalOptions = opts; | ||
for (var opt in opts) { | ||
options[underscore(opt)] = opts[opt]; | ||
} // Normalize option `bom` | ||
_this.__normalizeOptions(opts); | ||
return _this; | ||
} | ||
if (options.bom === undefined || options.bom === null || options.bom === false) { | ||
options.bom = false; | ||
} else if (options.bom !== true) { | ||
throw new CsvError('CSV_INVALID_OPTION_BOM', ['Invalid option bom:', 'bom must be true,', "got ".concat(JSON.stringify(options.bom))]); | ||
} // Normalize option `cast` | ||
_createClass(Parser, [{ | ||
key: "__normalizeOptions", | ||
value: function __normalizeOptions(opts) { | ||
var options = {}; // const options = {encoding: encoding} | ||
// Merge with user options | ||
for (var opt in opts) { | ||
options[underscore(opt)] = opts[opt]; | ||
} // Normalize option `encoding` | ||
// Note: defined first because other options depends on it | ||
// to convert chars/strings into buffers. | ||
var fnCastField = null; | ||
if (options.cast === undefined || options.cast === null || options.cast === false || options.cast === '') { | ||
options.cast = undefined; | ||
} else if (typeof options.cast === 'function') { | ||
fnCastField = options.cast; | ||
options.cast = true; | ||
} else if (options.cast !== true) { | ||
throw new CsvError('CSV_INVALID_OPTION_CAST', ['Invalid option cast:', 'cast must be true or a function,', "got ".concat(JSON.stringify(options.cast))]); | ||
} // Normalize option `cast_date` | ||
if (options.encoding === undefined) { | ||
options.encoding = 'utf8'; | ||
} else if (typeof options.encoding !== 'string' && options.encoding !== null) { | ||
throw new CsvError('CSV_INVALID_OPTION_ENCODING', ['Invalid option encoding:', 'encoding must be a string or null to return a buffer,', "got ".concat(JSON.stringify(options.encoding))], options); | ||
} // Normalize option `bom` | ||
if (options.cast_date === undefined || options.cast_date === null || options.cast_date === false || options.cast_date === '') { | ||
options.cast_date = false; | ||
} else if (options.cast_date === true) { | ||
options.cast_date = function (value) { | ||
var date = Date.parse(value); | ||
return !isNaN(date) ? new Date(date) : value; | ||
}; | ||
} else if (typeof options.cast_date !== 'function') { | ||
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', ['Invalid option cast_date:', 'cast_date must be true or a function,', "got ".concat(JSON.stringify(options.cast_date))]); | ||
} // Normalize option `columns` | ||
if (options.bom === undefined || options.bom === null || options.bom === false) { | ||
options.bom = false; | ||
} else if (options.bom !== true) { | ||
throw new CsvError('CSV_INVALID_OPTION_BOM', ['Invalid option bom:', 'bom must be true,', "got ".concat(JSON.stringify(options.bom))], options); | ||
} // Normalize option `cast` | ||
var fnFirstLineToHeaders = null; | ||
var fnCastField = null; | ||
if (options.columns === true) { | ||
// Fields in the first line are converted as-is to columns | ||
fnFirstLineToHeaders = undefined; | ||
} else if (typeof options.columns === 'function') { | ||
fnFirstLineToHeaders = options.columns; | ||
options.columns = true; | ||
} else if (Array.isArray(options.columns)) { | ||
options.columns = normalizeColumnsArray(options.columns); | ||
} else if (options.columns === undefined || options.columns === null || options.columns === false) { | ||
options.columns = false; | ||
} else { | ||
throw new CsvError('CSV_INVALID_OPTION_COLUMNS', ['Invalid option columns:', 'expect an object, a function or true,', "got ".concat(JSON.stringify(options.columns))]); | ||
} // Normalize option `columns_duplicates_to_array` | ||
if (options.cast === undefined || options.cast === null || options.cast === false || options.cast === '') { | ||
options.cast = undefined; | ||
} else if (typeof options.cast === 'function') { | ||
fnCastField = options.cast; | ||
options.cast = true; | ||
} else if (options.cast !== true) { | ||
throw new CsvError('CSV_INVALID_OPTION_CAST', ['Invalid option cast:', 'cast must be true or a function,', "got ".concat(JSON.stringify(options.cast))], options); | ||
} // Normalize option `cast_date` | ||
if (options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false) { | ||
options.columns_duplicates_to_array = false; | ||
} else if (options.columns_duplicates_to_array !== true) { | ||
throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', ['Invalid option columns_duplicates_to_array:', 'expect an boolean,', "got ".concat(JSON.stringify(options.columns_duplicates_to_array))]); | ||
} // Normalize option `comment` | ||
if (options.cast_date === undefined || options.cast_date === null || options.cast_date === false || options.cast_date === '') { | ||
options.cast_date = false; | ||
} else if (options.cast_date === true) { | ||
options.cast_date = function (value) { | ||
var date = Date.parse(value); | ||
return !isNaN(date) ? new Date(date) : value; | ||
}; | ||
} else if (typeof options.cast_date !== 'function') { | ||
throw new CsvError('CSV_INVALID_OPTION_CAST_DATE', ['Invalid option cast_date:', 'cast_date must be true or a function,', "got ".concat(JSON.stringify(options.cast_date))], options); | ||
} // Normalize option `columns` | ||
if (options.comment === undefined || options.comment === null || options.comment === false || options.comment === '') { | ||
options.comment = null; | ||
} else { | ||
if (typeof options.comment === 'string') { | ||
options.comment = Buffer.from(options.comment); | ||
} | ||
var fnFirstLineToHeaders = null; | ||
if (!Buffer.isBuffer(options.comment)) { | ||
throw new CsvError('CSV_INVALID_OPTION_COMMENT', ['Invalid option comment:', 'comment must be a buffer or a string,', "got ".concat(JSON.stringify(options.comment))]); | ||
} | ||
} // Normalize option `delimiter` | ||
if (options.columns === true) { | ||
// Fields in the first line are converted as-is to columns | ||
fnFirstLineToHeaders = undefined; | ||
} else if (typeof options.columns === 'function') { | ||
fnFirstLineToHeaders = options.columns; | ||
options.columns = true; | ||
} else if (Array.isArray(options.columns)) { | ||
options.columns = normalizeColumnsArray(options.columns); | ||
} else if (options.columns === undefined || options.columns === null || options.columns === false) { | ||
options.columns = false; | ||
} else { | ||
throw new CsvError('CSV_INVALID_OPTION_COLUMNS', ['Invalid option columns:', 'expect an object, a function or true,', "got ".concat(JSON.stringify(options.columns))], options); | ||
} // Normalize option `columns_duplicates_to_array` | ||
var delimiter_json = JSON.stringify(options.delimiter); | ||
if (!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter]; | ||
if (options.columns_duplicates_to_array === undefined || options.columns_duplicates_to_array === null || options.columns_duplicates_to_array === false) { | ||
options.columns_duplicates_to_array = false; | ||
} else if (options.columns_duplicates_to_array !== true) { | ||
throw new CsvError('CSV_INVALID_OPTION_COLUMNS_DUPLICATES_TO_ARRAY', ['Invalid option columns_duplicates_to_array:', 'expect an boolean,', "got ".concat(JSON.stringify(options.columns_duplicates_to_array))], options); | ||
} // Normalize option `comment` | ||
if (options.delimiter.length === 0) { | ||
throw new CsvError('CSV_INVALID_OPTION_DELIMITER', ['Invalid option delimiter:', 'delimiter must be a non empty string or buffer or array of string|buffer,', "got ".concat(delimiter_json)]); | ||
} | ||
options.delimiter = options.delimiter.map(function (delimiter) { | ||
if (delimiter === undefined || delimiter === null || delimiter === false) { | ||
return Buffer.from(','); | ||
} | ||
if (options.comment === undefined || options.comment === null || options.comment === false || options.comment === '') { | ||
options.comment = null; | ||
} else { | ||
if (typeof options.comment === 'string') { | ||
options.comment = Buffer.from(options.comment, options.encoding); | ||
} | ||
if (typeof delimiter === 'string') { | ||
delimiter = Buffer.from(delimiter); | ||
} | ||
if (!Buffer.isBuffer(options.comment)) { | ||
throw new CsvError('CSV_INVALID_OPTION_COMMENT', ['Invalid option comment:', 'comment must be a buffer or a string,', "got ".concat(JSON.stringify(options.comment))], options); | ||
} | ||
} // Normalize option `delimiter` | ||
if (!Buffer.isBuffer(delimiter) || delimiter.length === 0) { | ||
throw new CsvError('CSV_INVALID_OPTION_DELIMITER', ['Invalid option delimiter:', 'delimiter must be a non empty string or buffer or array of string|buffer,', "got ".concat(delimiter_json)]); | ||
var delimiter_json = JSON.stringify(options.delimiter); | ||
if (!Array.isArray(options.delimiter)) options.delimiter = [options.delimiter]; | ||
if (options.delimiter.length === 0) { | ||
throw new CsvError('CSV_INVALID_OPTION_DELIMITER', ['Invalid option delimiter:', 'delimiter must be a non empty string or buffer or array of string|buffer,', "got ".concat(delimiter_json)], options); | ||
} | ||
return delimiter; | ||
}); // Normalize option `escape` | ||
options.delimiter = options.delimiter.map(function (delimiter) { | ||
if (delimiter === undefined || delimiter === null || delimiter === false) { | ||
return Buffer.from(',', options.encoding); | ||
} | ||
if (options.escape === undefined || options.escape === true) { | ||
options.escape = Buffer.from('"'); | ||
} else if (typeof options.escape === 'string') { | ||
options.escape = Buffer.from(options.escape); | ||
} else if (options.escape === null || options.escape === false) { | ||
options.escape = null; | ||
} | ||
if (typeof delimiter === 'string') { | ||
delimiter = Buffer.from(delimiter, options.encoding); | ||
} | ||
if (options.escape !== null) { | ||
if (!Buffer.isBuffer(options.escape)) { | ||
throw new Error("Invalid Option: escape must be a buffer, a string or a boolean, got ".concat(JSON.stringify(options.escape))); | ||
} else if (options.escape.length !== 1) { | ||
throw new Error("Invalid Option Length: escape must be one character, got ".concat(options.escape.length)); | ||
} else { | ||
options.escape = options.escape[0]; | ||
} | ||
} // Normalize option `from` | ||
if (!Buffer.isBuffer(delimiter) || delimiter.length === 0) { | ||
throw new CsvError('CSV_INVALID_OPTION_DELIMITER', ['Invalid option delimiter:', 'delimiter must be a non empty string or buffer or array of string|buffer,', "got ".concat(delimiter_json)], options); | ||
} | ||
return delimiter; | ||
}); // Normalize option `escape` | ||
if (options.from === undefined || options.from === null) { | ||
options.from = 1; | ||
} else { | ||
if (typeof options.from === 'string' && /\d+/.test(options.from)) { | ||
options.from = parseInt(options.from); | ||
if (options.escape === undefined || options.escape === true) { | ||
options.escape = Buffer.from('"', options.encoding); | ||
} else if (typeof options.escape === 'string') { | ||
options.escape = Buffer.from(options.escape, options.encoding); | ||
} else if (options.escape === null || options.escape === false) { | ||
options.escape = null; | ||
} | ||
if (Number.isInteger(options.from)) { | ||
if (options.from < 0) { | ||
throw new Error("Invalid Option: from must be a positive integer, got ".concat(JSON.stringify(opts.from))); | ||
if (options.escape !== null) { | ||
if (!Buffer.isBuffer(options.escape)) { | ||
throw new Error("Invalid Option: escape must be a buffer, a string or a boolean, got ".concat(JSON.stringify(options.escape))); // }else if(options.escape.length !== 1){ | ||
// throw new Error(`Invalid Option Length: escape must be one character, got ${options.escape.length}`) | ||
// }else{ | ||
// options.escape = options.escape[0] | ||
} | ||
} // Normalize option `from` | ||
if (options.from === undefined || options.from === null) { | ||
options.from = 1; | ||
} else { | ||
throw new Error("Invalid Option: from must be an integer, got ".concat(JSON.stringify(options.from))); | ||
} | ||
} // Normalize option `from_line` | ||
if (typeof options.from === 'string' && /\d+/.test(options.from)) { | ||
options.from = parseInt(options.from); | ||
} | ||
if (Number.isInteger(options.from)) { | ||
if (options.from < 0) { | ||
throw new Error("Invalid Option: from must be a positive integer, got ".concat(JSON.stringify(opts.from))); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: from must be an integer, got ".concat(JSON.stringify(options.from))); | ||
} | ||
} // Normalize option `from_line` | ||
if (options.from_line === undefined || options.from_line === null) { | ||
options.from_line = 1; | ||
} else { | ||
if (typeof options.from_line === 'string' && /\d+/.test(options.from_line)) { | ||
options.from_line = parseInt(options.from_line); | ||
} | ||
if (Number.isInteger(options.from_line)) { | ||
if (options.from_line <= 0) { | ||
throw new Error("Invalid Option: from_line must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.from_line))); | ||
if (options.from_line === undefined || options.from_line === null) { | ||
options.from_line = 1; | ||
} else { | ||
if (typeof options.from_line === 'string' && /\d+/.test(options.from_line)) { | ||
options.from_line = parseInt(options.from_line); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: from_line must be an integer, got ".concat(JSON.stringify(opts.from_line))); | ||
} | ||
} // Normalize option `info` | ||
if (Number.isInteger(options.from_line)) { | ||
if (options.from_line <= 0) { | ||
throw new Error("Invalid Option: from_line must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.from_line))); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: from_line must be an integer, got ".concat(JSON.stringify(opts.from_line))); | ||
} | ||
} // Normalize option `info` | ||
if (options.info === undefined || options.info === null || options.info === false) { | ||
options.info = false; | ||
} else if (options.info !== true) { | ||
throw new Error("Invalid Option: info must be true, got ".concat(JSON.stringify(options.info))); | ||
} // Normalize option `max_record_size` | ||
if (options.info === undefined || options.info === null || options.info === false) { | ||
options.info = false; | ||
} else if (options.info !== true) { | ||
throw new Error("Invalid Option: info must be true, got ".concat(JSON.stringify(options.info))); | ||
} // Normalize option `max_record_size` | ||
if (options.max_record_size === undefined || options.max_record_size === null || options.max_record_size === false) { | ||
options.max_record_size = 0; | ||
} else if (Number.isInteger(options.max_record_size) && options.max_record_size >= 0) {// Great, nothing to do | ||
} else if (typeof options.max_record_size === 'string' && /\d+/.test(options.max_record_size)) { | ||
options.max_record_size = parseInt(options.max_record_size); | ||
} else { | ||
throw new Error("Invalid Option: max_record_size must be a positive integer, got ".concat(JSON.stringify(options.max_record_size))); | ||
} // Normalize option `objname` | ||
if (options.max_record_size === undefined || options.max_record_size === null || options.max_record_size === false) { | ||
options.max_record_size = 0; | ||
} else if (Number.isInteger(options.max_record_size) && options.max_record_size >= 0) {// Great, nothing to do | ||
} else if (typeof options.max_record_size === 'string' && /\d+/.test(options.max_record_size)) { | ||
options.max_record_size = parseInt(options.max_record_size); | ||
} else { | ||
throw new Error("Invalid Option: max_record_size must be a positive integer, got ".concat(JSON.stringify(options.max_record_size))); | ||
} // Normalize option `objname` | ||
if (options.objname === undefined || options.objname === null || options.objname === false) { | ||
options.objname = undefined; | ||
} else if (Buffer.isBuffer(options.objname)) { | ||
if (options.objname.length === 0) { | ||
throw new Error("Invalid Option: objname must be a non empty buffer"); | ||
} | ||
options.objname = options.objname.toString(); | ||
} else if (typeof options.objname === 'string') { | ||
if (options.objname.length === 0) { | ||
throw new Error("Invalid Option: objname must be a non empty string"); | ||
} // Great, nothing to do | ||
if (options.objname === undefined || options.objname === null || options.objname === false) { | ||
options.objname = undefined; | ||
} else if (Buffer.isBuffer(options.objname)) { | ||
if (options.objname.length === 0) { | ||
throw new Error("Invalid Option: objname must be a non empty buffer"); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: objname must be a string or a buffer, got ".concat(options.objname)); | ||
} // Normalize option `on_record` | ||
if (options.encoding === null) {// Don't call `toString`, leave objname as a buffer | ||
} else { | ||
options.objname = options.objname.toString(options.encoding); | ||
} | ||
} else if (typeof options.objname === 'string') { | ||
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 ".concat(options.objname)); | ||
} // Normalize option `on_record` | ||
if (options.on_record === undefined || options.on_record === null) { | ||
options.on_record = undefined; | ||
} else if (typeof options.on_record !== 'function') { | ||
throw new CsvError('CSV_INVALID_OPTION_ON_RECORD', ['Invalid option `on_record`:', 'expect a function,', "got ".concat(JSON.stringify(options.on_record))]); | ||
} // Normalize option `quote` | ||
if (options.on_record === undefined || options.on_record === null) { | ||
options.on_record = undefined; | ||
} else if (typeof options.on_record !== 'function') { | ||
throw new CsvError('CSV_INVALID_OPTION_ON_RECORD', ['Invalid option `on_record`:', 'expect a function,', "got ".concat(JSON.stringify(options.on_record))], options); | ||
} // Normalize option `quote` | ||
if (options.quote === null || options.quote === false || options.quote === '') { | ||
options.quote = null; | ||
} else { | ||
if (options.quote === undefined || options.quote === true) { | ||
options.quote = Buffer.from('"'); | ||
} else if (typeof options.quote === 'string') { | ||
options.quote = Buffer.from(options.quote); | ||
} | ||
if (!Buffer.isBuffer(options.quote)) { | ||
throw new Error("Invalid Option: quote must be a buffer or a string, got ".concat(JSON.stringify(options.quote))); | ||
} else if (options.quote.length !== 1) { | ||
throw new Error("Invalid Option Length: quote must be one character, got ".concat(options.quote.length)); | ||
if (options.quote === null || options.quote === false || options.quote === '') { | ||
options.quote = null; | ||
} else { | ||
options.quote = options.quote[0]; | ||
} | ||
} // Normalize option `raw` | ||
if (options.quote === undefined || options.quote === true) { | ||
options.quote = Buffer.from('"', options.encoding); | ||
} else if (typeof options.quote === 'string') { | ||
options.quote = Buffer.from(options.quote, options.encoding); | ||
} | ||
if (!Buffer.isBuffer(options.quote)) { | ||
throw new Error("Invalid Option: quote must be a buffer or a string, got ".concat(JSON.stringify(options.quote))); // }else if(options.quote.length !== 1){ | ||
// throw new Error(`Invalid Option Length: quote must be one character, got ${options.quote.length}`) | ||
// }else{ | ||
// options.quote = options.quote[0] | ||
} | ||
} // Normalize option `raw` | ||
if (options.raw === undefined || options.raw === null || options.raw === false) { | ||
options.raw = false; | ||
} else if (options.raw !== true) { | ||
throw new Error("Invalid Option: raw must be true, got ".concat(JSON.stringify(options.raw))); | ||
} // Normalize option `record_delimiter` | ||
if (options.raw === undefined || options.raw === null || options.raw === false) { | ||
options.raw = false; | ||
} else if (options.raw !== true) { | ||
throw new Error("Invalid Option: raw must be true, got ".concat(JSON.stringify(options.raw))); | ||
} // Normalize option `record_delimiter` | ||
if (!options.record_delimiter) { | ||
options.record_delimiter = []; | ||
} else if (!Array.isArray(options.record_delimiter)) { | ||
options.record_delimiter = [options.record_delimiter]; | ||
} | ||
options.record_delimiter = options.record_delimiter.map(function (rd) { | ||
if (typeof rd === 'string') { | ||
rd = Buffer.from(rd); | ||
if (!options.record_delimiter) { | ||
options.record_delimiter = []; | ||
} else if (!Array.isArray(options.record_delimiter)) { | ||
options.record_delimiter = [options.record_delimiter]; | ||
} | ||
return rd; | ||
}); // Normalize option `relax` | ||
options.record_delimiter = options.record_delimiter.map(function (rd) { | ||
if (typeof rd === 'string') { | ||
rd = Buffer.from(rd, options.encoding); | ||
} | ||
if (typeof options.relax === 'boolean') {// Great, nothing to do | ||
} else if (options.relax === undefined || options.relax === null) { | ||
options.relax = false; | ||
} else { | ||
throw new Error("Invalid Option: relax must be a boolean, got ".concat(JSON.stringify(options.relax))); | ||
} // Normalize option `relax_column_count` | ||
return rd; | ||
}); // Normalize option `relax` | ||
if (typeof options.relax === 'boolean') {// Great, nothing to do | ||
} else if (options.relax === undefined || options.relax === null) { | ||
options.relax = false; | ||
} else { | ||
throw new Error("Invalid Option: relax must be a boolean, got ".concat(JSON.stringify(options.relax))); | ||
} // Normalize option `relax_column_count` | ||
if (typeof options.relax_column_count === 'boolean') {// Great, nothing to do | ||
} else if (options.relax_column_count === undefined || options.relax_column_count === null) { | ||
options.relax_column_count = false; | ||
} else { | ||
throw new Error("Invalid Option: relax_column_count must be a boolean, got ".concat(JSON.stringify(options.relax_column_count))); | ||
} | ||
if (typeof options.relax_column_count_less === 'boolean') {// Great, nothing to do | ||
} else if (options.relax_column_count_less === undefined || options.relax_column_count_less === null) { | ||
options.relax_column_count_less = false; | ||
} else { | ||
throw new Error("Invalid Option: relax_column_count_less must be a boolean, got ".concat(JSON.stringify(options.relax_column_count_less))); | ||
} | ||
if (typeof options.relax_column_count === 'boolean') {// Great, nothing to do | ||
} else if (options.relax_column_count === undefined || options.relax_column_count === null) { | ||
options.relax_column_count = false; | ||
} else { | ||
throw new Error("Invalid Option: relax_column_count must be a boolean, got ".concat(JSON.stringify(options.relax_column_count))); | ||
} | ||
if (typeof options.relax_column_count_more === 'boolean') {// Great, nothing to do | ||
} else if (options.relax_column_count_more === undefined || options.relax_column_count_more === null) { | ||
options.relax_column_count_more = false; | ||
} else { | ||
throw new Error("Invalid Option: relax_column_count_more must be a boolean, got ".concat(JSON.stringify(options.relax_column_count_more))); | ||
} // Normalize option `skip_empty_lines` | ||
if (typeof options.relax_column_count_less === 'boolean') {// Great, nothing to do | ||
} else if (options.relax_column_count_less === undefined || options.relax_column_count_less === null) { | ||
options.relax_column_count_less = false; | ||
} else { | ||
throw new Error("Invalid Option: relax_column_count_less must be a boolean, got ".concat(JSON.stringify(options.relax_column_count_less))); | ||
} | ||
if (typeof options.relax_column_count_more === 'boolean') {// Great, nothing to do | ||
} else if (options.relax_column_count_more === undefined || options.relax_column_count_more === null) { | ||
options.relax_column_count_more = false; | ||
} else { | ||
throw new Error("Invalid Option: relax_column_count_more must be a boolean, got ".concat(JSON.stringify(options.relax_column_count_more))); | ||
} // Normalize option `skip_empty_lines` | ||
if (typeof options.skip_empty_lines === 'boolean') {// Great, nothing to do | ||
} else if (options.skip_empty_lines === undefined || options.skip_empty_lines === null) { | ||
options.skip_empty_lines = false; | ||
} else { | ||
throw new Error("Invalid Option: skip_empty_lines must be a boolean, got ".concat(JSON.stringify(options.skip_empty_lines))); | ||
} // Normalize option `skip_lines_with_empty_values` | ||
if (typeof options.skip_empty_lines === 'boolean') {// Great, nothing to do | ||
} else if (options.skip_empty_lines === undefined || options.skip_empty_lines === null) { | ||
options.skip_empty_lines = false; | ||
} else { | ||
throw new Error("Invalid Option: skip_empty_lines must be a boolean, got ".concat(JSON.stringify(options.skip_empty_lines))); | ||
} // Normalize option `skip_lines_with_empty_values` | ||
if (typeof options.skip_lines_with_empty_values === 'boolean') {// Great, nothing to do | ||
} else if (options.skip_lines_with_empty_values === undefined || options.skip_lines_with_empty_values === null) { | ||
options.skip_lines_with_empty_values = false; | ||
} else { | ||
throw new Error("Invalid Option: skip_lines_with_empty_values must be a boolean, got ".concat(JSON.stringify(options.skip_lines_with_empty_values))); | ||
} // Normalize option `skip_lines_with_error` | ||
if (typeof options.skip_lines_with_empty_values === 'boolean') {// Great, nothing to do | ||
} else if (options.skip_lines_with_empty_values === undefined || options.skip_lines_with_empty_values === null) { | ||
options.skip_lines_with_empty_values = false; | ||
} else { | ||
throw new Error("Invalid Option: skip_lines_with_empty_values must be a boolean, got ".concat(JSON.stringify(options.skip_lines_with_empty_values))); | ||
} // Normalize option `skip_lines_with_error` | ||
if (typeof options.skip_lines_with_error === 'boolean') {// Great, nothing to do | ||
} else if (options.skip_lines_with_error === undefined || options.skip_lines_with_error === null) { | ||
options.skip_lines_with_error = false; | ||
} else { | ||
throw new Error("Invalid Option: skip_lines_with_error must be a boolean, got ".concat(JSON.stringify(options.skip_lines_with_error))); | ||
} // Normalize option `rtrim` | ||
if (typeof options.skip_lines_with_error === 'boolean') {// Great, nothing to do | ||
} else if (options.skip_lines_with_error === undefined || options.skip_lines_with_error === null) { | ||
options.skip_lines_with_error = false; | ||
} else { | ||
throw new Error("Invalid Option: skip_lines_with_error must be a boolean, got ".concat(JSON.stringify(options.skip_lines_with_error))); | ||
} // Normalize option `rtrim` | ||
if (options.rtrim === undefined || options.rtrim === null || options.rtrim === false) { | ||
options.rtrim = false; | ||
} else if (options.rtrim !== true) { | ||
throw new Error("Invalid Option: rtrim must be a boolean, got ".concat(JSON.stringify(options.rtrim))); | ||
} // Normalize option `ltrim` | ||
if (options.rtrim === undefined || options.rtrim === null || options.rtrim === false) { | ||
options.rtrim = false; | ||
} else if (options.rtrim !== true) { | ||
throw new Error("Invalid Option: rtrim must be a boolean, got ".concat(JSON.stringify(options.rtrim))); | ||
} // Normalize option `ltrim` | ||
if (options.ltrim === undefined || options.ltrim === null || options.ltrim === false) { | ||
options.ltrim = false; | ||
} else if (options.ltrim !== true) { | ||
throw new Error("Invalid Option: ltrim must be a boolean, got ".concat(JSON.stringify(options.ltrim))); | ||
} // Normalize option `trim` | ||
if (options.ltrim === undefined || options.ltrim === null || options.ltrim === false) { | ||
options.ltrim = false; | ||
} else if (options.ltrim !== true) { | ||
throw new Error("Invalid Option: ltrim must be a boolean, got ".concat(JSON.stringify(options.ltrim))); | ||
} // Normalize option `trim` | ||
if (options.trim === undefined || options.trim === null || options.trim === false) { | ||
options.trim = false; | ||
} else if (options.trim !== true) { | ||
throw new Error("Invalid Option: trim must be a boolean, got ".concat(JSON.stringify(options.trim))); | ||
} // Normalize options `trim`, `ltrim` and `rtrim` | ||
if (options.trim === undefined || options.trim === null || options.trim === false) { | ||
options.trim = false; | ||
} else if (options.trim !== true) { | ||
throw new Error("Invalid Option: trim must be a boolean, got ".concat(JSON.stringify(options.trim))); | ||
} // Normalize options `trim`, `ltrim` and `rtrim` | ||
if (options.trim === true && opts.ltrim !== false) { | ||
options.ltrim = true; | ||
} else if (options.ltrim !== true) { | ||
options.ltrim = false; | ||
} | ||
if (options.trim === true && opts.rtrim !== false) { | ||
options.rtrim = true; | ||
} else if (options.rtrim !== true) { | ||
options.rtrim = false; | ||
} // Normalize option `to` | ||
if (options.trim === true && opts.ltrim !== false) { | ||
options.ltrim = true; | ||
} else if (options.ltrim !== true) { | ||
options.ltrim = false; | ||
} | ||
if (options.trim === true && opts.rtrim !== false) { | ||
options.rtrim = true; | ||
} else if (options.rtrim !== true) { | ||
options.rtrim = false; | ||
} // Normalize option `to` | ||
if (options.to === undefined || options.to === null) { | ||
options.to = -1; | ||
} else { | ||
if (typeof options.to === 'string' && /\d+/.test(options.to)) { | ||
options.to = parseInt(options.to); | ||
} | ||
if (Number.isInteger(options.to)) { | ||
if (options.to <= 0) { | ||
throw new Error("Invalid Option: to must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.to))); | ||
if (options.to === undefined || options.to === null) { | ||
options.to = -1; | ||
} else { | ||
if (typeof options.to === 'string' && /\d+/.test(options.to)) { | ||
options.to = parseInt(options.to); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: to must be an integer, got ".concat(JSON.stringify(opts.to))); | ||
} | ||
} // Normalize option `to_line` | ||
if (Number.isInteger(options.to)) { | ||
if (options.to <= 0) { | ||
throw new Error("Invalid Option: to must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.to))); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: to must be an integer, got ".concat(JSON.stringify(opts.to))); | ||
} | ||
} // Normalize option `to_line` | ||
if (options.to_line === undefined || options.to_line === null) { | ||
options.to_line = -1; | ||
} else { | ||
if (typeof options.to_line === 'string' && /\d+/.test(options.to_line)) { | ||
options.to_line = parseInt(options.to_line); | ||
} | ||
if (Number.isInteger(options.to_line)) { | ||
if (options.to_line <= 0) { | ||
throw new Error("Invalid Option: to_line must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.to_line))); | ||
if (options.to_line === undefined || options.to_line === null) { | ||
options.to_line = -1; | ||
} else { | ||
if (typeof options.to_line === 'string' && /\d+/.test(options.to_line)) { | ||
options.to_line = parseInt(options.to_line); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: to_line must be an integer, got ".concat(JSON.stringify(opts.to_line))); | ||
if (Number.isInteger(options.to_line)) { | ||
if (options.to_line <= 0) { | ||
throw new Error("Invalid Option: to_line must be a positive integer greater than 0, got ".concat(JSON.stringify(opts.to_line))); | ||
} | ||
} else { | ||
throw new Error("Invalid Option: to_line must be an integer, got ".concat(JSON.stringify(opts.to_line))); | ||
} | ||
} | ||
} | ||
_this.info = { | ||
comment_lines: 0, | ||
empty_lines: 0, | ||
invalid_field_length: 0, | ||
lines: 1, | ||
records: 0 | ||
}; | ||
_this.options = options; | ||
_this.state = { | ||
bomSkipped: false, | ||
castField: fnCastField, | ||
commenting: false, | ||
enabled: options.from_line === 1, | ||
escaping: false, | ||
escapeIsQuote: options.escape === options.quote, | ||
expectedRecordLength: options.columns === null ? 0 : options.columns.length, | ||
field: new ResizeableBuffer(20), | ||
firstLineToHeaders: fnFirstLineToHeaders, | ||
info: Object.assign({}, _this.info), | ||
previousBuf: undefined, | ||
quoting: false, | ||
stop: false, | ||
rawBuffer: new ResizeableBuffer(100), | ||
record: [], | ||
recordHasError: false, | ||
record_length: 0, | ||
recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 2 : Math.max.apply(Math, _toConsumableArray(options.record_delimiter.map(function (v) { | ||
return v.length; | ||
}))), | ||
trimChars: [Buffer.from(' ')[0], Buffer.from('\t')[0]], | ||
wasQuoting: false, | ||
wasRowDelimiter: false | ||
}; | ||
return _this; | ||
} // Implementation of `Transform._transform` | ||
this.info = { | ||
comment_lines: 0, | ||
empty_lines: 0, | ||
invalid_field_length: 0, | ||
lines: 1, | ||
records: 0 | ||
}; | ||
this.options = options; | ||
this.state = { | ||
bomSkipped: false, | ||
castField: fnCastField, | ||
commenting: false, | ||
enabled: options.from_line === 1, | ||
escaping: false, | ||
// escapeIsQuote: options.escape === options.quote, | ||
escapeIsQuote: Buffer.isBuffer(options.escape) && Buffer.isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0, | ||
expectedRecordLength: options.columns === null ? 0 : options.columns.length, | ||
field: new ResizeableBuffer(20), | ||
firstLineToHeaders: fnFirstLineToHeaders, | ||
info: Object.assign({}, this.info), | ||
needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment | ||
options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) { | ||
return delimiter.length; | ||
})), [// Skip if the remaining buffer can be escape sequence | ||
options.quote !== null ? options.quote.length : 0])), | ||
previousBuf: undefined, | ||
quoting: false, | ||
stop: false, | ||
rawBuffer: new ResizeableBuffer(100), | ||
record: [], | ||
recordHasError: false, | ||
record_length: 0, | ||
recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 2 : Math.max.apply(Math, _toConsumableArray(options.record_delimiter.map(function (v) { | ||
return v.length; | ||
}))), | ||
trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]], | ||
wasQuoting: false, | ||
wasRowDelimiter: false | ||
}; | ||
} // Implementation of `Transform._transform` | ||
_createClass(Parser, [{ | ||
}, { | ||
key: "_transform", | ||
@@ -561,8 +601,15 @@ value: function _transform(buf, encoding, callback) { | ||
return; | ||
} // skip BOM detect because data length < 3 | ||
} | ||
} else { | ||
for (var encoding in boms) { | ||
if (boms[encoding].compare(buf, 0, boms[encoding].length) === 0) { | ||
// Skip BOM | ||
buf = buf.slice(boms[encoding].length); // Renormalize original options with the new encoding | ||
} else { | ||
if (bom_utf8.compare(buf, 0, 3) === 0) { | ||
// Skip BOM | ||
buf = buf.slice(3); | ||
this.__normalizeOptions(_objectSpread(_objectSpread({}, this.__originalOptions), {}, { | ||
encoding: encoding | ||
})); | ||
break; | ||
} | ||
} | ||
@@ -626,6 +673,8 @@ | ||
// We are quoting, the char is an escape chr and there is a chr to escape | ||
if (escape !== null && this.state.quoting === true && chr === escape && pos + 1 < bufLen) { | ||
// if(escape !== null && this.state.quoting === true && chr === escape && pos + 1 < bufLen){ | ||
if (escape !== null && this.state.quoting === true && this.__isEscape(buf, pos, chr) && pos + escape.length < bufLen) { | ||
if (escapeIsQuote) { | ||
if (buf[pos + 1] === quote) { | ||
if (this.__isQuote(buf, pos + escape.length)) { | ||
this.state.escaping = true; | ||
pos += escape.length - 1; | ||
continue; | ||
@@ -635,2 +684,3 @@ } | ||
this.state.escaping = true; | ||
pos += escape.length - 1; | ||
continue; | ||
@@ -640,7 +690,8 @@ } | ||
// TODO: need to compare bytes instead of single char | ||
// if(this.state.commenting === false && chr === quote){ | ||
if (this.state.commenting === false && chr === quote) { | ||
if (this.state.commenting === false && this.__isQuote(buf, pos)) { | ||
if (this.state.quoting === true) { | ||
var nextChr = buf[pos + 1]; | ||
var nextChr = buf[pos + quote.length]; | ||
@@ -650,18 +701,20 @@ var isNextChrTrimable = rtrim && this.__isCharTrimable(nextChr); // const isNextChrComment = nextChr === comment | ||
var isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos + 1, nextChr); | ||
var isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos + quote.length, nextChr); | ||
var isNextChrDelimiter = this.__isDelimiter(nextChr, buf, pos + 1); | ||
var isNextChrDelimiter = this.__isDelimiter(buf, pos + quote.length, nextChr); | ||
var isNextChrRowDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRowDelimiter(buf, pos + 1) : this.__isRecordDelimiter(nextChr, buf, pos + 1); // Escape a quote | ||
var isNextChrRowDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRowDelimiter(buf, pos + quote.length) : this.__isRecordDelimiter(nextChr, buf, pos + quote.length); // Escape a quote | ||
// Treat next char as a regular character | ||
// TODO: need to compare bytes instead of single char | ||
// if(escape !== null && chr === escape && nextChr === quote){ | ||
if (escape !== null && chr === escape && nextChr === quote) { | ||
pos++; | ||
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) { | ||
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.__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, row delimiter, trimable character', '(if activated) or comment'], this.options, this.__context())); | ||
@@ -671,5 +724,5 @@ if (err !== undefined) return err; | ||
this.state.quoting = false; | ||
this.state.wasQuoting = true; // continue | ||
this.state.wasQuoting = true; | ||
this.state.field.prepend(quote); | ||
pos += quote.length - 1; | ||
} | ||
@@ -680,3 +733,3 @@ } else { | ||
if (relax === false) { | ||
var _err = this.__error(new CsvError('INVALID_OPENING_QUOTE', ['Invalid Opening Quote:', "a quote is found inside a field at line ".concat(this.info.lines)], this.__context(), { | ||
var _err = this.__error(new CsvError('INVALID_OPENING_QUOTE', ['Invalid Opening Quote:', "a quote is found inside a field at line ".concat(this.info.lines)], this.options, this.__context(), { | ||
field: this.state.field | ||
@@ -689,2 +742,3 @@ })); | ||
this.state.quoting = true; | ||
pos += quote.length - 1; | ||
continue; | ||
@@ -755,3 +809,3 @@ } | ||
var delimiterLength = this.__isDelimiter(chr, buf, pos); | ||
var delimiterLength = this.__isDelimiter(buf, pos, chr); | ||
@@ -770,3 +824,3 @@ if (delimiterLength !== 0) { | ||
if (max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size) { | ||
var _err2 = this.__error(new CsvError('CSV_MAX_RECORD_SIZE', ['Max Record Size:', 'record exceed the maximum number of tolerated bytes', "of ".concat(max_record_size), "at line ".concat(this.info.lines)], this.__context())); | ||
var _err2 = this.__error(new CsvError('CSV_MAX_RECORD_SIZE', ['Max Record Size:', 'record exceed the maximum number of tolerated bytes', "of ".concat(max_record_size), "at line ".concat(this.info.lines)], this.options, this.__context())); | ||
@@ -784,3 +838,3 @@ if (_err2 !== undefined) return _err2; | ||
} else if (rtrim === true && !this.__isCharTrimable(chr)) { | ||
var _err3 = this.__error(new CsvError('CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE', ['Invalid Closing Quote:', 'found non trimable byte after quote', "at line ".concat(this.info.lines)], this.__context())); | ||
var _err3 = this.__error(new CsvError('CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE', ['Invalid Closing Quote:', 'found non trimable byte after quote', "at line ".concat(this.info.lines)], this.options, this.__context())); | ||
@@ -794,3 +848,3 @@ if (_err3 !== undefined) return _err3; | ||
if (this.state.quoting === true) { | ||
var _err4 = this.__error(new CsvError('CSV_QUOTE_NOT_CLOSED', ['Quote Not Closed:', "the parsing is finished with an opening quote at line ".concat(this.info.lines)], this.__context())); | ||
var _err4 = this.__error(new CsvError('CSV_QUOTE_NOT_CLOSED', ['Quote Not Closed:', "the parsing is finished with an opening quote at line ".concat(this.info.lines)], this.options, this.__context())); | ||
@@ -835,2 +889,3 @@ if (_err4 !== undefined) return _err4; | ||
columns_duplicates_to_array = _this$options2.columns_duplicates_to_array, | ||
encoding = _this$options2.encoding, | ||
info = _this$options2.info, | ||
@@ -873,3 +928,3 @@ from = _this$options2.from, | ||
if (columns === false) { | ||
var err = this.__error(new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.__context(), { | ||
var err = this.__error(new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), { | ||
record: record | ||
@@ -882,3 +937,3 @@ })); | ||
new CsvError('CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH', ['Invalid Record Length:', "columns length is ".concat(columns.length, ","), // rename columns | ||
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.__context(), { | ||
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), { | ||
record: record | ||
@@ -935,3 +990,3 @@ })); | ||
}, raw === true ? { | ||
raw: this.state.rawBuffer.toString() | ||
raw: this.state.rawBuffer.toString(encoding) | ||
} : {}, info === true ? { | ||
@@ -956,3 +1011,3 @@ info: this.state.info | ||
}, raw === true ? { | ||
raw: this.state.rawBuffer.toString() | ||
raw: this.state.rawBuffer.toString(encoding) | ||
} : {}, info === true ? { | ||
@@ -978,3 +1033,3 @@ info: this.state.info | ||
}, raw === true ? { | ||
raw: this.state.rawBuffer.toString() | ||
raw: this.state.rawBuffer.toString(encoding) | ||
} : {}, info === true ? { | ||
@@ -1008,3 +1063,3 @@ info: this.state.info | ||
if (!Array.isArray(headers)) { | ||
return this.__error(new CsvError('CSV_INVALID_COLUMN_MAPPING', ['Invalid Column Mapping:', 'expect an array from column function,', "got ".concat(JSON.stringify(headers))], this.__context(), { | ||
return this.__error(new CsvError('CSV_INVALID_COLUMN_MAPPING', ['Invalid Column Mapping:', 'expect an array from column function,', "got ".concat(JSON.stringify(headers))], this.options, this.__context(), { | ||
headers: headers | ||
@@ -1040,2 +1095,3 @@ })); | ||
cast = _this$options3.cast, | ||
encoding = _this$options3.encoding, | ||
rtrim = _this$options3.rtrim, | ||
@@ -1052,3 +1108,3 @@ max_record_size = _this$options3.max_record_size; | ||
var field = this.state.field.toString(); | ||
var field = this.state.field.toString(encoding); | ||
@@ -1150,8 +1206,8 @@ if (rtrim === true && wasQuoting === false) { | ||
key: "__compareBytes", | ||
value: function __compareBytes(sourceBuf, targetBuf, pos, firtByte) { | ||
if (sourceBuf[0] !== firtByte) return 0; | ||
value: function __compareBytes(sourceBuf, targetBuf, targetPos, firstByte) { | ||
if (sourceBuf[0] !== firstByte) return 0; | ||
var sourceLength = sourceBuf.length; | ||
for (var i = 1; i < sourceLength; i++) { | ||
if (sourceBuf[i] !== targetBuf[pos + i]) return 0; | ||
if (sourceBuf[i] !== targetBuf[targetPos + i]) return 0; | ||
} | ||
@@ -1164,21 +1220,13 @@ | ||
value: function __needMoreData(i, bufLen, end) { | ||
if (end) { | ||
return false; | ||
} | ||
var _this$options5 = this.options, | ||
comment = _this$options5.comment, | ||
delimiter = _this$options5.delimiter; | ||
if (end) return false; | ||
var quote = this.options.quote; | ||
var _this$state4 = this.state, | ||
quoting = _this$state4.quoting, | ||
needMoreDataSize = _this$state4.needMoreDataSize, | ||
recordDelimiterMaxLength = _this$state4.recordDelimiterMaxLength; | ||
var numOfCharLeft = bufLen - i - 1; | ||
var requiredLength = Math.max( // Skip if the remaining buffer smaller than comment | ||
comment ? comment.length : 0, // Skip if the remaining buffer smaller than row delimiter | ||
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 | ||
// 1 is for quote.length | ||
quoting ? 1 + recordDelimiterMaxLength : 0, // Skip if the remaining buffer can be delimiter | ||
delimiter.length, // Skip if the remaining buffer can be escape sequence | ||
// 1 is for escape.length | ||
1); | ||
quoting ? quote.length + recordDelimiterMaxLength : 0); | ||
return numOfCharLeft < requiredLength; | ||
@@ -1188,3 +1236,3 @@ } | ||
key: "__isDelimiter", | ||
value: function __isDelimiter(chr, buf, pos) { | ||
value: function __isDelimiter(buf, pos, chr) { | ||
var delimiter = this.options.delimiter; | ||
@@ -1232,4 +1280,39 @@ | ||
}, { | ||
key: "__isEscape", | ||
value: function __isEscape(buf, pos, chr) { | ||
var escape = this.options.escape; | ||
if (escape === null) return false; | ||
var l = escape.length; | ||
if (escape[0] === chr) { | ||
for (var i = 0; i < l; i++) { | ||
if (escape[i] !== buf[pos + i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: "__isQuote", | ||
value: function __isQuote(buf, pos) { | ||
var quote = this.options.quote; | ||
if (quote === null) return false; | ||
var l = quote.length; | ||
for (var i = 0; i < l; i++) { | ||
if (quote[i] !== buf[pos + i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
}, { | ||
key: "__autoDiscoverRowDelimiter", | ||
value: function __autoDiscoverRowDelimiter(buf, pos) { | ||
var encoding = this.options.encoding; | ||
var chr = buf[pos]; | ||
@@ -1239,7 +1322,7 @@ | ||
if (buf[pos + 1] === nl) { | ||
this.options.record_delimiter.push(Buffer.from('\r\n')); | ||
this.options.record_delimiter.push(Buffer.from('\r\n', encoding)); | ||
this.state.recordDelimiterMaxLength = 2; | ||
return 2; | ||
} else { | ||
this.options.record_delimiter.push(Buffer.from('\r')); | ||
this.options.record_delimiter.push(Buffer.from('\r', encoding)); | ||
this.state.recordDelimiterMaxLength = 1; | ||
@@ -1249,3 +1332,3 @@ return 1; | ||
} else if (chr === nl) { | ||
this.options.record_delimiter.push(Buffer.from('\n')); | ||
this.options.record_delimiter.push(Buffer.from('\n', encoding)); | ||
this.state.recordDelimiterMaxLength = 1; | ||
@@ -1307,3 +1390,3 @@ return 1; | ||
} else { | ||
throw new CsvError('CSV_INVALID_ARGUMENT', ['Invalid argument:', "got ".concat(JSON.stringify(argument), " at index ").concat(i)]); | ||
throw new CsvError('CSV_INVALID_ARGUMENT', ['Invalid argument:', "got ".concat(JSON.stringify(argument), " at index ").concat(i)], this.options); | ||
} | ||
@@ -1356,3 +1439,3 @@ } | ||
function CsvError(code, message) { | ||
function CsvError(code, message, options) { | ||
var _this2; | ||
@@ -1371,4 +1454,4 @@ | ||
for (var _len = arguments.length, contexts = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
contexts[_key - 2] = arguments[_key]; | ||
for (var _len = arguments.length, contexts = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { | ||
contexts[_key - 3] = arguments[_key]; | ||
} | ||
@@ -1381,3 +1464,3 @@ | ||
var value = context[key]; | ||
_this2[key] = Buffer.isBuffer(value) ? value.toString() : value == null ? value : JSON.parse(JSON.stringify(value)); | ||
_this2[key] = Buffer.isBuffer(value) ? value.toString(options.encoding) : value == null ? value : JSON.parse(JSON.stringify(value)); | ||
} | ||
@@ -1384,0 +1467,0 @@ } |
@@ -23,11 +23,31 @@ "use strict"; | ||
value: function prepend(val) { | ||
var length = this.length++; | ||
if (Buffer.isBuffer(val)) { | ||
var length = this.length + val.length; | ||
if (length === this.size) { | ||
this.resize(); | ||
if (length >= this.size) { | ||
this.resize(); | ||
if (length >= this.size) { | ||
throw Error('INVALID_BUFFER_STATE'); | ||
} | ||
} | ||
var buf = this.buf; | ||
this.buf = Buffer.alloc(this.size); | ||
val.copy(this.buf, 0); | ||
buf.copy(this.buf, val.length); | ||
this.length += val.length; | ||
} else { | ||
var _length = this.length++; | ||
if (_length === this.size) { | ||
this.resize(); | ||
} | ||
var _buf = this.clone(); | ||
this.buf[0] = val; | ||
_buf.copy(this.buf, 1, 0, _length); | ||
} | ||
var buf = this.clone(); | ||
this.buf[0] = val; | ||
buf.copy(this.buf, 1, 0, length); | ||
} | ||
@@ -61,4 +81,6 @@ }, { | ||
key: "toString", | ||
value: function toString() { | ||
return this.buf.slice(0, this.length).toString(); | ||
value: function toString(encoding) { | ||
// console.log('!!', encoding) | ||
// return this.buf.slice(0, this.length) | ||
return this.buf.slice(0, this.length).toString(encoding); | ||
} | ||
@@ -68,3 +90,3 @@ }, { | ||
value: function toJSON() { | ||
return this.toString(); | ||
return this.toString('utf8'); | ||
} | ||
@@ -71,0 +93,0 @@ }, { |
@@ -98,2 +98,6 @@ // Original definitions in https://github.com/DefinitelyTyped/DefinitelyTyped by: David Muller <https://github.com/davidm77> | ||
/** | ||
* Set the source and destination encoding, a value of `null` returns buffer instead of strings. | ||
*/ | ||
encoding?: string | null; | ||
/** | ||
* Set the escape character, one character only, defaults to double quotes. | ||
@@ -233,3 +237,3 @@ */ | ||
constructor(code: CsvErrorCode, message: string | string[], ...contexts: any[]); | ||
constructor(code: CsvErrorCode, message: string | string[], options?: Options, ...contexts: any[]); | ||
} | ||
@@ -236,0 +240,0 @@ |
238
lib/index.js
@@ -17,8 +17,26 @@ | ||
const space = 32 | ||
const bom_utf8 = Buffer.from([239, 187, 191]) | ||
// const bom_utf8 = Buffer.from([239, 187, 191]) | ||
const boms = { | ||
// Note, the following are equals: | ||
// Buffer.from("\ufeff") | ||
// Buffer.from([239, 187, 191]) | ||
// Buffer.from('EFBBBF', 'hex') | ||
'utf8': Buffer.from([239, 187, 191]), | ||
// Note, the following are equals: | ||
// Buffer.from "\ufeff", 'utf16le | ||
// Buffer.from([255, 254]) | ||
'utf16le': Buffer.from([255, 254]) | ||
} | ||
class Parser extends Transform { | ||
constructor(opts = {}){ | ||
super({...{readableObjectMode: true}, ...opts}) | ||
// const encoding = opts.encoding | ||
// delete opts.encoding | ||
super({...{readableObjectMode: true}, ...opts, encoding: null}) | ||
this.__originalOptions = opts | ||
this.__normalizeOptions(opts) | ||
} | ||
__normalizeOptions(opts){ | ||
const options = {} | ||
// const options = {encoding: encoding} | ||
// Merge with user options | ||
@@ -28,2 +46,14 @@ for(let opt in opts){ | ||
} | ||
// Normalize option `encoding` | ||
// Note: defined first because other options depends on it | ||
// to convert chars/strings into buffers. | ||
if(options.encoding === undefined){ | ||
options.encoding = 'utf8' | ||
}else if(typeof options.encoding !== 'string' && options.encoding !== null){ | ||
throw new CsvError('CSV_INVALID_OPTION_ENCODING', [ | ||
'Invalid option encoding:', | ||
'encoding must be a string or null to return a buffer,', | ||
`got ${JSON.stringify(options.encoding)}` | ||
], options) | ||
} | ||
// Normalize option `bom` | ||
@@ -36,3 +66,3 @@ if(options.bom === undefined || options.bom === null || options.bom === false){ | ||
`got ${JSON.stringify(options.bom)}` | ||
]) | ||
], options) | ||
} | ||
@@ -50,3 +80,3 @@ // Normalize option `cast` | ||
`got ${JSON.stringify(options.cast)}` | ||
]) | ||
], options) | ||
} | ||
@@ -65,3 +95,3 @@ // Normalize option `cast_date` | ||
`got ${JSON.stringify(options.cast_date)}` | ||
]) | ||
], options) | ||
} | ||
@@ -85,3 +115,3 @@ // Normalize option `columns` | ||
`got ${JSON.stringify(options.columns)}` | ||
]) | ||
], options) | ||
} | ||
@@ -96,3 +126,3 @@ // Normalize option `columns_duplicates_to_array` | ||
`got ${JSON.stringify(options.columns_duplicates_to_array)}` | ||
]) | ||
], options) | ||
} | ||
@@ -104,3 +134,3 @@ // Normalize option `comment` | ||
if(typeof options.comment === 'string'){ | ||
options.comment = Buffer.from(options.comment) | ||
options.comment = Buffer.from(options.comment, options.encoding) | ||
} | ||
@@ -112,3 +142,3 @@ if(!Buffer.isBuffer(options.comment)){ | ||
`got ${JSON.stringify(options.comment)}` | ||
]) | ||
], options) | ||
} | ||
@@ -124,10 +154,10 @@ } | ||
`got ${delimiter_json}` | ||
]) | ||
], options) | ||
} | ||
options.delimiter = options.delimiter.map(function(delimiter){ | ||
if(delimiter === undefined || delimiter === null || delimiter === false){ | ||
return Buffer.from(',') | ||
return Buffer.from(',', options.encoding) | ||
} | ||
if(typeof delimiter === 'string'){ | ||
delimiter = Buffer.from(delimiter) | ||
delimiter = Buffer.from(delimiter, options.encoding) | ||
} | ||
@@ -139,3 +169,3 @@ if( !Buffer.isBuffer(delimiter) || delimiter.length === 0){ | ||
`got ${delimiter_json}` | ||
]) | ||
], options) | ||
} | ||
@@ -146,5 +176,5 @@ return delimiter | ||
if(options.escape === undefined || options.escape === true){ | ||
options.escape = Buffer.from('"') | ||
options.escape = Buffer.from('"', options.encoding) | ||
}else if(typeof options.escape === 'string'){ | ||
options.escape = Buffer.from(options.escape) | ||
options.escape = Buffer.from(options.escape, options.encoding) | ||
}else if (options.escape === null || options.escape === false){ | ||
@@ -156,6 +186,6 @@ options.escape = null | ||
throw new Error(`Invalid Option: escape must be a buffer, a string or a boolean, got ${JSON.stringify(options.escape)}`) | ||
}else if(options.escape.length !== 1){ | ||
throw new Error(`Invalid Option Length: escape must be one character, got ${options.escape.length}`) | ||
}else{ | ||
options.escape = options.escape[0] | ||
// }else if(options.escape.length !== 1){ | ||
// throw new Error(`Invalid Option Length: escape must be one character, got ${options.escape.length}`) | ||
// }else{ | ||
// options.escape = options.escape[0] | ||
} | ||
@@ -216,3 +246,7 @@ } | ||
} | ||
options.objname = options.objname.toString() | ||
if(options.encoding === null){ | ||
// Don't call `toString`, leave objname as a buffer | ||
}else{ | ||
options.objname = options.objname.toString(options.encoding) | ||
} | ||
}else if(typeof options.objname === 'string'){ | ||
@@ -234,3 +268,3 @@ if(options.objname.length === 0){ | ||
`got ${JSON.stringify(options.on_record)}` | ||
]) | ||
], options) | ||
} | ||
@@ -242,12 +276,12 @@ // Normalize option `quote` | ||
if(options.quote === undefined || options.quote === true){ | ||
options.quote = Buffer.from('"') | ||
options.quote = Buffer.from('"', options.encoding) | ||
}else if(typeof options.quote === 'string'){ | ||
options.quote = Buffer.from(options.quote) | ||
options.quote = Buffer.from(options.quote, options.encoding) | ||
} | ||
if(!Buffer.isBuffer(options.quote)){ | ||
throw new Error(`Invalid Option: quote must be a buffer or a string, got ${JSON.stringify(options.quote)}`) | ||
}else if(options.quote.length !== 1){ | ||
throw new Error(`Invalid Option Length: quote must be one character, got ${options.quote.length}`) | ||
}else{ | ||
options.quote = options.quote[0] | ||
// }else if(options.quote.length !== 1){ | ||
// throw new Error(`Invalid Option Length: quote must be one character, got ${options.quote.length}`) | ||
// }else{ | ||
// options.quote = options.quote[0] | ||
} | ||
@@ -269,3 +303,3 @@ } | ||
if(typeof rd === 'string'){ | ||
rd = Buffer.from(rd) | ||
rd = Buffer.from(rd, options.encoding) | ||
} | ||
@@ -401,3 +435,4 @@ return rd | ||
escaping: false, | ||
escapeIsQuote: options.escape === options.quote, | ||
// escapeIsQuote: options.escape === options.quote, | ||
escapeIsQuote: Buffer.isBuffer(options.escape) && Buffer.isBuffer(options.quote) && Buffer.compare(options.escape, options.quote) === 0, | ||
expectedRecordLength: options.columns === null ? 0 : options.columns.length, | ||
@@ -407,2 +442,10 @@ field: new ResizeableBuffer(20), | ||
info: Object.assign({}, this.info), | ||
needMoreDataSize: Math.max( | ||
// Skip if the remaining buffer smaller than comment | ||
options.comment !== null ? options.comment.length : 0, | ||
// Skip if the remaining buffer can be delimiter | ||
...options.delimiter.map( (delimiter) => delimiter.length), | ||
// Skip if the remaining buffer can be escape sequence | ||
options.quote !== null ? options.quote.length : 0, | ||
), | ||
previousBuf: undefined, | ||
@@ -416,3 +459,3 @@ quoting: false, | ||
recordDelimiterMaxLength: options.record_delimiter.length === 0 ? 2 : Math.max(...options.record_delimiter.map( (v) => v.length)), | ||
trimChars: [Buffer.from(' ')[0], Buffer.from('\t')[0]], | ||
trimChars: [Buffer.from(' ', options.encoding)[0], Buffer.from('\t', options.encoding)[0]], | ||
wasQuoting: false, | ||
@@ -471,7 +514,11 @@ wasRowDelimiter: false | ||
} | ||
// skip BOM detect because data length < 3 | ||
}else{ | ||
if(bom_utf8.compare(buf, 0, 3) === 0){ | ||
// Skip BOM | ||
buf = buf.slice(3) | ||
for(let encoding in boms){ | ||
if(boms[encoding].compare(buf, 0, boms[encoding].length) === 0){ | ||
// Skip BOM | ||
buf = buf.slice(boms[encoding].length) | ||
// Renormalize original options with the new encoding | ||
this.__normalizeOptions({...this.__originalOptions, encoding: encoding}) | ||
break | ||
} | ||
} | ||
@@ -522,6 +569,8 @@ this.state.bomSkipped = true | ||
// We are quoting, the char is an escape chr and there is a chr to escape | ||
if(escape !== null && this.state.quoting === true && chr === escape && pos + 1 < bufLen){ | ||
// if(escape !== null && this.state.quoting === true && chr === escape && pos + 1 < bufLen){ | ||
if(escape !== null && this.state.quoting === true && this.__isEscape(buf, pos, chr) && pos + escape.length < bufLen){ | ||
if(escapeIsQuote){ | ||
if(buf[pos+1] === quote){ | ||
if(this.__isQuote(buf, pos+escape.length)){ | ||
this.state.escaping = true | ||
pos += escape.length - 1 | ||
continue | ||
@@ -531,2 +580,3 @@ } | ||
this.state.escaping = true | ||
pos += escape.length - 1 | ||
continue | ||
@@ -537,18 +587,21 @@ } | ||
// TODO: need to compare bytes instead of single char | ||
if(this.state.commenting === false && chr === quote){ | ||
// if(this.state.commenting === false && chr === quote){ | ||
if(this.state.commenting === false && this.__isQuote(buf, pos)){ | ||
if(this.state.quoting === true){ | ||
const nextChr = buf[pos+1] | ||
const nextChr = buf[pos+quote.length] | ||
const isNextChrTrimable = rtrim && this.__isCharTrimable(nextChr) | ||
// const isNextChrComment = nextChr === comment | ||
const isNextChrComment = comment !== null && this.__compareBytes(comment, buf, pos+1, nextChr) | ||
const isNextChrDelimiter = this.__isDelimiter(nextChr, buf, pos+1) | ||
const isNextChrRowDelimiter = record_delimiter.length === 0 ? this.__autoDiscoverRowDelimiter(buf, pos+1) : this.__isRecordDelimiter(nextChr, buf, pos+1) | ||
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) | ||
// Escape a quote | ||
// Treat next char as a regular character | ||
// TODO: need to compare bytes instead of single char | ||
if(escape !== null && chr === escape && nextChr === quote){ | ||
pos++ | ||
// if(escape !== null && chr === escape && nextChr === quote){ | ||
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){ | ||
this.state.quoting = false | ||
this.state.wasQuoting = true | ||
pos += quote.length - 1 | ||
continue | ||
@@ -563,3 +616,3 @@ }else if(relax === false){ | ||
'(if activated) or comment', | ||
], this.__context()) | ||
], this.options, this.__context()) | ||
) | ||
@@ -570,4 +623,4 @@ if(err !== undefined) return err | ||
this.state.wasQuoting = true | ||
// continue | ||
this.state.field.prepend(quote) | ||
pos += quote.length - 1 | ||
} | ||
@@ -582,3 +635,3 @@ }else{ | ||
`a quote is found inside a field at line ${this.info.lines}`, | ||
], this.__context(), { | ||
], this.options, this.__context(), { | ||
field: this.state.field, | ||
@@ -591,2 +644,3 @@ }) | ||
this.state.quoting = true | ||
pos += quote.length - 1 | ||
continue | ||
@@ -642,3 +696,3 @@ } | ||
} | ||
let delimiterLength = this.__isDelimiter(chr, buf, pos) | ||
let delimiterLength = this.__isDelimiter(buf, pos, chr) | ||
if(delimiterLength !== 0){ | ||
@@ -660,3 +714,3 @@ const errField = this.__onField() | ||
`at line ${this.info.lines}`, | ||
], this.__context()) | ||
], this.options, this.__context()) | ||
) | ||
@@ -678,3 +732,3 @@ if(err !== undefined) return err | ||
`at line ${this.info.lines}`, | ||
], this.__context()) | ||
], this.options, this.__context()) | ||
) | ||
@@ -691,3 +745,3 @@ if(err !== undefined) return err | ||
`the parsing is finished with an opening quote at line ${this.info.lines}`, | ||
], this.__context()) | ||
], this.options, this.__context()) | ||
) | ||
@@ -721,3 +775,3 @@ if(err !== undefined) return err | ||
__onRow(){ | ||
const {columns, columns_duplicates_to_array, info, from, relax_column_count, relax_column_count_less, relax_column_count_more, raw, skip_lines_with_empty_values} = this.options | ||
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 | ||
@@ -751,3 +805,3 @@ if(enabled === false){ | ||
`got ${recordLength} on line ${this.info.lines}`, | ||
], this.__context(), { | ||
], this.options, this.__context(), { | ||
record: record, | ||
@@ -764,3 +818,3 @@ }) | ||
`got ${recordLength} on line ${this.info.lines}`, | ||
], this.__context(), { | ||
], this.options, this.__context(), { | ||
record: record, | ||
@@ -808,3 +862,3 @@ }) | ||
{record: obj}, | ||
(raw === true ? {raw: this.state.rawBuffer.toString()}: {}), | ||
(raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), | ||
(info === true ? {info: this.state.info}: {}) | ||
@@ -825,3 +879,3 @@ )) | ||
{record: [obj[objname], obj]}, | ||
raw === true ? {raw: this.state.rawBuffer.toString()}: {}, | ||
raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, | ||
info === true ? {info: this.state.info}: {} | ||
@@ -843,3 +897,3 @@ )) | ||
{record: record}, | ||
raw === true ? {raw: this.state.rawBuffer.toString()}: {}, | ||
raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, | ||
info === true ? {info: this.state.info}: {} | ||
@@ -870,3 +924,3 @@ )) | ||
`got ${JSON.stringify(headers)}` | ||
], this.__context(), { | ||
], this.options, this.__context(), { | ||
headers: headers, | ||
@@ -893,3 +947,3 @@ }) | ||
__onField(){ | ||
const {cast, rtrim, max_record_size} = this.options | ||
const {cast, encoding, rtrim, max_record_size} = this.options | ||
const {enabled, wasQuoting} = this.state | ||
@@ -900,3 +954,3 @@ // Short circuit for the from_line options | ||
} | ||
let field = this.state.field.toString() | ||
let field = this.state.field.toString(encoding) | ||
if(rtrim === true && wasQuoting === false){ | ||
@@ -968,7 +1022,7 @@ field = field.trimRight() | ||
} | ||
__compareBytes(sourceBuf, targetBuf, pos, firtByte){ | ||
if(sourceBuf[0] !== firtByte) return 0 | ||
__compareBytes(sourceBuf, targetBuf, targetPos, firstByte){ | ||
if(sourceBuf[0] !== firstByte) return 0 | ||
const sourceLength = sourceBuf.length | ||
for(let i = 1; i < sourceLength; i++){ | ||
if(sourceBuf[i] !== targetBuf[pos+i]) return 0 | ||
if(sourceBuf[i] !== targetBuf[targetPos+i]) return 0 | ||
} | ||
@@ -978,25 +1032,17 @@ return sourceLength | ||
__needMoreData(i, bufLen, end){ | ||
if(end){ | ||
return false | ||
} | ||
const {comment, delimiter} = this.options | ||
const {quoting, recordDelimiterMaxLength} = this.state | ||
if(end) return false | ||
const {quote} = this.options | ||
const {quoting, needMoreDataSize, recordDelimiterMaxLength} = this.state | ||
const numOfCharLeft = bufLen - i - 1 | ||
const requiredLength = Math.max( | ||
// Skip if the remaining buffer smaller than comment | ||
comment ? comment.length : 0, | ||
// Skip if the remaining buffer smaller than row delimiter | ||
needMoreDataSize, | ||
// Skip if the remaining buffer smaller than record delimiter | ||
recordDelimiterMaxLength, | ||
// Skip if the remaining buffer can be row delimiter following the closing quote | ||
// 1 is for quote.length | ||
quoting ? (1 + recordDelimiterMaxLength) : 0, | ||
// Skip if the remaining buffer can be delimiter | ||
delimiter.length, | ||
// Skip if the remaining buffer can be escape sequence | ||
// 1 is for escape.length | ||
1 | ||
quoting ? (quote.length + recordDelimiterMaxLength) : 0, | ||
) | ||
return numOfCharLeft < requiredLength | ||
} | ||
__isDelimiter(chr, buf, pos){ | ||
__isDelimiter(buf, pos, chr){ | ||
const {delimiter} = this.options | ||
@@ -1032,11 +1078,37 @@ loop1: for(let i = 0; i < delimiter.length; i++){ | ||
} | ||
__isEscape(buf, pos, chr){ | ||
const {escape} = this.options | ||
if(escape === null) return false | ||
const l = escape.length | ||
if(escape[0] === chr){ | ||
for(let i = 0; i < l; i++){ | ||
if(escape[i] !== buf[pos+i]){ | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
return false | ||
} | ||
__isQuote(buf, pos){ | ||
const {quote} = this.options | ||
if(quote === null) return false | ||
const l = quote.length | ||
for(let i = 0; i < l; i++){ | ||
if(quote[i] !== buf[pos+i]){ | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
__autoDiscoverRowDelimiter(buf, pos){ | ||
const {encoding} = this.options | ||
const chr = buf[pos] | ||
if(chr === cr){ | ||
if(buf[pos+1] === nl){ | ||
this.options.record_delimiter.push(Buffer.from('\r\n')) | ||
this.options.record_delimiter.push(Buffer.from('\r\n', encoding)) | ||
this.state.recordDelimiterMaxLength = 2 | ||
return 2 | ||
}else{ | ||
this.options.record_delimiter.push(Buffer.from('\r')) | ||
this.options.record_delimiter.push(Buffer.from('\r', encoding)) | ||
this.state.recordDelimiterMaxLength = 1 | ||
@@ -1046,3 +1118,3 @@ return 1 | ||
}else if(chr === nl){ | ||
this.options.record_delimiter.push(Buffer.from('\n')) | ||
this.options.record_delimiter.push(Buffer.from('\n', encoding)) | ||
this.state.recordDelimiterMaxLength = 1 | ||
@@ -1100,3 +1172,3 @@ return 1 | ||
`got ${JSON.stringify(argument)} at index ${i}` | ||
]) | ||
], this.options) | ||
} | ||
@@ -1140,3 +1212,3 @@ } | ||
class CsvError extends Error { | ||
constructor(code, message, ...contexts) { | ||
constructor(code, message, options, ...contexts) { | ||
if(Array.isArray(message)) message = message.join(' ') | ||
@@ -1151,3 +1223,3 @@ super(message) | ||
const value = context[key] | ||
this[key] = Buffer.isBuffer(value) ? value.toString() : value == null ? value : JSON.parse(JSON.stringify(value)) | ||
this[key] = Buffer.isBuffer(value) ? value.toString(options.encoding) : value == null ? value : JSON.parse(JSON.stringify(value)) | ||
} | ||
@@ -1154,0 +1226,0 @@ } |
@@ -10,9 +10,24 @@ | ||
prepend(val){ | ||
const length = this.length++ | ||
if(length === this.size){ | ||
this.resize() | ||
if(Buffer.isBuffer(val)){ | ||
const length = this.length + val.length | ||
if(length >= this.size){ | ||
this.resize() | ||
if(length >= this.size){ | ||
throw Error('INVALID_BUFFER_STATE') | ||
} | ||
} | ||
const buf = this.buf | ||
this.buf = Buffer.alloc(this.size) | ||
val.copy(this.buf, 0) | ||
buf.copy(this.buf, val.length) | ||
this.length += val.length | ||
}else{ | ||
const length = this.length++ | ||
if(length === this.size){ | ||
this.resize() | ||
} | ||
const buf = this.clone() | ||
this.buf[0] = val | ||
buf.copy(this.buf,1, 0, length) | ||
} | ||
const buf = this.clone() | ||
this.buf[0] = val | ||
buf.copy(this.buf,1, 0, length) | ||
} | ||
@@ -36,7 +51,9 @@ append(val){ | ||
} | ||
toString(){ | ||
return this.buf.slice(0, this.length).toString() | ||
toString(encoding){ | ||
// console.log('!!', encoding) | ||
// return this.buf.slice(0, this.length) | ||
return this.buf.slice(0, this.length).toString(encoding) | ||
} | ||
toJSON(){ | ||
return this.toString() | ||
return this.toString('utf8') | ||
} | ||
@@ -43,0 +60,0 @@ reset(){ |
{ | ||
"version": "4.12.0", | ||
"version": "4.13.0", | ||
"name": "csv-parse", | ||
@@ -4,0 +4,0 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
158767
3092