Skip to content

Commit

Permalink
unique columns to the scope of the parse, not global
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Graul authored and Irene Ros committed Jun 13, 2012
1 parent ce5dea1 commit 2b98ae2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/parsers/delimited.js
Expand Up @@ -43,8 +43,18 @@
_.extend(Miso.Parsers.Delimited.prototype, Miso.Parsers.prototype, {

parse : function(data) {
var columns = [];
var columnData = {};
var columns = [],
columnData = {},
uniqueSequence = {};
uniqueId = function(str) {
if ( !uniqueSequence[str] ) {
uniqueSequence[str] = 0;
}
var id = str + uniqueSequence[str];
uniqueSequence[str] += 1;
return id;
}


var parseCSV = function(delimiterPattern, strData, strDelimiter, skipRows, emptyValue) {

Expand Down Expand Up @@ -168,9 +178,9 @@
} else {

function createColumnName(start) {
var newName = _.uniqueId(start);
var newName = uniqueId(start);
while ( columns.indexOf(newName) !== -1 ) {
newName = _.uniqueId(start);
newName = uniqueId(start);
}
return newName;
}
Expand Down
37 changes: 29 additions & 8 deletions test/unit/bugs.js
Expand Up @@ -96,7 +96,7 @@

});

test("#130 - CSV Parser adds together columns with the same name", 2, function() {
test("#130 - CSV Parser adds together columns with the same name", 3, function() {
var data = "A,B,C,B\n" +
"1,2,3,4\n" +
"5,6,7,8";
Expand All @@ -107,7 +107,30 @@

ds.fetch({ success: function() {
equals(this._columns.length, 5);
ok(/B\d+/.test(_.last(this._columns).name));
ok( this._column('B') );
ok( this._column('B0') );
}});

});


test("#130 - CSV Parser generating multiple sequences of column names", 7, function() {
var data = "A,A,B,B,,\n" +
"1,2,3,4,2,2\n" +
"5,6,7,8,2,2";
var ds = new Miso.Dataset({
data : data,
delimiter : ","
});

ds.fetch({ success: function() {
equals(this._columns.length, 7);
ok( this._column('B') );
ok( this._column('B0') );
ok( this._column('A') );
ok( this._column('A0') );
ok( this._column('X0') );
ok( this._column('X1') );
}});

});
Expand All @@ -123,9 +146,8 @@

ds.fetch({ success: function() {
equals(this._columns.length, 5);
_.each(_.map(this._columns, function(c) { return c.name }), function(n) {
if ( /X\d+/.test(n) ) { ok(true) }
});
ok( this._column('X0') );
ok( this._column('X1') );
}});

});
Expand All @@ -141,9 +163,8 @@

ds.fetch({ success: function() {
equals(this._columns.length, 5);
_.each(_.map(this._columns, function(c) { return c.name }), function(n) {
if ( /X\d+/.test(n) ) { ok(true) }
});
ok( this._column('X0') );
ok( this._column('X1') );
}});

});
Expand Down

0 comments on commit 2b98ae2

Please sign in to comment.