biojs-io-graduates
Version:
A parser for the BioJS tutorial graduate list
48 lines (40 loc) • 1.26 kB
JavaScript
/*
* Welcome to the biojs tutorial.
* Can you build a parser for counting the number of appearances for each country in the data set?
* URL to this tutorial: http://edu.biojs.net/graduates/basic_io/
*/
var _ = require("lodash"); // only needed for a shorter way to use parse
var parser = require("biojs-io-parser"); // adds extra methods like read
var graduates = {};
parser.mixin(graduates); // graduates inherits the methods from the generic io parser
graduates.read_static = function() {
var data = "greenify:DE\ndaviddao:HK\nmhelvens:NL\ntimruffles:UK\niriscshih:TW\n\n";
return graduates.parse(data);
};
graduates.parse = function(data) {
return _(data.split("\n")).map(function(e) {
return e.split(":");
}).filter(function(e) {
return e.length === 2;
}).map(function(e) {
return e[1];
}).countBy().value();
};
graduates.parse_long = function(data) {
// count countries
var parsed = {};
for (var i = 0; i < data.length; i++) {
// skip empty rows
if (data[i].trim().length === 0) {
continue;
}
var row = data[i].split(":");
// init if new
if (typeof parsed[row[1]] === "undefined") {
parsed[row[1]] = 0;
}
parsed[row[1]] ++;
}
return parsed;
};
module.exports = graduates;