Skip to content

Commit

Permalink
reverseEach implemented, tests, fixes #135
Browse files Browse the repository at this point in the history
+ Hinting fixes
  • Loading branch information
alexgraul authored and Irene Ros committed Jun 13, 2012
1 parent 7983468 commit 1c3b609
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/importers/remote.js
Expand Up @@ -147,7 +147,7 @@
}

if (!paramStr && !isScript) {
url += "?"
url += "?";
}

if ( !paramStr || !/callback/.test(paramStr) ) {
Expand Down
21 changes: 11 additions & 10 deletions src/parsers/delimited.js
Expand Up @@ -46,14 +46,15 @@
var columns = [],
columnData = {},
uniqueSequence = {};
uniqueId = function(str) {
if ( !uniqueSequence[str] ) {
uniqueSequence[str] = 0;
}
var id = str + uniqueSequence[str];
uniqueSequence[str] += 1;
return id;
}

var 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 @@ -177,13 +178,13 @@

} else {

function createColumnName(start) {
var createColumnName = function(start) {
var newName = uniqueId(start);
while ( columns.indexOf(newName) !== -1 ) {
newName = uniqueId(start);
}
return newName;
}
};

//No column name? Create one starting with X
if ( _.isUndefined(strMatchedValue) || strMatchedValue === '' ) {
Expand Down
15 changes: 14 additions & 1 deletion src/view.js
Expand Up @@ -391,13 +391,26 @@
* iterator - function that is passed each row
* iterator(rowObject, index, dataset)
* context - options object. Optional.
*/
*/
each : function(iterator, context) {
for(var i = 0; i < this.length; i++) {
iterator.apply(context || this, [this.rowByPosition(i), i]);
}
},

/**
* Iterates over all rows in the dataset in reverse order
* Parameters:
* iterator - function that is passed each row
* iterator(rowObject, index, dataset)
* context - options object. Optional.
*/
reverseEach : function(iterator, context) {
for(var i = this.length-1; i >= 0; i--) {
iterator.apply(context || this, [this.rowByPosition(i), i]);
}
},

/**
* Iterates over each column.
* Parameters:
Expand Down
37 changes: 37 additions & 0 deletions test/unit/views.js
Expand Up @@ -200,6 +200,43 @@

module("Views :: Rows Selection");

test("each", function() {
var ds = Util.baseSample();
var expectedRow = {
_id : ds._columns[0].data[0],
one : 1,
two : 4,
three : 7
};

ds.each(function(row, index) {
if (index === 0) {
ok(_.isEqual(row, expectedRow), "Row by position is equal");
}
});

});


test("reversed each", 1, function() {
var ds = Util.baseSample();
var expectedRow = {
_id : ds._columns[0].data[2],
one : 3,
two : 6,
three : 9
};
var count = 0;

ds.reverseEach(function(row, index) {
console.log(row, index);
if (count === 0) {
ok(_.isEqual(row, expectedRow), "Row by position is equal");
}
count += 1;
});
});

test("Get row by position", function() {
var ds = Util.baseSample();
var row = ds.rowByPosition(0);
Expand Down

0 comments on commit 1c3b609

Please sign in to comment.