UNPKG

@forzalabs/remora

Version:

A powerful CLI tool for seamless data translation.

121 lines (120 loc) 5.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Algo_1 = __importDefault(require("../../core/Algo")); const CSVParser_1 = __importDefault(require("../parsing/CSVParser")); const TypeCaster_1 = __importDefault(require("../transform/TypeCaster")); class DatasetRecord { constructor(row, dimensions, delimiter) { this.parse = (row, delimiter, dimensions) => { if (!this.isEmpty() && dimensions.length > 0) { const parts = CSVParser_1.default.parseRow(row, delimiter); for (let i = 0; i < dimensions.length; i++) { const dim = dimensions[i]; // Use dim.index to get the correct column from the file, not the loop index this._value[dim.name] = TypeCaster_1.default.cast(parts[dim.index], dim.type, dim.format); } } }; this.stringify = () => this._dimensions.map(x => `"${this._value[x.name]}"`).join(this._delimiter); this.isEmpty = () => { var _a; return ((_a = this._row) === null || _a === void 0 ? void 0 : _a.trim().length) === 0; }; this.getRaw = () => this._row; this.getValue = (dimension) => this._value[dimension]; this.setValue = (dimension, value) => { this._value[dimension] = value; return this; }; /** * Reinitialize the record with new data instead of creating a new instance * This is used for object pooling optimization */ this.reinitialize = (row, dimensions, delimiter) => { this._row = row; this._dimensions = dimensions; this._delimiter = delimiter; this._value = {}; this.parse(row, delimiter, this._dimensions); }; this.wholeUpdateDimension = (update) => { var _a, _b, _c, _d, _e; if (update.toDelete) { // To remove delete this._value[update.currentDimension.name]; if (this._dimensions.some(x => x.key === update.currentDimension.name)) this._dimensions = this._dimensions.filter(x => x.key !== update.currentDimension.name); else this._dimensions = this._dimensions.filter(x => x.key !== update.currentDimension.key); } else if (!update.currentDimension) { // To create (at the right position) const newDimension = { index: update.newPosition, key: update.newName, name: update.newName, hidden: update.newHidden, type: (_b = (_a = update.currentDimension) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : 'string' }; this._value[newDimension.name] = null; this._dimensions = [...this._dimensions, newDimension]; } else { // Change: name, hidden, position let index = this._dimensions.findIndex(x => x.key === update.currentDimension.name); if (index < 0) index = this._dimensions.findIndex(x => x.key === update.currentDimension.key); const currentDim = this._dimensions[index]; const updatedDim = { name: update.newName, key: (_c = currentDim.key) !== null && _c !== void 0 ? _c : update.newName, hidden: update.newHidden, index: update.newPosition, type: (_e = (_d = update.currentDimension) === null || _d === void 0 ? void 0 : _d.type) !== null && _e !== void 0 ? _e : 'string' }; this._value[updatedDim.name] = this._value[currentDim.name]; if (updatedDim.name !== currentDim.name) delete this._value[currentDim.name]; const newDimensions = [...this._dimensions]; newDimensions.splice(index, 1, updatedDim); this._dimensions = newDimensions; } return this; }; this.sortDimensions = () => { const isOutOfOrder = this._dimensions.some((dim, index) => dim.index !== index); if (isOutOfOrder) { this._dimensions.sort((a, b) => a.index - b.index); } }; this.toJSON = () => { if (this._dimensions.some(x => x.hidden)) { // remove the not wanted dimension const clonedValue = structuredClone(this._value); for (const dim of this._dimensions) { if (dim.hidden) delete clonedValue[dim.name]; } return JSON.stringify(clonedValue); } else { return JSON.stringify(this._value); } }; this.toCSV = (delimiter) => { const myDelimtier = delimiter !== null && delimiter !== void 0 ? delimiter : this._delimiter; // remove the not wanted dimension const line = this._dimensions .filter(x => !x.hidden) .map(x => { var _a, _b; return `"${Algo_1.default.replaceAll((_b = (_a = this._value[x.name]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '', '"', '""')}"`; }) .join(myDelimtier); return line; }; this._row = row; this._dimensions = dimensions; this._delimiter = delimiter; this._value = {}; this.parse(row, delimiter, this._dimensions); } } exports.default = DatasetRecord;