-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose the getFirstName function for issue: #2
- Loading branch information
1 parent
1386153
commit 1e9400f
Showing
4 changed files
with
79 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
var male = require('./genders/male'); | ||
var female = require('./genders/female'); | ||
'use strict'; | ||
|
||
exports.detect = function(fullName, lang) { | ||
var firstName = getFirstNameFromFullName(fullName); | ||
resgender = getGender(firstName, lang); | ||
return resgender; | ||
// Requirements | ||
const male = require('./genders/male'); | ||
const female = require('./genders/female'); | ||
|
||
// Match gender type from the first name | ||
const getGender = function(firstName, lang) { | ||
if (lang && male[firstName] && male[firstName][lang] && female[firstName] && female[firstName][lang]) { | ||
return 'unisex'; | ||
} else if (lang && male[firstName] && male[firstName][lang]) { | ||
return 'male'; | ||
} else if (lang && female[firstName] && female[firstName][lang]) { | ||
return 'female'; | ||
} else if (male[firstName] && female[firstName]) { | ||
return 'unisex'; | ||
} else if (male[firstName]) { | ||
return 'male'; | ||
} else if (female[firstName]) { | ||
return 'female'; | ||
} | ||
return 'unknown'; | ||
} | ||
|
||
function getGender(firstName, lang) { | ||
gender = 'unknown'; | ||
if (lang && male[firstName] && male[firstName][lang] && female[firstName] && female[firstName][lang]) { | ||
gender = 'unisex'; | ||
} else if (lang && male[firstName] && male[firstName][lang]) { | ||
gender = 'male'; | ||
} else if (lang && female[firstName] && female[firstName][lang]) { | ||
gender = 'female'; | ||
} else if (male[firstName] && female[firstName]) { | ||
gender = 'unisex'; | ||
} else if (male[firstName]) { | ||
gender = 'male'; | ||
} else if (female[firstName]) { | ||
gender = 'female'; | ||
} | ||
return gender; | ||
// Extract the first name from a full name string | ||
const getFirstNameFromFullName = function(fullName) { | ||
return fullName | ||
.toLowerCase() | ||
.replace(/^\s+|^0-9+|[^a-z-úñäâàáéèëêïîöôùüûœç\- ]+/g, '') | ||
.split(/\s/)[0]; | ||
} | ||
|
||
function getFirstNameFromFullName(fullName) { | ||
fullName = fullName.toLowerCase(); | ||
fullName = fullName.replace(/^\s+|^0-9+|[^a-z-úñäâàáéèëêïîöôùüûœç\- ]+/g, ''); | ||
return fullName.split(/\s/)[0]; | ||
// Detect gender | ||
exports.detect = function(fullName, lang) { | ||
const firstName = getFirstNameFromFullName(fullName); | ||
const resgender = getGender(firstName, lang); | ||
return resgender; | ||
} | ||
|
||
// Export getFirstNameFromFullName function | ||
exports.getFirstName = getFirstNameFromFullName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,45 @@ | ||
/* | ||
* If you want run tests please install the test framework Mocha (https://mochajs.org). | ||
* Then type the command on the parent folder: mocha test | ||
*/ | ||
'use strict'; | ||
|
||
// Require assert mocha | ||
var assert = require('assert'); | ||
const assert = require('assert'); | ||
|
||
// Require gender detection module | ||
var gender = require('../index.js'); | ||
const gender = require('../index.js'); | ||
|
||
describe('Simple names', function() { | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('John','en')); | ||
assert.equal('female', gender.detect('Holly')); | ||
}); | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('John','en')); | ||
assert.equal('female', gender.detect('Holly')); | ||
}); | ||
}); | ||
|
||
describe('Double names', function() { | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('Tim Johnson')); | ||
assert.equal('female', gender.detect('Francesca Rossi')); | ||
}); | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('Tim Johnson')); | ||
assert.equal('female', gender.detect('Francesca Rossi')); | ||
}); | ||
}); | ||
|
||
describe('Bad formatted names', function() { | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('BiLL$...')); | ||
assert.equal('female', gender.detect('::Jenni♥fer::')); | ||
}); | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('BiLL$...')); | ||
assert.equal('female', gender.detect('::Jenni♥fer::')); | ||
}); | ||
}); | ||
|
||
describe('Simple names with languages variable', function() { | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('Johnny','en')); | ||
assert.equal('male', gender.detect('Andrea','it')); | ||
assert.equal('unisex', gender.detect('Andrea','hgjj')); | ||
}); | ||
it('It should return male, female, or unknown', function() { | ||
assert.equal('male', gender.detect('Johnny','en')); | ||
assert.equal('male', gender.detect('Andrea','it')); | ||
assert.equal('unisex', gender.detect('Andrea','hgjj')); | ||
}); | ||
}); | ||
|
||
describe('First names', function() { | ||
it('It should return the first name', function() { | ||
assert.equal('mario', gender.getFirstName('Mario')); | ||
assert.equal('karl', gender.getFirstName('Karl Callagan')); | ||
assert.equal('francesca', gender.getFirstName('Francesca Chiara Rossi')); | ||
assert.equal('ludwig', gender.getFirstName('Ludwig van Beethoven')); | ||
}); | ||
}); |