From 1e9400f5bcb2ee1843a0924b80a281d1c98c6a91 Mon Sep 17 00:00:00 2001 From: Davide Miceli Date: Fri, 26 Oct 2018 23:45:42 +0200 Subject: [PATCH] Expose the getFirstName function for issue: https://github.com/davidemiceli/gender-detection/issues/2 --- README.md | 17 +++++++++------ index.js | 61 ++++++++++++++++++++++++++++++---------------------- package.json | 5 ++++- test/test.js | 52 ++++++++++++++++++++++++-------------------- 4 files changed, 79 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 3d1b2da..87182aa 100644 --- a/README.md +++ b/README.md @@ -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'); @@ -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 +``` \ No newline at end of file diff --git a/index.js b/index.js index 4c407b9..9afe92d 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/package.json b/package.json index d4a76d2..33f4c3f 100644 --- a/package.json +++ b/package.json @@ -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", @@ -29,5 +29,8 @@ }, "scripts": { "test": "mocha test" + }, + "devDependencies": { + "mocha": "^5.2.0" } } diff --git a/test/test.js b/test/test.js index 4decb3c..9998cce 100644 --- a/test/test.js +++ b/test/test.js @@ -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')); + }); + });