Skip to content

Commit

Permalink
csv-harder test. Close #25
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 18, 2016
1 parent 4a6fc26 commit 7043262
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 212 deletions.
14 changes: 11 additions & 3 deletions lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ if (typeof Object.assign != 'function') {
if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}

var output = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
Expand Down Expand Up @@ -286,8 +285,17 @@ function getDelimitedField(info, field) {
}

function delimiterSplit(info, line) {
var parts = line.split(info.opts.separator);
return parts.map(function(name, index){ return getDelimitedField(info, name); });
var del = info.delimiter;
var sep = info.opts.separator;
var expressions = [
del+sep+del,
sep+del,
del+sep
];
var splitter=new RegExp(expressions.join('|'));
return line.split(splitter).map(function(name, index){
return getDelimitedField(info, name);
});
}

function separateWithDelimiter(info) {
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/csv-harder.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
create table "csv-harder" (
"rnum" integer,
"txt" character varying(20),
primary key ("rnum")
);

insert into "csv-harder" ("rnum", "txt") values
(1, 'txt 1'),
(2, 'incluye separator(;)'),
(3, 'txt con " doble'),
(4, 'txt con '' simple'),
(5, 'incluye delimiter(")');
6 changes: 6 additions & 0 deletions test/fixtures/csv-harder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"rnum";"txt"
1;"txt 1"
2;"incluye separator(;)"
3;"txt con "" doble"
4;"txt con ' simple"
5;"incluye delimiter("")"
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe("fixtures", function(){
{path:'pk-custom-names'},
{path:'with-null-lines'},
{path:'csv-simple'},
{path:'csv-harder'},
].forEach(function(fixture){
if(fixture.skip) {
it.skip("fixture: "+fixture.path);
Expand Down
Loading

0 comments on commit 7043262

Please sign in to comment.