Skip to content

Instantly share code, notes, and snippets.

@xshyamx
Created March 6, 2014 13:42
Show Gist options
  • Save xshyamx/9389975 to your computer and use it in GitHub Desktop.
Save xshyamx/9389975 to your computer and use it in GitHub Desktop.
For Better names in Chance.jshttp://chancejs.com/#todo
/*
* For http://chancejs.com/#todo - Better names
* Get national data from http://www.ssa.gov/oact/babynames/limits.html
* extract names.zip
* cat yob*.txt > all.csv
*/
var fs = require('fs');
var filename = 'all.csv', males = {}, females = {};
fs.readFile(filename, {encoding: 'utf-8'}, function (err, data) {
if (err) throw err;
data.split('\n').forEach(function(line) {
var parts = line.split(',');
if ( parts[1] == 'M' ) {
males[parts[0]] = (males[parts[0]] || 0) + parseInt(parts[2]);
} else {
females[parts[0]] = (females[parts[0]] || 0) + parseInt(parts[2]);
}
});
var desc = function(a,b) {
return b[1] - a[1];
},
name = function(x) {
return x[0];
},
notEmpty = function(x) {
return x.length > 0;
},
maleArr = Object.keys(males).map(function(name) {
return [name, males[name]];
}).sort(desc).map(name).filter(notEmpty).slice(0, 250),
femaleArr = Object.keys(females).map(function(name) {
return [name, females[name]];
}).sort(desc).map(name).filter(notEmpty).slice(0, 250);
var outFile = 'chance-names.js';
fs.writeFile(
outFile,
'var firstNames = ' + JSON.stringify({Male: maleArr, Female: femaleArr}) + ';',
function(err) {
if ( err ) throw err;
console.log('wrote %s successfully', outFile);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment