-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
41 lines (36 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
// 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';
}
// 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];
}
// Detect gender
exports.detect = function(fullName, lang) {
const firstName = getFirstNameFromFullName(fullName);
const resgender = getGender(firstName, lang);
return resgender;
}
// Export getFirstNameFromFullName function
exports.getFirstName = getFirstNameFromFullName;