md-curcuma
Version:
A Typescript library for transporting and converting markdown and other data.
67 lines (66 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CSV_IO_Reader = void 0;
const observer_1 = require("../core/observer");
const types_1 = require("./types");
const fs = require("fs");
const csv_parse_1 = require("csv-parse");
class CSV_IO_Reader {
constructor(props) {
this.observer_subject = new observer_1.Observer_Subject();
this.props = props;
}
add_observer(observer, id) {
this.observer_subject.add_observer(observer, id);
}
notify_all(props) {
this.observer_subject.notify_all(props);
}
notify(props) {
this.observer_subject.notify(props);
}
read() {
let options = {
delimiter: this.props.delimiter,
columns: this.props.columns,
quote: this.props.quote,
relax_quotes: this.props.relax_quotes,
skip_records_with_error: this.props.skip_records_with_error,
trim: this.props.trim,
};
const parser = fs.createReadStream(this.props.path).pipe((0, csv_parse_1.parse)(options));
parser.on("data", (row) => {
let json_obj = row;
let o_props = new types_1.IO_Observer_Props();
o_props.from = "csv-io";
o_props.to = "runner";
o_props.command = "perform-tasks";
let io_meta = new types_1.IO_Meta();
io_meta.file_list_reader = [this.props.path];
io_meta.file_name_reader = this.props.path;
let the_dao = {
data: json_obj,
io_meta: io_meta,
};
o_props.dao = the_dao;
console.log("csv-io.do_command: perform tasks for", this.props.path);
this.notify_all(o_props);
});
parser.on("end", () => {
let m_props = new types_1.IO_Observer_Props();
m_props.from = "csv-io";
m_props.to = "runner";
m_props.command = "tasks-finished";
this.notify_all(m_props);
});
parser.on("error", (err) => {
console.error("Error parsing CSV:", err);
});
}
static cleanup(str) {
str = str.replace(/[(),"-]/g, "");
str = str.replace(/[ ]/g, "_");
return str;
}
}
exports.CSV_IO_Reader = CSV_IO_Reader;