csv-stringify
Advanced tools
Comparing version 6.0.3 to 6.0.4
100
lib/index.js
@@ -252,3 +252,5 @@ | ||
// Normalize option `columns` | ||
options.columns = this.normalize_columns(options.columns); | ||
const [err, columns] = this.normalize_columns(options.columns); | ||
if(err) return err; | ||
options.columns = columns; | ||
// Normalize option `quoted` | ||
@@ -309,6 +311,25 @@ if(options.quoted === undefined || options.quoted === null){ | ||
} | ||
const err = this.__transform(chunk); | ||
if(err !== undefined){ | ||
this.state.stop = true; | ||
} | ||
callback(err); | ||
} | ||
_flush(callback){ | ||
if(this.state.stop === true){ | ||
// Note, Node.js 12 call flush even after an error, we must prevent | ||
// `callback` from being called in flush without any error. | ||
return; | ||
} | ||
if(this.info.records === 0){ | ||
this.bom(); | ||
const err = this.headers(); | ||
if(err) callback(err); | ||
} | ||
callback(); | ||
} | ||
__transform(chunk){ | ||
// Chunk validation | ||
if(!Array.isArray(chunk) && typeof chunk !== 'object'){ | ||
this.state.stop = true; | ||
return callback(Error(`Invalid Record: expect an array or an object, got ${JSON.stringify(chunk)}`)); | ||
return Error(`Invalid Record: expect an array or an object, got ${JSON.stringify(chunk)}`); | ||
} | ||
@@ -319,7 +340,8 @@ // Detect columns from the first record | ||
if(this.options.header === true && this.options.columns === undefined){ | ||
this.state.stop = true; | ||
return callback(Error('Undiscoverable Columns: header option requires column option or object records')); | ||
return Error('Undiscoverable Columns: header option requires column option or object records'); | ||
} | ||
}else if(this.options.columns === undefined){ | ||
this.options.columns = this.normalize_columns(Object.keys(chunk)); | ||
const [err, columns] = this.normalize_columns(Object.keys(chunk)); | ||
if(err) return; | ||
this.options.columns = columns; | ||
} | ||
@@ -330,3 +352,4 @@ } | ||
this.bom(); | ||
this.headers(); | ||
const err = this.headers(); | ||
if(err) return err; | ||
} | ||
@@ -337,9 +360,9 @@ // Emit and stringify the record if an object or an array | ||
}catch(err){ | ||
this.state.stop = true; | ||
return this.emit('error', err); | ||
return err; | ||
} | ||
// Convert the record into a string | ||
let chunk_string; | ||
let err, chunk_string; | ||
if(this.options.eof){ | ||
chunk_string = this.stringify(chunk); | ||
[err, chunk_string] = this.stringify(chunk); | ||
if(err) return err; | ||
if(chunk_string === undefined){ | ||
@@ -351,3 +374,4 @@ return; | ||
}else{ | ||
chunk_string = this.stringify(chunk); | ||
[err, chunk_string] = this.stringify(chunk); | ||
if(err) return err; | ||
if(chunk_string === undefined){ | ||
@@ -364,14 +388,6 @@ return; | ||
this.push(chunk_string); | ||
callback(); | ||
} | ||
_flush(callback){ | ||
if(this.info.records === 0){ | ||
this.bom(); | ||
this.headers(); | ||
} | ||
callback(); | ||
} | ||
stringify(chunk, chunkIsHeader=false){ | ||
if(typeof chunk !== 'object'){ | ||
return chunk; | ||
return [undefined, chunk]; | ||
} | ||
@@ -393,6 +409,3 @@ const {columns} = this.options; | ||
}); | ||
if(err){ | ||
this.emit('error', err); | ||
return; | ||
} | ||
if(err) return [err]; | ||
record[i] = [value, field]; | ||
@@ -408,6 +421,3 @@ } | ||
}); | ||
if(err){ | ||
this.emit('error', err); | ||
return; | ||
} | ||
if(err) return [err]; | ||
record[i] = [value, field]; | ||
@@ -424,3 +434,2 @@ } | ||
}else if(isObject(value)){ | ||
// let { value, ...options } = value | ||
options = value; | ||
@@ -430,9 +439,7 @@ value = options.value; | ||
if(typeof value !== "string" && value !== undefined && value !== null){ | ||
this.emit("error", Error(`Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`)); | ||
return; | ||
if(err) return [Error(`Invalid Casting Value: returned value must return a string, null or undefined, got ${JSON.stringify(value)}`)]; | ||
} | ||
options = {...this.options, ...options}; | ||
if((err = this.normalize(options)) !== undefined){ | ||
this.emit("error", err); | ||
return; | ||
return [err]; | ||
} | ||
@@ -442,4 +449,3 @@ }else if(value === undefined || value === null){ | ||
}else{ | ||
this.emit("error", Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)); | ||
return; | ||
return [Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`)]; | ||
} | ||
@@ -449,4 +455,3 @@ const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options; | ||
if(typeof value !== 'string'){ | ||
this.emit("error", Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)); | ||
return null; | ||
return [Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`)]; | ||
} | ||
@@ -488,3 +493,3 @@ const containsdelimiter = delimiter.length && value.indexOf(delimiter) >= 0; | ||
} | ||
return csvrecord; | ||
return [undefined, csvrecord]; | ||
} | ||
@@ -504,8 +509,11 @@ bom(){ | ||
} | ||
let err; | ||
let headers = this.options.columns.map(column => column.header); | ||
if(this.options.eof){ | ||
headers = this.stringify(headers, true) + this.options.record_delimiter; | ||
[err, headers] = this.stringify(headers, true); | ||
headers += this.options.record_delimiter; | ||
}else{ | ||
headers = this.stringify(headers); | ||
[err, headers] = this.stringify(headers); | ||
} | ||
if(err) return err; | ||
this.push(headers); | ||
@@ -537,6 +545,6 @@ } | ||
if(columns === undefined || columns === null){ | ||
return undefined; | ||
return []; | ||
} | ||
if(typeof columns !== 'object'){ | ||
throw Error('Invalid option "columns": expect an array or an object'); | ||
return [Error('Invalid option "columns": expect an array or an object')]; | ||
} | ||
@@ -562,3 +570,3 @@ if(!Array.isArray(columns)){ | ||
if(!column.key){ | ||
throw Error('Invalid column definition: property "key" is required'); | ||
return [Error('Invalid column definition: property "key" is required')]; | ||
} | ||
@@ -570,3 +578,3 @@ if(column.header === undefined){ | ||
}else{ | ||
throw Error('Invalid column definition: expect a string or an object'); | ||
return [Error('Invalid column definition: expect a string or an object')]; | ||
} | ||
@@ -576,3 +584,3 @@ } | ||
} | ||
return columns; | ||
return [undefined, columns]; | ||
} | ||
@@ -579,0 +587,0 @@ } |
import { Stringifier } from './index.js'; | ||
import { StringDecoder } from 'string_decoder'; | ||
const stringify = function(records, options={}){ | ||
const data = []; | ||
if(Buffer.isBuffer(records)){ | ||
const decoder = new StringDecoder(); | ||
records = decoder.write(records); | ||
} | ||
function onData(record){ | ||
if(record){ | ||
data.push(record.toString()); | ||
const stringifier = new Stringifier(options); | ||
stringifier.push = function(record){ | ||
if(record === null){ | ||
return; | ||
} | ||
} | ||
const stringifier = new Stringifier(options); | ||
stringifier.on('data', onData); | ||
data.push(record.toString()); | ||
}; | ||
for(const record of records){ | ||
stringifier.write(record); | ||
const err = stringifier.__transform(record, null); | ||
if(err !== undefined) throw err; | ||
} | ||
stringifier.end(); | ||
stringifier.removeListener('data', onData); | ||
return data.join(''); | ||
@@ -24,0 +18,0 @@ }; |
{ | ||
"version": "6.0.3", | ||
"version": "6.0.4", | ||
"name": "csv-stringify", | ||
@@ -89,3 +89,3 @@ "description": "CSV stringifier implementing the Node.js `stream.Transform` API", | ||
}, | ||
"gitHead": "e00242dd598b165bdfcf2d17a2b8cd63806df816" | ||
"gitHead": "8bdbe7ef8b4ff626fcd79d39b273a8bee7f278fb" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
1268576
30900