-
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.
Merge pull request #4 from davidemiceli/develop
Add new exposed getFirstName() function.
- Loading branch information
Showing
5 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# TypeScript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next | ||
|
||
# custom | ||
package-lock.json |
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')); | ||
}); | ||
}); |