Skip to content

Commit

Permalink
Expose the getFirstName function for issue: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidemiceli committed Oct 26, 2018
1 parent 1386153 commit 1e9400f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 56 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ This module is able to clean the text, detecting gender from dirty or unclear na
## Example
```javascript
// Require gender detection module
var gender = require('gender-detection');
const gender = require('gender-detection');

let g;

// Use it to detect the gender:
g = gender.detect('Tim Johnson');
Expand All @@ -31,10 +33,13 @@ g = gender.detect('BiLL$...');

g = gender.detect('::Jenni♥fer::');
// "female"
```

### Tests
// Extract the first name
const first_name = gender.getFirstName('Mario Bros');
// "mario"
```

In case you want to run some tests, please install the test framework Mocha (https://mochajs.org)
After installing it, on the parent directory type: `mocha test`.
The test directory is obviously `/test`.
### Unit tests
```shell
npm test
```
61 changes: 35 additions & 26 deletions index.js
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;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gender-detection",
"version": "0.0.5",
"version": "0.1.0",
"description": "Detect the gender of a person using his/her first name.",
"license": "GPL-3.0",
"main": "index.js",
Expand Down Expand Up @@ -29,5 +29,8 @@
},
"scripts": {
"test": "mocha test"
},
"devDependencies": {
"mocha": "^5.2.0"
}
}
52 changes: 29 additions & 23 deletions test/test.js
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'));
});
});

0 comments on commit 1e9400f

Please sign in to comment.