nested_json_to_csv
Version:
95 lines (94 loc) • 4.42 kB
JavaScript
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Csv_instances, _Csv_headers, _Csv_body, _Csv_rows, _Csv_lint, _Csv_replaceAt;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Csv = void 0;
class Csv {
constructor(headers, body) {
_Csv_instances.add(this);
_Csv_headers.set(this, void 0);
_Csv_body.set(this, void 0);
_Csv_rows.set(this, [[]]);
if (headers != undefined && headers.length == 0) {
throw Error("Can't have no headers in csv");
}
__classPrivateFieldSet(this, _Csv_headers, headers !== null && headers !== void 0 ? headers : [], "f");
__classPrivateFieldSet(this, _Csv_body, body !== null && body !== void 0 ? body : "", "f");
}
getRows() {
return __classPrivateFieldGet(this, _Csv_rows, "f");
}
getHeaders() {
return __classPrivateFieldGet(this, _Csv_headers, "f");
}
setbody(body) {
__classPrivateFieldSet(this, _Csv_body, body, "f");
return this;
}
getBody() {
return __classPrivateFieldGet(this, _Csv_body, "f");
}
setHeaders(headers) {
__classPrivateFieldSet(this, _Csv_headers, headers, "f");
return this;
}
makeHeaders(rawCsv) {
let headers = [];
let line = rawCsv.substring(0, rawCsv.indexOf("\n"));
line = __classPrivateFieldGet(this, _Csv_instances, "m", _Csv_lint).call(this, line);
headers = line.split(",");
__classPrivateFieldSet(this, _Csv_headers, headers, "f");
return this;
}
makeRows() {
if (__classPrivateFieldGet(this, _Csv_body, "f").length == 0) {
throw Error("Can't make an array of an empty body");
}
const lines = __classPrivateFieldGet(this, _Csv_body, "f").split("\n");
if (lines[lines.length - 1] == "")
lines.pop();
__classPrivateFieldSet(this, _Csv_rows, [], "f");
lines.forEach((line) => {
line = __classPrivateFieldGet(this, _Csv_instances, "m", _Csv_lint).call(this, line);
const row = line.split(",");
__classPrivateFieldGet(this, _Csv_rows, "f").push(row);
});
return this;
}
make() {
return __classPrivateFieldGet(this, _Csv_headers, "f").join() + "\n" + __classPrivateFieldGet(this, _Csv_body, "f");
}
}
exports.Csv = Csv;
_Csv_headers = new WeakMap(), _Csv_body = new WeakMap(), _Csv_rows = new WeakMap(), _Csv_instances = new WeakSet(), _Csv_lint = function _Csv_lint(line) {
line = line[line.length - 1] == "," ? line.slice(0, line.length - 1) : line;
line =
line[line.length - 1] == "\r" ? line.slice(0, line.length - 1) : line;
line = line[line.length - 1] == "," ? line.slice(0, line.length - 1) : line;
let replace = false;
for (let i = 0; i < line.length; i++) {
if (line[i] == '"')
replace = !replace;
if (replace) {
if (line[i] == ",")
line = __classPrivateFieldGet(this, _Csv_instances, "m", _Csv_replaceAt).call(this, line, i, "-");
}
}
line =
line[line.length - 1] == "\r" ? line.slice(0, line.length - 1) : line;
return line;
}, _Csv_replaceAt = function _Csv_replaceAt(word, index, char) {
const arrayChar = word.split("");
arrayChar[index] = char;
return arrayChar.join("");
};