223 lines
27 KiB
JavaScript
223 lines
27 KiB
JavaScript
|
"use strict";
|
||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
|
};
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
var src_1 = __importDefault(require("../src"));
|
||
|
var assert = require("assert");
|
||
|
var fs = require("fs");
|
||
|
var sinon_1 = require("sinon");
|
||
|
var sb = sinon_1.sandbox.create();
|
||
|
describe("testCSVConverter3", function () {
|
||
|
afterEach(function () {
|
||
|
sb.restore();
|
||
|
});
|
||
|
it("should parse large csv file with UTF-8 without spliting characters", function (done) {
|
||
|
var testData = __dirname + "/data/large-utf8.csv";
|
||
|
var rs = fs.createReadStream(testData);
|
||
|
var csvConverter = src_1.default({});
|
||
|
var count = 0;
|
||
|
csvConverter.preRawData(function (csvRawData) {
|
||
|
assert(csvRawData.charCodeAt(0) < 2000);
|
||
|
return csvRawData;
|
||
|
});
|
||
|
csvConverter.on("data", function () {
|
||
|
count++;
|
||
|
});
|
||
|
csvConverter.then(function () {
|
||
|
assert(count === 5290);
|
||
|
done();
|
||
|
});
|
||
|
rs.pipe(csvConverter);
|
||
|
});
|
||
|
it("should setup customise type convert function", function (done) {
|
||
|
src_1.default({
|
||
|
checkType: true,
|
||
|
colParser: {
|
||
|
"column1": "string",
|
||
|
"column5": function (item, head, resultRow, row, i) {
|
||
|
assert.equal(item, '{"hello":"world"}');
|
||
|
assert.equal(head, "column5"),
|
||
|
assert(resultRow);
|
||
|
assert(row);
|
||
|
assert.equal(i, 5);
|
||
|
return "hello world";
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
.fromFile(__dirname + "/data/dataWithType")
|
||
|
.subscribe(function (json) {
|
||
|
assert.equal(typeof json.column1, "string");
|
||
|
assert.equal(json.column5, "hello world");
|
||
|
assert.strictEqual(json["name#!"], false);
|
||
|
assert.strictEqual(json["column9"], true);
|
||
|
})
|
||
|
.on('done', function () {
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
it("should accept pipe as quote", function (done) {
|
||
|
src_1.default({
|
||
|
quote: "|",
|
||
|
output: "csv"
|
||
|
})
|
||
|
.fromFile(__dirname + "/data/pipeAsQuote")
|
||
|
.subscribe(function (csv) {
|
||
|
assert.equal(csv[2], "blahhh, blah");
|
||
|
})
|
||
|
.on('done', function () {
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
it("emit file not exists error when try to open a non-exists file", function () {
|
||
|
var called = false;
|
||
|
var cb = sb.spy(function (err) {
|
||
|
assert(err.toString().indexOf("File does not exist") > -1);
|
||
|
});
|
||
|
return src_1.default()
|
||
|
.fromFile("somefile")
|
||
|
.subscribe(function (csv) {
|
||
|
})
|
||
|
.on("error", cb)
|
||
|
.then(function () {
|
||
|
assert(false);
|
||
|
}, function (err) {
|
||
|
assert.equal(cb.callCount, 1);
|
||
|
});
|
||
|
});
|
||
|
it("should include column that is both included and excluded", function () {
|
||
|
return src_1.default({
|
||
|
includeColumns: /b/,
|
||
|
ignoreColumns: /a|b/
|
||
|
})
|
||
|
.fromString("a,b,c\n1,2,3\n4,5,6")
|
||
|
.subscribe(function (d) {
|
||
|
assert(d.b);
|
||
|
assert(!d.a);
|
||
|
});
|
||
|
});
|
||
|
it("should allow async preLine hook", function () {
|
||
|
return src_1.default()
|
||
|
.preFileLine(function (line) {
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
setTimeout(function () {
|
||
|
resolve(line + "changed");
|
||
|
}, 20);
|
||
|
});
|
||
|
})
|
||
|
.fromString("a,b\n1,2")
|
||
|
.subscribe(function (d) {
|
||
|
assert(d.bchanged);
|
||
|
assert.equal(d.bchanged, "2changed");
|
||
|
});
|
||
|
});
|
||
|
it("should allow async subscribe function", function () {
|
||
|
return src_1.default({ trim: true })
|
||
|
.fromString("a,b,c\n 1,2,3\n 4,5,6")
|
||
|
.subscribe(function (d) {
|
||
|
return new Promise(function (resolve, reject) {
|
||
|
setTimeout(function () {
|
||
|
d.a = 10;
|
||
|
resolve();
|
||
|
}, 20);
|
||
|
});
|
||
|
})
|
||
|
.then(function (d) {
|
||
|
assert.equal(d[0].a, 10);
|
||
|
assert.equal(d[1].a, 10);
|
||
|
});
|
||
|
});
|
||
|
it("should propagate value to next then", function () {
|
||
|
return src_1.default({ trim: true })
|
||
|
.fromString("a,b,c\n 1,2,3\n 4,5,6")
|
||
|
.then(undefined, undefined)
|
||
|
.then(function (d) {
|
||
|
assert.equal(d.length, 2);
|
||
|
assert.equal(d[0].a, "1");
|
||
|
});
|
||
|
});
|
||
|
it("should propagate error to next then", function () {
|
||
|
return src_1.default({ trim: true })
|
||
|
.fromFile(__dirname + "/data/dataWithUnclosedQuotes")
|
||
|
.then(undefined, undefined)
|
||
|
.then(function () {
|
||
|
assert(false);
|
||
|
}, function (err) {
|
||
|
assert(err);
|
||
|
assert.equal(err.err, "unclosed_quote");
|
||
|
});
|
||
|
});
|
||
|
it("should fallback to text is number can not be parsed", function () {
|
||
|
return src_1.default({
|
||
|
colParser: {
|
||
|
"a": "number"
|
||
|
}
|
||
|
})
|
||
|
.fromString("a,b,c\n 1,2,3\n fefe,5,6")
|
||
|
.then(function (d) {
|
||
|
assert.strictEqual(d[0].a, 1);
|
||
|
assert.equal(d[1].a, "fefe");
|
||
|
});
|
||
|
});
|
||
|
it("should omit a column", function () {
|
||
|
return src_1.default({
|
||
|
colParser: {
|
||
|
"a": "omit"
|
||
|
}
|
||
|
})
|
||
|
.fromString("a,b,c\n 1,2,3\n fefe,5,6")
|
||
|
.then(function (d) {
|
||
|
assert.strictEqual(d[0].a, undefined);
|
||
|
assert.equal(d[1].a, undefined);
|
||
|
});
|
||
|
});
|
||
|
it("could turn off quote and should trim even quote is turned off", function () {
|
||
|
return src_1.default({
|
||
|
quote: "off",
|
||
|
trim: true
|
||
|
})
|
||
|
.fromString("a,b,c\n \"1\",\"2\",\"3\"\n \"fefe,5\",6")
|
||
|
.then(function (d) {
|
||
|
assert.equal(d[0].a, '"1"');
|
||
|
assert.equal(d[0].b, '"2"');
|
||
|
assert.equal(d[1].a, '"fefe');
|
||
|
assert.equal(d[1].b, '5"');
|
||
|
});
|
||
|
});
|
||
|
it("should allow ignoreEmpty with checkColumn", function () {
|
||
|
return src_1.default({
|
||
|
checkColumn: true,
|
||
|
ignoreEmpty: true
|
||
|
})
|
||
|
.fromString("date,altitude,airtime\n 2016-07-08,2000,23\n \n 2016-07-09,3000,43")
|
||
|
.then(function (data) {
|
||
|
}, function (err) {
|
||
|
console.log(err);
|
||
|
assert(!err);
|
||
|
});
|
||
|
});
|
||
|
it("should allow quotes without content", function () {
|
||
|
var data = "a|^^|^b^";
|
||
|
return src_1.default({
|
||
|
delimiter: '|',
|
||
|
quote: '^',
|
||
|
noheader: true,
|
||
|
})
|
||
|
.fromString(data)
|
||
|
.then(function (jsonObj) {
|
||
|
assert.equal(jsonObj[0].field2, "");
|
||
|
});
|
||
|
});
|
||
|
it("should parse header with quotes correctly", function () {
|
||
|
var testData = __dirname + "/data/csvWithUnclosedHeader";
|
||
|
return src_1.default({
|
||
|
headers: ["exam_date", "sample_no", "status", "sample_type", "patient_id", "last_name", "first_name", "gender_of_patient", "patient_birth_date", "patient_note", "patient_department", "accession_number", "sample_site", "physician", "operator", "department", "note", "test_order_code", "draw_time", "approval_status", "approval_time", "report_layout", "patient_account_number", "none_1", "errors_detected_during_measurement", "age", "error_code_01", "weight", "error_code_02", "height", "error_code_03", "hcg_beta_p", "error_code_04", "troponin_i_p", "error_code_05", "ck_mb_p", "error_code_06", "d_dimer_p", "error_code_07", "hscrp_p", "error_code_08", "myoglobin_p", "error_code_09", "nt_probnp", "error_code_10", "crp", "error_code_11", "bnp", "error_code_12", "tnt", "error_code_13", "demo_p", "error_code_14", "pct", "error_code_15"]
|
||
|
})
|
||
|
.fromFile(testData)
|
||
|
.then(function (d) {
|
||
|
assert.equal(d.length, 2);
|
||
|
assert.equal(d[0].sample_no, "12669");
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
//# source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