UNPKG

csv-parse

Version:

CSV parsing implementing the Node.js `stream.Transform` API

23 lines (20 loc) 678 B
import { CsvError, transform } from "./api/index.js"; import { normalize_options } from "./api/normalize_options.js"; const parse = function (data, opts = {}) { if (typeof data === "string") { data = Buffer.from(data); } const records = opts && opts.objname ? Object.create(null) : []; const parser = transform(opts); const push = (record) => { if (parser.options.objname === undefined) records.push(record); else { records[record[0]] = record[1]; } }; const close = () => {}; const error = parser.parse(data, true, push, close); if (error !== undefined) throw error; return records; }; export { parse, CsvError, normalize_options };