(options, state, info)
| 20 | }; |
| 21 | |
| 22 | const stringifier = function (options, state, info) { |
| 23 | return { |
| 24 | options: options, |
| 25 | state: state, |
| 26 | info: info, |
| 27 | __transform: function (chunk, push) { |
| 28 | // Chunk validation |
| 29 | if (!Array.isArray(chunk) && typeof chunk !== "object") { |
| 30 | return Error( |
| 31 | `Invalid Record: expect an array or an object, got ${JSON.stringify(chunk)}`, |
| 32 | ); |
| 33 | } |
| 34 | // Detect columns from the first record |
| 35 | if (this.info.records === 0) { |
| 36 | if (Array.isArray(chunk)) { |
| 37 | if ( |
| 38 | this.options.header === true && |
| 39 | this.options.columns === undefined |
| 40 | ) { |
| 41 | return Error( |
| 42 | "Undiscoverable Columns: header option requires column option or object records", |
| 43 | ); |
| 44 | } |
| 45 | } else if (this.options.columns === undefined) { |
| 46 | const [err, columns] = normalize_columns(Object.keys(chunk)); |
| 47 | if (err) return; |
| 48 | this.options.columns = columns; |
| 49 | } |
| 50 | } |
| 51 | // Emit the header |
| 52 | if (this.info.records === 0) { |
| 53 | this.bom(push); |
| 54 | const err = this.headers(push); |
| 55 | if (err) return err; |
| 56 | } |
| 57 | // Emit and stringify the record if an object or an array |
| 58 | try { |
| 59 | // this.emit('record', chunk, this.info.records); |
| 60 | if (this.options.on_record) { |
| 61 | this.options.on_record(chunk, this.info.records); |
| 62 | } |
| 63 | } catch (err) { |
| 64 | return err; |
| 65 | } |
| 66 | // Convert the record into a string |
| 67 | let err, chunk_string; |
| 68 | if (this.options.eof) { |
| 69 | [err, chunk_string] = this.stringify(chunk); |
| 70 | if (err) return err; |
| 71 | if (chunk_string === undefined) { |
| 72 | return; |
| 73 | } else { |
| 74 | chunk_string = chunk_string + this.options.record_delimiter; |
| 75 | } |
| 76 | } else { |
| 77 | [err, chunk_string] = this.stringify(chunk); |
| 78 | if (err) return err; |
| 79 | if (chunk_string === undefined) { |
no test coverage detected