From 2b98ae2fe0edcdf6e353e6d8f36f507030541931 Mon Sep 17 00:00:00 2001 From: Alex Graul Date: Sat, 9 Jun 2012 13:33:19 +0100 Subject: [PATCH] unique columns to the scope of the parse, not global --- src/parsers/delimited.js | 18 ++++++++++++++---- test/unit/bugs.js | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/parsers/delimited.js b/src/parsers/delimited.js index d01e80c..ef3618a 100644 --- a/src/parsers/delimited.js +++ b/src/parsers/delimited.js @@ -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) { @@ -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; } diff --git a/test/unit/bugs.js b/test/unit/bugs.js index 5132066..0b679d2 100644 --- a/test/unit/bugs.js +++ b/test/unit/bugs.js @@ -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"; @@ -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') ); }}); }); @@ -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') ); }}); }); @@ -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') ); }}); });