WME/aufgabe5/node_modules/csvtojson/.ts-node/46be5da2105bfdf13e8db086405c89f751746b6e7f1dbb5a5600bad96af1b73b/7746f769111b8e01145ed9cad6ea8e80ba4e28d41394c7291bd5dd6d6617e7a8.js

200 lines
22 KiB
JavaScript
Raw Normal View History

2019-01-09 11:25:28 +00:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var CSVError_1 = __importDefault(require("./CSVError"));
var set_1 = __importDefault(require("lodash/set"));
var numReg = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
function default_1(csvRows, conv) {
var res = [];
for (var i = 0, len = csvRows.length; i < len; i++) {
var r = processRow(csvRows[i], conv, i);
if (r) {
res.push(r);
}
}
return res;
}
exports.default = default_1;
;
function processRow(row, conv, index) {
if (conv.parseParam.checkColumn && conv.parseRuntime.headers && row.length !== conv.parseRuntime.headers.length) {
throw (CSVError_1.default.column_mismatched(conv.parseRuntime.parsedLineNumber + index));
}
var headRow = conv.parseRuntime.headers || [];
var resultRow = convertRowToJson(row, headRow, conv);
if (resultRow) {
return resultRow;
}
else {
return null;
}
}
function convertRowToJson(row, headRow, conv) {
var hasValue = false;
var resultRow = {};
for (var i = 0, len = row.length; i < len; i++) {
var item = row[i];
if (conv.parseParam.ignoreEmpty && item === '') {
continue;
}
hasValue = true;
var head = headRow[i];
if (!head || head === "") {
head = headRow[i] = "field" + (i + 1);
}
var convFunc = getConvFunc(head, i, conv);
if (convFunc) {
var convRes = convFunc(item, head, resultRow, row, i);
if (convRes !== undefined) {
setPath(resultRow, head, convRes, conv, i);
}
}
else {
// var flag = getFlag(head, i, param);
// if (flag === 'omit') {
// continue;
// }
if (conv.parseParam.checkType) {
var convertFunc = checkType(item, head, i, conv);
item = convertFunc(item);
}
if (item !== undefined) {
setPath(resultRow, head, item, conv, i);
}
}
}
if (hasValue) {
return resultRow;
}
else {
return null;
}
}
var builtInConv = {
"string": stringType,
"number": numberType,
"omit": function () { }
};
function getConvFunc(head, i, conv) {
if (conv.parseRuntime.columnConv[i] !== undefined) {
return conv.parseRuntime.columnConv[i];
}
else {
var flag = conv.parseParam.colParser[head];
if (flag === undefined) {
return conv.parseRuntime.columnConv[i] = null;
}
if (typeof flag === "object") {
flag = flag.cellParser || "string";
}
if (typeof flag === "string") {
flag = flag.trim().toLowerCase();
var builtInFunc = builtInConv[flag];
if (builtInFunc) {
return conv.parseRuntime.columnConv[i] = builtInFunc;
}
else {
return conv.parseRuntime.columnConv[i] = null;
}
}
else if (typeof flag === "function") {
return conv.parseRuntime.columnConv[i] = flag;
}
else {
return conv.parseRuntime.columnConv[i] = null;
}
}
}
function setPath(resultJson, head, value, conv, headIdx) {
if (!conv.parseRuntime.columnValueSetter[headIdx]) {
if (conv.parseParam.flatKeys) {
conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
}
else {
if (head.indexOf(".") > -1) {
if (conv.parseParam.colParser[head] && conv.parseParam.colParser[head].flat) {
conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
}
else {
conv.parseRuntime.columnValueSetter[headIdx] = jsonSetter;
}
}
else {
conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
}
}
}
conv.parseRuntime.columnValueSetter[headIdx](resultJson, head, value);
// flatSetter(resultJson, head, value);
}
function flatSetter(resultJson, head, value) {
resultJson[head] = value;
}
function jsonSetter(resultJson, head, value) {
set_1.default(resultJson, head, value);
}
function checkType(item, head, headIdx, conv) {
if (conv.parseRuntime.headerType[headIdx]) {
return conv.parseRuntime.headerType[headIdx];
}
else if (head.indexOf('number#!') > -1) {
return conv.parseRuntime.headerType[headIdx] = numberType;
}
else if (head.indexOf('string#!') > -1) {
return conv.parseRuntime.headerType[headIdx] = stringType;
}
else if (conv.parseParam.checkType) {
return conv.parseRuntime.headerType[headIdx] = dynamicType;
}
else {
return conv.parseRuntime.headerType[headIdx] = stringType;
}
}
function numberType(item) {
var rtn = parseFloat(item);
if (isNaN(rtn)) {
return item;
}
return rtn;
}
function stringType(item) {
return item.toString();
}
function dynamicType(item) {
var trimed = item.trim();
if (trimed === "") {
return stringType(item);
}
if (numReg.test(trimed)) {
return numberType(item);
}
else if (trimed.length === 5 && trimed.toLowerCase() === "false" || trimed.length === 4 && trimed.toLowerCase() === "true") {
return booleanType(item);
}
else if (trimed[0] === "{" && trimed[trimed.length - 1] === "}" || trimed[0] === "[" && trimed[trimed.length - 1] === "]") {
return jsonType(item);
}
else {
return stringType(item);
}
}
function booleanType(item) {
var trimed = item.trim();
if (trimed.length === 5 && trimed.toLowerCase() === "false") {
return false;
}
else {
return true;
}
}
function jsonType(item) {
try {
return JSON.parse(item);
}
catch (e) {
return item;
}
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiL1VzZXJzL2t4aWFuZy93b3JrL3Byb2plY3RzL2NzdjJqc29uL3NyYy9saW5lVG9Kc29uLnRzIiwic291cmNlcyI6WyIvVXNlcnMva3hpYW5nL3dvcmsvcHJvamVjdHMvY3N2Mmpzb24vc3JjL2xpbmVUb0pzb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFDQSx3REFBa0M7QUFFbEMsbURBQTZCO0FBRzdCLElBQUksTUFBTSxHQUFHLDBDQUEwQyxDQUFDO0FBRXhELG1CQUF5QixPQUFtQixFQUFFLElBQWU7SUFDM0QsSUFBTSxHQUFHLEdBQWlCLEVBQUUsQ0FBQztJQUM3QixLQUFLLElBQUksQ0FBQyxHQUFHLENBQUMsRUFBRSxHQUFHLEdBQUcsT0FBTyxDQUFDLE1BQU0sRUFBRSxDQUFDLEdBQUcsR0FBRyxFQUFFLENBQUMsRUFBRSxFQUFFO1FBQ2xELElBQU0sQ0FBQyxHQUFHLFVBQVUsQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDLEVBQUUsSUFBSSxFQUFFLENBQUMsQ0FBQyxDQUFDO1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