Skip to content

Commit

Permalink
Replace some foreach callback with simple for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 1, 2012
1 parent 9b9fd16 commit 9333d0b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ module.exports = function(){
if( csv.writeOptions.lineBreaks === null ){
csv.writeOptions.lineBreaks = "\r\n";
}
data.forEach(function(line){
state.line = line;
for(var i=0; i<data.length; i++){
state.line = data[i];
flush();
})
}
}else{
try{
parse(data);
Expand Down Expand Up @@ -330,9 +330,10 @@ module.exports = function(){
return;
}
var line = {};
csv.readOptions.columns.forEach(function(column, i){
for(var i=0; i<csv.readOptions.columns.length; i++){
var column = csv.readOptions.columns[i];
line[column] = state.line[i]||null;
})
}
state.line = line;
line = null;
}
Expand Down Expand Up @@ -373,9 +374,10 @@ module.exports = function(){
var columns = csv.writeOptions.columns || csv.readOptions.columns;
var _line = [];
if(columns){
columns.forEach(function(column, i){
for(var i=0; i<columns.length; i++){
var column = columns[i];
_line[i] = (typeof line[column] === 'undefined' || line[column] === null) ? '' : line[column];
})
}
}else{
for(var column in line){
_line.push(line[column]);
Expand All @@ -390,7 +392,8 @@ module.exports = function(){
}
if(line instanceof Array){
var newLine = state.countWriten ? csv.writeOptions.lineBreaks || "\r" : '';
line.forEach(function(field,i){
for(var i=0; i<line.length; i++){
var field = line[i];
if(typeof field === 'string'){
// fine 99% of the cases, keep going
}else if(typeof field === 'number'){
Expand Down Expand Up @@ -421,7 +424,7 @@ module.exports = function(){
if(i!==line.length-1){
newLine += csv.writeOptions.delimiter || csv.readOptions.delimiter;
}
});
}
line = newLine;
}
}
Expand Down

0 comments on commit 9333d0b

Please sign in to comment.