| 77 | } |
| 78 | |
| 79 | const parse = function () { |
| 80 | let data, options, callback; |
| 81 | for (const i in arguments) { |
| 82 | const argument = arguments[i]; |
| 83 | const type = typeof argument; |
| 84 | if ( |
| 85 | data === undefined && |
| 86 | (typeof argument === "string" || Buffer.isBuffer(argument)) |
| 87 | ) { |
| 88 | data = argument; |
| 89 | } else if (options === undefined && is_object(argument)) { |
| 90 | options = argument; |
| 91 | } else if (callback === undefined && type === "function") { |
| 92 | callback = argument; |
| 93 | } else { |
| 94 | throw new CsvError( |
| 95 | "CSV_INVALID_ARGUMENT", |
| 96 | ["Invalid argument:", `got ${JSON.stringify(argument)} at index ${i}`], |
| 97 | options || {}, |
| 98 | ); |
| 99 | } |
| 100 | } |
| 101 | const parser = new Parser(options); |
| 102 | if (callback) { |
| 103 | const records = |
| 104 | options === undefined || options.objname === undefined |
| 105 | ? [] |
| 106 | : Object.create(null); |
| 107 | parser.on("readable", function () { |
| 108 | let record; |
| 109 | while ((record = this.read()) !== null) { |
| 110 | if (options === undefined || options.objname === undefined) { |
| 111 | records.push(record); |
| 112 | } else { |
| 113 | Object.assign(records, { |
| 114 | [record[0]]: record[1], |
| 115 | // writable: true, |
| 116 | // enumerable: true, |
| 117 | // configurable: true |
| 118 | }); |
| 119 | } |
| 120 | } |
| 121 | }); |
| 122 | parser.on("error", function (err) { |
| 123 | callback(err, undefined, parser.api.__infoDataSet()); |
| 124 | }); |
| 125 | parser.on("end", function () { |
| 126 | callback(undefined, records, parser.api.__infoDataSet()); |
| 127 | }); |
| 128 | } |
| 129 | if (data !== undefined) { |
| 130 | const writer = function () { |
| 131 | parser.write(data); |
| 132 | parser.end(); |
| 133 | }; |
| 134 | // Support Deno, Rollup doesn't provide a shim for setImmediate |
| 135 | if (typeof setImmediate === "function") { |
| 136 | setImmediate(writer); |