This project is part of the @thi.ng/umbrella monorepo.
Customizable, transducer-based CSV parser/object mapper and transformer.
Partially ported and extended from the Clojure versions of the ws-ldn-1 and resonate-2014 workshop repos.
The parser supports customizable delimiters, quoted and unquoted cells, line breaks within quoted cells. Being transducer based, memory usage is kept at a minimum by only processing single lines of (pre-split) text. For CSV records spanning multiple lines (due to line breaks), new results are only emitted/processed downstream once a logical CSV row is complete.
Also, being transducer based means the parser can be easily integrated into more complex data preparation, cleaning or conversion workflows. See @thi.ng/transducers for more details/ideas.
STABLE - used in production
Search or submit any issues for this package
- Simple CSV row parsing w/o object mapping (
parseCSVSimple()
) - CSV output from structured data
- CSVW support (#257)
- Integration with thi.ng/egf
yarn add @thi.ng/csv
ES module import:
<script type="module" src="https://cdn.skypack.dev/@thi.ng/csv"></script>
For Node.js REPL:
# with flag only for < v16
node --experimental-repl-await
> const csv = await import("@thi.ng/csv");
Package sizes (gzipped, pre-treeshake): ESM: 1.53 KB
Also see extensive doc strings for parseCSV()
and CSVOpts
. See
parseCSVSimple()
and parseCSVFromString()
for alternatives and syntax sugar
forms.
import { parseCSV, upper, float } from "@thi.ng/csv";
[...parseCSV(
{
all: false,
cols: {
"country": { tx: upper },
"latitude": { alias: "lat", tx: float(0) },
"longitude": { alias: "lon", tx: float(0) },
}
},
[
`"country","country group","name (en)","latitude","longitude"`,
`"at","eu","Austria","47.6965545","13.34598005"`,
`"be","eu","Belgium","50.501045","4.47667405"`,
`"bg","eu","Bulgaria","42.72567375","25.4823218"`,
]
)]
// [
// { country: 'AT', lat: 47.6965545, lon: 13.34598005 },
// { country: 'BE', lat: 50.501045, lon: 4.47667405 },
// { country: 'BG', lat: 42.72567375, lon: 25.4823218 }
// ]
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-csv,
title = "@thi.ng/csv",
author = "Karsten Schmidt",
note = "https://thi.ng/csv",
year = 2014
}
© 2014 - 2021 Karsten Schmidt // Apache Software License 2.0