@pujansrt/data-genie
Version:
High performant ETL engine written in TypeScript
69 lines (68 loc) • 3.09 kB
TypeScript
import { DataWriter, DataRecord } from '../core/interfaces';
/**
* FixedWidthWriter class for writing data records to a file in a fixed-width format.
* Each field's width is predefined, and values are padded or truncated to fit.
*/
export declare class FixedWidthWriter implements DataWriter {
private readonly filePath;
private outputStream;
private fieldWidths;
private fieldNames;
private hasFieldNamesInFirstRow;
private headerWritten;
private initializedFieldNames;
/**
* Constructs a new FixedWidthWriter.
* @param filePath The path to the output file.
*/
constructor(filePath: string);
/**
* Sets the fixed widths for each field. This is mandatory before writing.
* @param widths A list of numbers, where each number represents the width of a field.
* @returns The current FixedWidthWriter instance for chaining.
*/
setFieldWidths(...widths: number[]): this;
/**
* Sets the names of the fields. If `hasFieldNamesInFirstRow` is true, these names
* will be used to write the header row. Otherwise, they define the order of data
* extraction from `DataRecord` objects.
* @param names A list of string names for the fields.
* @returns The current FixedWidthWriter instance for chaining.
*/
setFieldNames(...names: string[]): this;
/**
* Specifies whether the first row written should be a header row containing the field names.
* If true, `setFieldNames` must be called before writing any records.
* @param value True to write field names in the first row, false otherwise.
* @returns The current FixedWidthWriter instance for chaining.
*/
setFieldNamesInFirstRow(value: boolean): this;
/**
* Writes a single data record to the fixed-width file.
* @param record The DataRecord object to write.
* @returns A Promise that resolves when the record has been written.
* @throws Error if field widths are not defined.
* @throws Error if `hasFieldNamesInFirstRow` is true but field names are not set.
* @throws Error if the number of field names does not match the number of field widths.
*/
write(record: DataRecord): Promise<void>;
/**
* Writes all data records from an asynchronous iterable to the fixed-width file.
* @param records An AsyncIterableIterator of DataRecord objects.
* @returns A Promise that resolves when all records have been written.
*/
writeAll(records: AsyncIterableIterator<DataRecord>): Promise<void>;
/**
* Closes the underlying write stream. This should be called when all data has been written.
* @returns A Promise that resolves when the stream is closed.
*/
close(): Promise<void>;
/**
* Formats a single field value to fit the specified width.
* It truncates if the value is too long and pads with spaces to the right if too short.
* @param value The value to format.
* @param width The target width for the field.
* @returns The formatted string.
*/
private formatField;
}