×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Adam Bedell
Added: Aug 24, 2013 8:20 PM
Views: 23
Tags: nodejs parser
http://blog.modulus.io/absolute-beginners-guide-to-nodejs
  1. // Parser constructor.
  2. var Parser = function() {
  3.  
  4. };
  5.  
  6. // Parses the specified text.
  7. Parser.prototype.parse = function(text) {
  8.  
  9.   var results = {};
  10.  
  11.   // Break up the file into lines.
  12.   var lines = text.split('\n');
  13.  
  14.   lines.forEach(function(line) {
  15.     var parts = line.split(' ');
  16.     var letter = parts[1];
  17.     var count = parseInt(parts[2]);
  18.  
  19.     if(!results[letter]) {
  20.       results[letter] = 0;
  21.     }
  22.  
  23.     results[letter] += parseInt(count);
  24.   });
  25.  
  26.   return results;
  27. };
  28.  
  29. // Export the Parser constructor from this module.
  30. module.exports = Parser;