-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
47 lines (41 loc) · 1.01 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
42
43
44
45
46
47
const enMap = require('./names/en');
const itMap = require('./names/it');
const esMap = require('./names/es');
const frMap = require('./names/fr');
const deMap = require('./names/de');
const trMap = require('./names/tr');
/**
* Gender detection from first name and optional language
* @param {String} name First name
* @param {String} [lang] Language
* @return {String} male, female, unknown
*/
function getGender(name, lang = 'all') {
if (!name) {
return 'unknown';
}
// Lowercase name and lang to make the match
name = name.toLowerCase();
lang = (lang || 'all').toLowerCase();
const maps = {
en: enMap,
it: itMap,
es: esMap,
fr: frMap,
de: deMap,
tr: trMap,
};
const mapToUse = maps[lang];
const primaryResult = mapToUse ? mapToUse.get(name) : null;
return (
primaryResult ||
trMap.get(name) ||
deMap.get(name) ||
frMap.get(name) ||
esMap.get(name) ||
enMap.get(name) ||
itMap.get(name) ||
'unknown'
);
}
exports.getGender = getGender;