@pujansrt/data-genie
Version:
High performant ETL engine written in TypeScript
99 lines (98 loc) • 3.62 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixedWidthReader = void 0;
const fs_1 = require("fs");
const readline = __importStar(require("readline"));
class FixedWidthReader {
constructor(filePath) {
this.filePath = filePath;
this.fieldWidths = [];
this.fieldNames = [];
this.hasFieldNamesInFirstRow = false;
this.initialized = false;
}
setFieldWidths(...widths) {
this.fieldWidths = widths;
return this;
}
setFieldNames(...names) {
this.fieldNames = names;
return this;
}
setFieldNamesInFirstRow(value) {
this.hasFieldNamesInFirstRow = value;
return this;
}
async *read() {
if (this.fieldWidths.length === 0) {
throw new Error('Field widths must be defined before reading.');
}
const stream = (0, fs_1.createReadStream)(this.filePath, { encoding: 'utf-8' });
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
for await (const line of rl) {
if (!line.trim())
continue;
// If field names come from first row
if (!this.initialized && this.hasFieldNamesInFirstRow) {
this.fieldNames = this.splitLine(line);
this.initialized = true;
continue; // skip header line
}
// Auto-generate field names if not set
if (!this.initialized && this.fieldNames.length === 0) {
this.fieldNames = this.fieldWidths.map((_, i) => `field${i}`);
this.initialized = true;
}
const values = this.splitLine(line);
const record = {};
for (let i = 0; i < this.fieldNames.length; i++) {
record[this.fieldNames[i]] = values[i];
}
yield record;
}
}
splitLine(line) {
const values = [];
let pos = 0;
for (const width of this.fieldWidths) {
const field = line.substring(pos, pos + width).trim();
values.push(field);
pos += width;
}
return values;
}
}
exports.FixedWidthReader = FixedWidthReader;