This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
/
index.js
627 lines (614 loc) · 19.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
CSV Stringify
Please look at the [project documentation](https://csv.js.org/stringify/) for
additional information.
*/
const { Transform } = require('stream')
const bom_utf8 = Buffer.from([239, 187, 191])
class Stringifier extends Transform {
constructor(opts = {}){
super({...{writableObjectMode: true}, ...opts})
const options = {}
let err
// Merge with user options
for(let opt in opts){
options[underscore(opt)] = opts[opt]
}
if(err = this.normalize(options)) throw err
switch(options.record_delimiter){
case 'auto':
options.record_delimiter = null
break
case 'unix':
options.record_delimiter = "\n"
break
case 'mac':
options.record_delimiter = "\r"
break
case 'windows':
options.record_delimiter = "\r\n"
break
case 'ascii':
options.record_delimiter = "\u001e"
break
case 'unicode':
options.record_delimiter = "\u2028"
break
}
// Expose options
this.options = options
// Internal state
this.state = {
stop: false
}
// Information
this.info = {
records: 0
}
if(options.bom === true){
this.push(bom_utf8)
}
this
}
normalize(options){
// Normalize option `bom`
if(options.bom === undefined || options.bom === null || options.bom === false){
options.bom = false
}else if(options.bom !== true){
return new CsvError('CSV_OPTION_BOOLEAN_INVALID_TYPE', [
'option `bom` is optional and must be a boolean value,',
`got ${JSON.stringify(options.bom)}`
])
}
// Normalize option `delimiter`
if(options.delimiter === undefined || options.delimiter === null){
options.delimiter = ','
}else if(Buffer.isBuffer(options.delimiter)){
options.delimiter = options.delimiter.toString()
}else if(typeof options.delimiter !== 'string'){
return new CsvError('CSV_OPTION_DELIMITER_INVALID_TYPE', [
'option `delimiter` must be a buffer or a string,',
`got ${JSON.stringify(options.delimiter)}`
])
}
// Normalize option `quote`
if(options.quote === undefined || options.quote === null){
options.quote = '"'
}else if(options.quote === true){
options.quote = '"'
}else if(options.quote === false){
options.quote = ''
}else if (Buffer.isBuffer(options.quote)){
options.quote = options.quote.toString()
}else if(typeof options.quote !== 'string'){
return new CsvError('CSV_OPTION_QUOTE_INVALID_TYPE', [
'option `quote` must be a boolean, a buffer or a string,',
`got ${JSON.stringify(options.quote)}`
])
}
// Normalize option `quoted`
if(options.quoted === undefined || options.quoted === null){
options.quoted = false
}else{
// todo
}
// Normalize option `quoted_empty`
if(options.quoted_empty === undefined || options.quoted_empty === null){
options.quoted_empty = undefined
}else{
// todo
}
// Normalize option `quoted_match`
if(options.quoted_match === undefined || options.quoted_match === null || options.quoted_match === false){
options.quoted_match = null
}else if(!Array.isArray(options.quoted_match)){
options.quoted_match = [options.quoted_match]
}
if(options.quoted_match){
for(let quoted_match of options.quoted_match){
const isString = typeof quoted_match === 'string'
const isRegExp = quoted_match instanceof RegExp
if(!isString && !isRegExp){
return Error(`Invalid Option: quoted_match must be a string or a regex, got ${JSON.stringify(quoted_match)}`)
}
}
}
// Normalize option `quoted_string`
if(options.quoted_string === undefined || options.quoted_string === null){
options.quoted_string = false
}else{
// todo
}
// Normalize option `eof`
if(options.eof === undefined || options.eof === null){
options.eof = true
}else{
// todo
}
// Normalize option `escape`
if(options.escape === undefined || options.escape === null){
options.escape = '"'
}else if(Buffer.isBuffer(options.escape)){
options.escape = options.escape.toString()
}else if(typeof options.escape !== 'string'){
return Error(`Invalid Option: escape must be a buffer or a string, got ${JSON.stringify(options.escape)}`)
}
if (options.escape.length > 1){
return Error(`Invalid Option: escape must be one character, got ${options.escape.length} characters`)
}
// Normalize option `header`
if(options.header === undefined || options.header === null){
options.header = false
}else{
// todo
}
// Normalize option `columns`
options.columns = this.normalize_columns(options.columns)
// Normalize option `quoted`
if(options.quoted === undefined || options.quoted === null){
options.quoted = false
}else{
// todo
}
// Normalize option `cast`
if(options.cast === undefined || options.cast === null){
options.cast = {}
}else{
// todo
}
// Normalize option cast.bigint
if(options.cast.bigint === undefined || options.cast.bigint === null){
// Cast boolean to string by default
options.cast.bigint = value => '' + value
}
// Normalize option cast.boolean
if(options.cast.boolean === undefined || options.cast.boolean === null){
// Cast boolean to string by default
options.cast.boolean = value => value ? '1' : ''
}
// Normalize option cast.date
if(options.cast.date === undefined || options.cast.date === null){
// Cast date to timestamp string by default
options.cast.date = value => '' + value.getTime()
}
// Normalize option cast.number
if(options.cast.number === undefined || options.cast.number === null){
// Cast number to string using native casting by default
options.cast.number = value => '' + value
}
// Normalize option cast.object
if(options.cast.object === undefined || options.cast.object === null){
// Stringify object as JSON by default
options.cast.object = value => JSON.stringify(value)
}
// Normalize option cast.string
if(options.cast.string === undefined || options.cast.string === null){
// Leave string untouched
options.cast.string = function(value){return value}
}
// Normalize option `record_delimiter`
if(options.record_delimiter === undefined || options.record_delimiter === null){
options.record_delimiter = '\n'
}else if(Buffer.isBuffer(options.record_delimiter)){
options.record_delimiter = options.record_delimiter.toString()
}else if(typeof options.record_delimiter !== 'string'){
return Error(`Invalid Option: record_delimiter must be a buffer or a string, got ${JSON.stringify(options.record_delimiter)}`)
}
}
_transform(chunk, encoding, callback){
if(this.state.stop === true){
return
}
// 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)}`))
}
// Detect columns from the first record
if(this.info.records === 0){
if(Array.isArray(chunk)){
if(this.options.header === true && !this.options.columns){
this.state.stop = true
return callback(Error('Undiscoverable Columns: header option requires column option or object records'))
}
}else if(this.options.columns === undefined || this.options.columns === null){
this.options.columns = this.normalize_columns(Object.keys(chunk))
}
}
// Emit the header
if(this.info.records === 0){
this.headers()
}
// Emit and stringify the record if an object or an array
try{
this.emit('record', chunk, this.info.records)
}catch(err){
this.state.stop = true
return this.emit('error', err)
}
// Convert the record into a string
if(this.options.eof){
chunk = this.stringify(chunk)
if(chunk === undefined){
return
}else{
chunk = chunk + this.options.record_delimiter
}
}else{
chunk = this.stringify(chunk)
if(chunk === undefined){
return
}else{
if(this.options.header || this.info.records){
chunk = this.options.record_delimiter + chunk
}
}
}
// Emit the csv
this.info.records++
this.push(chunk)
callback()
null
}
_flush(callback){
if(this.info.records === 0){
this.headers()
}
callback()
null
}
stringify(chunk, chunkIsHeader=false){
if(typeof chunk !== 'object'){
return chunk
}
const {columns, header} = this.options
const record = []
// Record is an array
if(Array.isArray(chunk)){
// We are getting an array but the user has specified output columns. In
// this case, we respect the columns indexes
if(columns){
chunk.splice(columns.length)
}
// Cast record elements
for(let i=0; i<chunk.length; i++){
const field = chunk[i]
const [err, value] = this.__cast(field, {
index: i, column: i, records: this.info.records, header: chunkIsHeader
})
if(err){
this.emit('error', err)
return
}
record[i] = [value, field]
}
// Record is a literal object
}else{
if(columns){
for(let i=0; i<columns.length; i++){
const field = get(chunk, columns[i].key)
const [err, value] = this.__cast(field, {
index: i, column: columns[i].key, records: this.info.records, header: chunkIsHeader
})
if(err){
this.emit('error', err)
return
}
record[i] = [value, field]
}
}else{
for(let column of chunk){
const field = chunk[column]
const [err, value] = this.__cast(field, {
index: i, column: columns[i].key, records: this.info.records, header: chunkIsHeader
})
if(err){
this.emit('error', err)
return
}
record.push([value, field])
}
}
}
let csvrecord = ''
for(let i=0; i<record.length; i++){
let options, err
let [value, field] = record[i]
if(typeof value === "string"){
options = this.options
}else if(isObject(value)){
// let { value, ...options } = value
options = value
value = options.value
delete 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
}
options = {...this.options, ...options}
if(err = this.normalize(options)){
this.emit("error", err)
return
}
}else if(value === undefined || value === null){
options = this.options
}else{
this.emit("error", Error(`Invalid Casting Value: returned value must return a string, an object, null or undefined, got ${JSON.stringify(value)}`))
return
}
const {delimiter, escape, quote, quoted, quoted_empty, quoted_string, quoted_match, record_delimiter} = options
if(value){
if(typeof value !== 'string'){
this.emit("error", Error(`Formatter must return a string, null or undefined, got ${JSON.stringify(value)}`))
return null
}
const containsdelimiter = delimiter.length && value.indexOf(delimiter) >= 0
const containsQuote = (quote !== '') && value.indexOf(quote) >= 0
const containsEscape = value.indexOf(escape) >= 0 && (escape !== quote)
const containsRecordDelimiter = value.indexOf(record_delimiter) >= 0
const quotedString = quoted_string && typeof field === 'string'
let quotedMatch = quoted_match && quoted_match.filter( quoted_match => {
if(typeof quoted_match === 'string'){
return value.indexOf(quoted_match) !== -1
}else{
return quoted_match.test(value)
}
})
quotedMatch = quotedMatch && quotedMatch.length > 0
const shouldQuote = containsQuote === true || containsdelimiter || containsRecordDelimiter || quoted || quotedString || quotedMatch
if(shouldQuote === true && containsEscape === true){
const regexp = escape === '\\'
? new RegExp(escape + escape, 'g')
: new RegExp(escape, 'g')
value = value.replace(regexp, escape + escape)
}
if(containsQuote === true){
const regexp = new RegExp(quote,'g')
value = value.replace(regexp, escape + quote)
}
if(shouldQuote === true){
value = quote + value + quote
}
csvrecord += value
}else if(quoted_empty === true || (field === '' && quoted_string === true && quoted_empty !== false)){
csvrecord += quote + quote
}
if(i !== record.length - 1){
csvrecord += delimiter
}
}
return csvrecord
}
headers(){
if(this.options.header === false){
return
}
if(this.options.columns === undefined){
return
}
let headers = this.options.columns.map(column => column.header)
if(this.options.eof){
headers = this.stringify(headers, true) + this.options.record_delimiter
}else{
headers = this.stringify(headers)
}
this.push(headers)
}
__cast(value, context){
const type = typeof value
try{
if(type === 'string'){ // Fine for 99% of the cases
return [undefined, this.options.cast.string(value, context)]
}else if(type === 'bigint'){
return [undefined, this.options.cast.bigint(value, context)]
}else if(type === 'number'){
return [undefined, this.options.cast.number(value, context)]
}else if(type === 'boolean'){
return [undefined, this.options.cast.boolean(value, context)]
}else if(value instanceof Date){
return [undefined, this.options.cast.date(value, context)]
}else if(type === 'object' && value !== null){
return [undefined, this.options.cast.object(value, context)]
}else{
return [undefined, value, value]
}
}catch(err){
return [err]
}
}
normalize_columns(columns){
if(columns === undefined || columns === null){
return undefined
}
if(typeof columns !== 'object'){
throw Error('Invalid option "columns": expect an array or an object')
}
if(!Array.isArray(columns)){
const newcolumns = []
for(let k in columns){
newcolumns.push({
key: k,
header: columns[k]
})
}
columns = newcolumns
}else{
const newcolumns = []
for(let column of columns){
if(typeof column === 'string'){
newcolumns.push({
key: column,
header: column
})
}else if(typeof column === 'object' && column !== undefined && !Array.isArray(column)){
if(!column.key){
throw Error('Invalid column definition: property "key" is required')
}
if(column.header === undefined){
column.header = column.key
}
newcolumns.push(column)
}else{
throw Error('Invalid column definition: expect a string or an object')
}
}
columns = newcolumns
}
return columns
}
}
const stringify = function(){
let data, options, callback
for(let i in arguments){
const argument = arguments[i]
const type = typeof argument
if(data === undefined && (Array.isArray(argument))){
data = argument
}else if(options === undefined && isObject(argument)){
options = argument
}else if(callback === undefined && type === 'function'){
callback = argument
}else{
throw new CsvError('CSV_INVALID_ARGUMENT', [
'Invalid argument:',
`got ${JSON.stringify(argument)} at index ${i}`
])
}
}
const stringifier = new Stringifier(options)
if(callback){
const chunks = []
stringifier.on('readable', function(){
let chunk
while((chunk = this.read()) !== null){
chunks.push(chunk)
}
})
stringifier.on('error', function(err){
callback(err)
})
stringifier.on('end', function(){
callback(undefined, chunks.join(''))
})
}
if(data !== undefined){
// Give a chance for events to be registered later
if(typeof setImmediate === 'function'){
setImmediate(function(){
for(let record of data){
stringifier.write(record)
}
stringifier.end()
})
}else{
for(let record of data){
stringifier.write(record)
}
stringifier.end()
}
}
return stringifier
}
class CsvError extends Error {
constructor(code, message, ...contexts) {
if(Array.isArray(message)) message = message.join(' ')
super(message)
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError)
}
this.code = code
for(const context of contexts){
for(const key in context){
const value = context[key]
this[key] = Buffer.isBuffer(value) ? value.toString() : value == null ? value : JSON.parse(JSON.stringify(value))
}
}
}
}
stringify.Stringifier = Stringifier
stringify.CsvError = CsvError
module.exports = stringify
const isObject = function(obj){
return typeof obj === 'object' && obj !== null && ! Array.isArray(obj)
}
const underscore = function(str){
return str.replace(/([A-Z])/g, function(_, match){
return '_' + match.toLowerCase()
})
}
// Lodash implementation of `get`
const charCodeOfDot = '.'.charCodeAt(0)
const reEscapeChar = /\\(\\)?/g
const rePropName = RegExp(
// Match anything that isn't a dot or bracket.
'[^.[\\]]+' + '|' +
// Or match property names within brackets.
'\\[(?:' +
// Match a non-string expression.
'([^"\'][^[]*)' + '|' +
// Or match strings (supports escaping characters).
'(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
')\\]'+ '|' +
// Or match "" as the space between consecutive dots or empty brackets.
'(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))'
, 'g')
const reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/
const reIsPlainProp = /^\w*$/
const getTag = function(value){
if(!value)
value === undefined ? '[object Undefined]' : '[object Null]'
return Object.prototype.toString.call(value)
}
const isKey = function(value, object){
if(Array.isArray(value)){
return false
}
const type = typeof value
if(type === 'number' || type === 'symbol' || type === 'boolean' || !value || isSymbol(value)){
return true
}
return reIsPlainProp.test(value) || !reIsDeepProp.test(value) ||
(object != null && value in Object(object))
}
const isSymbol = function(value){
const type = typeof value
return type === 'symbol' || (type === 'object' && value && getTag(value) === '[object Symbol]')
}
const stringToPath = function(string){
const result = []
if(string.charCodeAt(0) === charCodeOfDot){
result.push('')
}
string.replace(rePropName, function(match, expression, quote, subString){
let key = match
if(quote){
key = subString.replace(reEscapeChar, '$1')
}else if(expression){
key = expression.trim()
}
result.push(key)
})
return result
}
const castPath = function(value, object){
if(Array.isArray(value)){
return value
} else {
return isKey(value, object) ? [value] : stringToPath(value)
}
}
const toKey = function(value){
if(typeof value === 'string' || isSymbol(value))
return value
const result = `${value}`
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result
}
const get = function(object, path){
path = castPath(path, object)
let index = 0
const length = path.length
while(object != null && index < length){
object = object[toKey(path[index++])]
}
return (index && index === length) ? object : undefined
}