Skip to content

Commit

Permalink
Merge pull request #684 from blslade-neumont/dev-gender-string
Browse files Browse the repository at this point in the history
Allowed gender passed to name.firstName(gender) to be a string
  • Loading branch information
Marak authored Aug 24, 2020
2 parents a0a8850 + b3fcea2 commit 3bbac5d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ function Name (faker) {
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 )

if(typeof gender === 'string') {
if(gender.toLowerCase() === 'male') {
gender = 0;
}
else if(gender.toLowerCase() === 'female') {
gender = 1;
}
}

if (typeof gender !== 'number') {
if(typeof faker.definitions.name.first_name === "undefined") {
gender = faker.random.number(1);
Expand Down
23 changes: 23 additions & 0 deletions test/name.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ if (typeof module !== 'undefined') {

}

function assertInArray(value, array) {
var idx = array.indexOf(value);
assert.notEqual(idx, -1);
}

describe("name.js", function () {
describe("firstName()", function () {
it("returns a random name", function () {
Expand All @@ -15,6 +20,24 @@ describe("name.js", function () {

faker.name.firstName.restore();
});

it("returns a gender-specific name when passed a number", function () {
for (var q = 0; q < 30; q++) {
var gender = Math.floor(Math.random() * 2);
var name = faker.name.firstName(gender);
if (gender === 0) assertInArray(name, faker.definitions.name.male_first_name);
else assertInArray(name, faker.definitions.name.female_first_name);
}
});

it("returns a gender-specific name when passed a string", function () {
for (var q = 0; q < 30; q++) {
var gender = Math.floor(Math.random() * 2);
var genderString = (gender === 0 ? 'male' : 'female');
var name = faker.name.firstName(genderString);
assertInArray(name, faker.definitions.name[genderString + '_first_name']);
}
});
});

describe("lastName()", function () {
Expand Down

0 comments on commit 3bbac5d

Please sign in to comment.