Skip to content

Commit

Permalink
Support gender in names
Browse files Browse the repository at this point in the history
  • Loading branch information
vectart committed Feb 17, 2015
1 parent a39082f commit 282b1ea
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ var faker = require('../index');

var _name = {

firstName: function () {
firstName: function (gender) {
if (typeof faker.definitions.name.male_first_name !== "undefined" && typeof faker.definitions.name.female_first_name !== "undefined") {
// some locale datasets ( like ru ) have first_name split by gender. since the name.first_name field does not exist in these datasets,
// we must randomly pick a name from either gender array so faker.name.firstName will return the correct locale data ( and not fallback )
var rand = faker.random.number(1);
if (rand === 0) {
gender = (typeof gender === 'number') ? gender : faker.random.number(1);
if (gender === 0) {
return faker.random.array_element(faker.locales[faker.locale].name.male_first_name)
} else {
return faker.random.array_element(faker.locales[faker.locale].name.female_first_name)
Expand All @@ -16,12 +16,12 @@ var _name = {
return faker.random.array_element(faker.definitions.name.first_name)
},

lastName: function () {
if (typeof faker.definitions.name.male_last_name !== "undefined" && typeof faker.defintions.name.female_last_name !== "undefined") {
lastName: function (gender) {
if (typeof faker.definitions.name.male_last_name !== "undefined" && typeof faker.definitions.name.female_last_name !== "undefined") {
// some locale datasets ( like ru ) have last_name split by gender. i have no idea how last names can have genders, but also i do not speak russian
// see above comment of firstName method
var rand = faker.random.number(1);
if (rand === 0) {
gender = (typeof gender === 'number') ? gender : faker.random.number(1);
if (gender === 0) {
return faker.random.array_element(faker.locales[faker.locale].name.male_last_name);
} else {
return faker.random.array_element(faker.locales[faker.locale].name.female_last_name);
Expand All @@ -30,15 +30,25 @@ var _name = {
return faker.random.array_element(faker.definitions.name.last_name);
},

findName: function (firstName, lastName) {
findName: function (firstName, lastName, gender) {
var r = faker.random.number(8);
firstName = firstName || faker.name.firstName();
lastName = lastName || faker.name.lastName();
var prefix, suffix;
// in particular locales first and last names split by gender,
// thus we keep consistency by passing 0 as male and 1 as female
gender = (typeof gender === 'number') ? gender : faker.random.number(1);
firstName = firstName || faker.name.firstName(gender);
lastName = lastName || faker.name.lastName(gender);
switch (r) {
case 0:
return faker.name.prefix() + " " + firstName + " " + lastName;
prefix = faker.name.prefix();
if (prefix) {
return prefix + " " + firstName + " " + lastName;
}
case 1:
return firstName + " " + lastName + " " + faker.name.suffix();
suffix = faker.name.prefix();
if (suffix) {
return firstName + " " + lastName + " " + suffix;
}
}

return firstName + " " + lastName;
Expand Down

0 comments on commit 282b1ea

Please sign in to comment.