UNPKG

@pujansrt/data-genie

Version:

High performant ETL engine written in TypeScript

46 lines (45 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TSVReader = void 0; const fs_1 = require("fs"); const csv_parse_1 = require("csv-parse"); /** * TSVReader class for reading data records from a Tab-Separated Values (TSV) file. * It uses the 'csv-parse' library, configured specifically for tab delimiters. */ class TSVReader { /** * Constructs a new TSVReader. * @param filePath The path to the TSV file. */ constructor(filePath) { this.hasFieldNamesInFirstRow = false; this.filePath = filePath; } /** * Specifies whether the first row of the TSV file contains field names (headers). * If true, the yielded records will be objects with these field names as keys. * If false, records will be arrays or objects with numerical keys (e.g., {0: 'value1', 1: 'value2'}). * @param value True if the first row is a header, false otherwise. * @returns The current TSVReader instance for chaining. */ setFieldNamesInFirstRow(value) { this.hasFieldNamesInFirstRow = value; return this; } /** * Reads data records from the TSV file asynchronously, yielding each record as it's parsed. * @returns An AsyncIterableIterator of DataRecord objects. */ async *read() { const parser = (0, fs_1.createReadStream)(this.filePath).pipe((0, csv_parse_1.parse)({ columns: this.hasFieldNamesInFirstRow, delimiter: '\t', // Fixed delimiter for TSV skip_empty_lines: true })); for await (const record of parser) { yield record; } } } exports.TSVReader = TSVReader;