blankRK
blankRK

Reputation: 41

Stop stream read of fast-csv node module

How i can stop reading csv rows at some particular index using fast-csv node module? In fast-csv their is option for stream pause and resume but no option to close stream at some particular row. Following is the code :

var csvstream = CSV
.fromPath(self.fetchFilePath(fileName),{ ltrim : true, rtrim : true , headers : true , ignoreEmpty : true })
.transform(function (data){
    Object.keys(data).forEach(function (key) {
       var newKey       = key.trim();
       data[newKey]     = data[key].trim();
    });
    return data;
})
.on("data", function(data){
     //checking unicode char presence
     Object.keys(data).forEach(function (key) {
       if(data[key]){
          var charValue   = PUNYCODE.ucs2.decode(data[key]);
          charValue = charValue.map(function(val) {
             if(val>126){
               FileCleanFlag=false;
               errorData.push(data);
             }
          });
       }else{
          FileCleanFlag=false;
       }
     });

     if(!FileCleanFlag){
        #here want to jump to end block instead of parsing next rows
     }
})
.on('end', function (){
    #some work on rows containing error
});

In above code how i can jump to "end" block if error occur in some row?

Upvotes: 1

Views: 3463

Answers (1)

Dustin
Dustin

Reputation: 1026

There is no great way to end streams. You could just define FileCleanFlag as true and then if its false don't do anymore work.

var FileCleanFlag = true;
var csvstream = CSV
.fromPath(self.fetchFilePath(fileName),{ ltrim : true, rtrim : true , headers : true , ignoreEmpty : true })
.transform(function (data){
Object.keys(data).forEach(function (key) {
   var newKey       = key.trim();
   data[newKey]     = data[key].trim();
});
    return data;
})
.on("data", function(data){
     if (!FileCleanFlag) { return; }
     //checking unicode char presence
     Object.keys(data).forEach(function (key) {
     if(data[key]){
      var charValue   = PUNYCODE.ucs2.decode(data[key]);
      charValue = charValue.map(function(val) {
         if(val>126){
           FileCleanFlag=false;
           errorData.push(data);
         }
      });
   }else{
      FileCleanFlag=false;
   }
 });

 if(!FileCleanFlag){
    #here want to jump to end block instead of parsing next rows
 }
})
.on('end', function (){
   #some work on rows containing error
});

See How to close a readable stream (before end)? for how to close a stream hacks if you need to go down that route.

Also you might be able to throw an error and catch it accordingly.

Upvotes: 1

Related Questions