WME/aufgabe5/node_modules/csvtojson/.ts-node/46be5da2105bfdf13e8db086405c89f751746b6e7f1dbb5a5600bad96af1b73b/5b7b6cc115cc8559849a34561c82531410ecb53ccdbe486e410336329db2fdb4.js

131 lines
17 KiB
JavaScript
Raw Normal View History

2019-01-09 11:25:28 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var rowSplit_1 = require("./rowSplit");
var Converter_1 = require("./Converter");
var assert = require("assert");
describe("Test delimiters", function () {
var getDelimiter = function (str, opt) {
return rowSplit_1.RowSplit.prototype["getDelimiter"].call({
conv: {
parseParam: {
delimiter: opt.delimiter
}
}
}, str);
};
it("should return the explicitly specified delimiter", function () {
var delimiter = ";";
var rowStr = "a;b;c";
var returnedDelimiter = getDelimiter(rowStr, { delimiter: ";" });
assert.equal(returnedDelimiter, delimiter);
});
it("should return the autodetected delimiter if 'auto' specified", function () {
var rowStr = "a;b;c";
var returnedDelimiter = getDelimiter(rowStr, { delimiter: "auto" });
assert(returnedDelimiter === ";");
});
it("should return the ',' delimiter if delimiter cannot be specified, in case of 'auto'", function () {
var rowStr = "abc";
var returnedDelimiter = getDelimiter(rowStr, { delimiter: "auto" });
assert(returnedDelimiter === ",");
});
it("should accetp an array with potential delimiters", function () {
var rowStr = "a$b$c";
var returnedDelimiter = getDelimiter(rowStr, { delimiter: [",", ";", "$"] });
assert(returnedDelimiter === '$');
});
});
describe("ParseMultiLine function", function () {
var rowSplit = new rowSplit_1.RowSplit(new Converter_1.Converter());
var func = function (lines) {
return rowSplit.parseMultiLines(lines);
};
it("should convert lines to csv lines", function () {
var lines = [
"a,b,c,d",
"hello,world,csvtojson,abc",
"1,2,3,4"
];
var res = func(lines);
assert.equal(res.rowsCells.length, 3);
assert.equal(res.partial, "");
});
it("should process line breaks", function () {
var lines = [
"a,b,c",
'15",hello,"ab',
"cde\"",
"\"b\"\"b\",cc,dd"
];
var res = func(lines);
assert.equal(res.rowsCells.length, 3);
assert.equal(res.rowsCells[1][0], "15\"");
assert.equal(res.rowsCells[1][2], "ab\ncde");
assert.equal(res.rowsCells[2][0], "b\"b");
assert.equal(res.partial, "");
});
it("should return partial if line not closed", function () {
var lines = [
"a,b,c",
'15",hello,"ab',
"d,e,f"
];
var res = func(lines);
assert.equal(res.rowsCells.length, 1);
assert.equal(res.partial, "15\",hello,\"ab\nd,e,f\n");
});
});
describe("RowSplit.parse function", function () {
var rowSplit = new rowSplit_1.RowSplit(new Converter_1.Converter());
var func = function (str) {
return rowSplit.parse(str);
};
it("should split complete csv line", function () {
var str = "hello,world,csvtojson,awesome";
var res = func(str);
assert.equal(res.cells.length, 4);
assert.equal(res.closed, true);
});
it("should split incomplete csv line", function () {
var str = "hello,world,\"csvtojson,awesome";
var res = func(str);
assert.equal(res.closed, false);
});
it("should allow multiple line", function () {
var str = "\"he\"llo\",world,\"csvtojson,a\"\nwesome\"";
var res = func(str);
assert.equal(res.closed, true);
assert.equal(res.cells[2], 'csvtojson,a"\nwesome');
});
it("should allow blank quotes", function () {
var data = "a|^^|^b^";
var rowSplit = new rowSplit_1.RowSplit(new Converter_1.Converter({
delimiter: '|',
quote: '^',
noheader: true
}));
var res = rowSplit.parse(data);
assert.equal(res.cells[1], "");
});
it("should allow blank quotes in quotes", function () {
var data = 'a,"hello,this,"", test"';
var rowSplit = new rowSplit_1.RowSplit(new Converter_1.Converter({
noheader: true
}));
var res = rowSplit.parse(data);
assert.equal(res.cells[1], 'hello,this,", test');
});
it("should smart detect if an initial quote is only part of value ", function () {
var data = '"Weight" (kg),Error code,"Height" (m)';
var rowSplit = new rowSplit_1.RowSplit(new Converter_1.Converter({
noheader: true
}));
var res = rowSplit.parse(data);
assert.equal(res.cells.length, 3);
assert(res.closed);
assert.equal(res.cells[0], '"Weight" (kg)');
assert.equal(res.cells[1], 'Error code');
assert.equal(res.cells[2], '"Height" (m)');
});
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiL1VzZXJzL2t4aWFuZy93b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