import { EventEmitter } from 'node:events'; import { Events } from './Events.interface'; import { RowMapper } from './mapper.interface'; export declare class SheetReader extends EventEmitter { /** * The path to the Excel or CSV file to be read. * @private * @property {string} path */ private path; /** * The encoding of the file. Defaults to 'utf-8' for CSV files. This property is used only when the file type is CSV. * @private * @property {BufferEncoding} encoding */ private encoding; /** * An optional array of strings representing the key names for the json object. This property is optional and if not provided, the rows will be emitted using the first row of the sheet as the keys for the json object. -- Is expected to be an array of arrays, where each inner array represents the headers for a specific sheet in the case of Excel files with multiple sheets. * @private * @property {Array>} headers */ private headers?; /** * A boolean indicating whether to include the first row of the sheet as part of the emitted data. If set to `true`, the first row will be included in the emitted data; if set to `false`, the first row will be treated as headers and will not be included in the emitted data. Defaults to `false`. * @private * @property {boolean} includeFirstRow */ private includeFirstRow; /** * The maximum number of data rows to read. If set, the reader will stop after emitting this many rows. Defaults to `undefined` (no limit). * @private * @property {number | undefined} maxRows */ private maxRows?; /** * Counter for emitted data rows. * @private */ private _emittedRows; /** * The type of the file being read, either 'EXCEL' or 'CSV'. This is determined based on the file extension of the provided path. * @private * @property {keyof typeof FILE_TYPES} fileType */ private fileType; /** * The read stream * @private * @property {ReadStream} reader - The read stream used to read the file. This stream is created based on the file type and is used to emit 'row' events as data is read from the file. */ private reader?; /** * A boolean indicating whether the reader is currently paused. This is used to control the flow of data when reading from the file, allowing the reader to be paused and resumed as needed. * @private * @property {boolean} _paused */ private _paused; /** * A function that resolves the promise used to pause the reader. This is set when the reader is paused and is called when the reader is resumed to allow the reading process to continue. * @private * @property {(() => void) | null} _resumeResolve */ private _resumeResolve; /** * An optional sheet name to filter which sheet is read in Excel files. If provided, only the sheet with this name will be emitted. * @private * @property {string | undefined} sheetName */ private sheetName?; /** * An optional function that takes a row object and returns a transformed version of that object. This allows you to modify the data as it's being read, such as changing property names, filtering out certain properties, or transforming values. * @private * @property {RowMapper[] | undefined} mappers */ private mappers?; /** * Validates the provided file path. Throws an error if the path is invalid or the file does not exist. * @param {string} path - The path to the file to be validated. * @throws {Error} Will throw an error if the file path is not provided or if the file does not exist. */ private validateFilePath; /** * Reads a CSV file line by line using the readline module. * @private */ private readCsv; /** * Parses a line from a CSV file into an array of fields. * @param line * @returns {string[]} An array of strings representing the fields in the CSV line. */ private parseCsvLine; /** * Reads an Excel file using the exceljs library. Emits a 'row' event for each row read from the sheet, providing the row data and the sheet name. * @private * */ private readExcel; /** * Maps the values of a row to the corresponding headers. * @param values - The values of the row. * @param headers - The headers to map the values to. * @returns An object representing the mapped row. */ private mapRow; /** * Retrieves the header for the current sheet. If headers were provided in the constructor, it returns the corresponding header for the current workbook number. Otherwise, it returns the default headers extracted from the first row of the sheet. * @param defaultHeaders * @param workBookNumber * @returns */ private getHeader; private getFileName; /** * * Initializes a new instance of the SheetReader class. * Validates the provided file path and sets up a read stream for the specified file. * * @param {object} params - An object containing the options. * @param {string} params.path - The path to the Excel or CSV file to be read. * @param {BufferEncoding} [params.encoding='utf-8'] - The encoding of the file. Defaults to 'utf-8' -- Is used only when file is CSV. * @param {Array>} [params.headers] - An optional array of strings representing the key names for the json object. This property is optional and if not provided, the rows will be emitted using the first row of the sheet as the keys for the json object. -- Is expected to be an array of arrays, where each inner array represents the headers for a specific sheet in the case of Excel files with multiple sheets. * @param {boolean} [params.includeFirstRow=false] - A boolean indicating whether to include the first row of the sheet as part of the emitted data. If set to `true`, the first row will be included in the emitted data; if set to `false`, the first row will be treated as headers and will not be included in the emitted data. Defaults to `false`. * @param {number} [params.maxRows] - The maximum number of data rows to read. If set, the reader will stop after emitting this many rows. * @param {RowMapper[]} [params.mappers] - An optional array of functions that take a row object and return a transformed version of that object. This allows you to modify the data as it's being read, such as changing property names, filtering out certain properties, or transforming values. Each function in the array corresponds to a sheet in the case of Excel files with multiple sheets. * @throws {Error} Will throw an error if the file path is not provided or if the file does not exist. */ constructor({ path, encoding, headers, includeFirstRow, maxRows, sheetName, mappers, }: { path: string; encoding?: BufferEncoding; headers?: Array>; includeFirstRow?: boolean; maxRows?: number; sheetName?: string; mappers?: RowMapper[]; }); /** * Adds listeners to the read stream for handling 'end' and 'error' events. * @private * @returns {Promise} A promise that resolves when the listeners have been added. */ start(): SheetReader; /** * Registers a listener that is called for each row read from the sheet. * * @param event - The `'row'` event. * @param listener - Receives the row data and the sheet name it belongs to. */ on(event: K, listener: Events[K]): this; /** * Checks if the reader is currently paused. * @returns {boolean} `true` if the reader is paused, otherwise `false`. * @public */ isPaused(): boolean; /** * Pauses the reading process. This can be used to temporarily stop reading from the file, for example, to manage backpressure or to perform some processing before resuming. * @public */ pause(): void; /** * Resumes the reading process if it was previously paused. This allows the reader to continue reading from the file after being paused. * @public */ resume(): void; /** * Destroys the reader and releases any resources associated with it. This should be called when you are done with the reader to ensure that all resources are properly cleaned up. * @public */ destroy(): void; }