UNPKG

genderize

Version:

Guess gender of a first name through a HTTP API

54 lines (44 loc) 1.27 kB
var stringify = require('querystring').stringify var request = require('request') var through = require('through2') function genderize (firstname, options, cb) { var qs = stringify({name: firstname}) if (typeof options === 'function') { cb = options } else { qs += '&' + stringify(options) } request('https://api.genderize.io?' + qs, function (err, res, body) { if (err) return cb(err) try { var parsed = JSON.parse(body) cb(null, parsed) } catch (e) { cb(new Error('Could not parse json response from genderize.io')) } }) } genderize.stream = function (options) { options = options || {} return through.obj(function (chunk, enc, cb) { if (Buffer.isBuffer(chunk)) chunk = chunk.toString() genderize(chunk, options, cb) }) } genderize.list = function (names, options, cb) { var values = names.reduce(function (acc, current, index) { acc['name[' + index + ']'] = current return acc }, {}) var qs = stringify(values) if (typeof options === 'function') { cb = options } else { qs += '&' + stringify(options) } request('https://api.genderize.io?' + qs, function (err, res, body) { if (err) cb(err) cb(null, JSON.parse(body)) }) } module.exports = genderize