@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
55 lines (54 loc) • 3.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Affirm_1 = __importDefault(require("../../core/Affirm"));
const CSVParser_1 = __importDefault(require("./CSVParser"));
const Constants_1 = __importDefault(require("../../Constants"));
const ProducerManager_1 = __importDefault(require("../producer/ProducerManager"));
class ParseManagerClass {
constructor() {
this._extractHeader = (headerLine, delimiter, producer, discover) => {
var _a, _b, _c, _d, _e, _f;
(0, Affirm_1.default)(headerLine, `Invalid CSV header line for producer "${producer.name}"`);
(0, Affirm_1.default)(delimiter, 'Invalid CSV delimiter');
(0, Affirm_1.default)(producer, 'Invalid producer');
let columns = ProducerManager_1.default.getColumns(producer);
const headerColumns = CSVParser_1.default.parseRow(headerLine, delimiter).map(x => x.trim());
// If I'm discovering the file, then it means that the dimensions are not set, so I use the ones that I get from the file directly
if (discover)
columns = headerColumns.map(x => ({ nameInProducer: x }));
const csvColumns = [];
for (const pColumn of columns) {
// Skip sourceFilename dimensions - they don't exist in the source file
// They are added dynamically by the driver when reading the file
if (((_a = pColumn.dimension) === null || _a === void 0 ? void 0 : _a.sourceFilename) === true) {
// Find the index of $source_filename in the header (it was added by the driver)
const sourceFilenameIndex = headerColumns.findIndex(x => x === Constants_1.default.SOURCE_FILENAME_COLUMN);
if (sourceFilenameIndex > -1) {
csvColumns.push({
index: sourceFilenameIndex,
name: Constants_1.default.SOURCE_FILENAME_COLUMN,
saveAs: pColumn.nameInProducer,
type: (_c = (_b = pColumn.dimension) === null || _b === void 0 ? void 0 : _b.type) !== null && _c !== void 0 ? _c : 'string'
});
}
continue;
}
const columnKey = (_d = pColumn.aliasInProducer) !== null && _d !== void 0 ? _d : pColumn.nameInProducer;
const csvColumnIndex = headerColumns.findIndex(x => x === columnKey);
(0, Affirm_1.default)(csvColumnIndex > -1, `The column "${pColumn.nameInProducer}" (with key "${columnKey}") of producer "${producer.name}" doesn't exist in the underlying dataset.`);
csvColumns.push({
index: csvColumnIndex,
name: columnKey,
saveAs: pColumn.nameInProducer,
type: (_f = (_e = pColumn.dimension) === null || _e === void 0 ? void 0 : _e.type) !== null && _f !== void 0 ? _f : 'string'
});
}
return csvColumns;
};
}
}
const ParseManager = new ParseManagerClass();
exports.default = ParseManager;