UNPKG

@thi.ng/csv

Version:

Customizable, transducer-based CSV parser/object mapper and transformer

105 lines 3.66 kB
import type { Transducer } from "@thi.ng/transducers"; import type { CSVOpts, CSVRecord, CSVRow, SimpleCSVOpts } from "./api.js"; /** * Configurable CSV parsing transducer, operating on line-based string iterables * and yielding tuple objects of CSV row records. If called with input, returns * ES6 iterator instead. * * @remarks * Parsing behavior can be customized via given {@link CSVOpts}. The default * behavior is: * * - comma delimiter * - field names are obtained from first line/row * - all columns are processed, but no coercions * - untrimmed cell values * - line comment prefix `#` * * Using the `cols` option, specific columns can be renamed and their values * coerced/transformed. Additionally, if `all` option is `false`, then the * result objects will only contain values of the columns specified in `cols`. * * Also see: * * - thi.ng/transducers * - {@link CSVOpts} * - {@link parseCSVFromString}. * * @example * ```ts tangle:../export/parse-csv.ts * import { parseCSV, upper, float } from "@thi.ng/csv"; * * console.log( * [...parseCSV( * { * all: false, * cols: { * "country": { tx: upper }, * "latitude": { alias: "lat", tx: float() }, * "longitude": { alias: "lon", tx: float() }, * } * }, * [ * `"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 } * // ] * ``` * * @param opts - */ export declare function parseCSV(opts?: Partial<CSVOpts>): Transducer<string, CSVRecord>; export declare function parseCSV(opts: Partial<CSVOpts>, src: Iterable<string>): IterableIterator<CSVRecord>; /** * Simplified version of {@link parseCSV} for use cases when no object mapping * is desired/required. Here, each CSV row will be emitted as simple array, * optionally with only filtered or transformed columns. * * @remarks * See {@link SimpleCSVOpts} for available options. Defaults are similar to * those used by {@link parseCSV}. * * @example * ```ts tangle:../export/parse-csv-simple.ts * import { parseCSVSimple, float } from "@thi.ng/csv"; * * console.log( * [...parseCSVSimple( * { cols: [float(), null,float()]}, * ["a,b,c","1,2,3","4,5,6"]) * ] * ); * // [ [ 1, 3 ], [ 4, 6 ] ] * ``` * * @param opts - */ export declare function parseCSVSimple(opts?: Partial<SimpleCSVOpts>): Transducer<string, CSVRow>; export declare function parseCSVSimple(opts: Partial<SimpleCSVOpts>, src: Iterable<string>): IterableIterator<CSVRow>; /** * Syntax sugar for iterator version of {@link parseCSV}, efficiently splitting * given source string into a line based input using * [`split`](https://docs.thi.ng/umbrella/strings/functions/split.html). * * @param opts - * @param src - */ export declare const parseCSVFromString: (opts: Partial<CSVOpts>, src: string) => IterableIterator<CSVRecord>; /** * Syntax sugar for iterator version of {@link parseCSVSimple}, efficiently * splitting given source string into a line based input using * [`split`](https://docs.thi.ng/umbrella/strings/functions/split.html). * * @param opts - * @param src - */ export declare const parseCSVSimpleFromString: (opts: Partial<SimpleCSVOpts>, src: string) => IterableIterator<CSVRow>; //# sourceMappingURL=parse.d.ts.map