-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
54 lines (50 loc) · 1.08 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
48
49
50
51
52
53
54
const male = require('./gender/male.json');
const female = require('./gender/female.json');
/**
* Remove space and extra characters from name
* @param {String} name
* @return {String}
*/
const cleanName = name => {
name = name.toLowerCase();
name = name.replace(/\ي/g, 'ی');
name = name.replace(/\ك/g, 'ک');
name = name.replace(/(\ـ|\َ|\ِ|\ُ|\ً|\ٍ|\ٌ|\ْ|\ْ)/g, '');
name = name.replace(/^\s+|^0-9+|^۰-۹|[^(آ-ی)(a-z)]+/g, '');
return name;
};
/**
* Detect gender by first name
* @param {String} name
* @return {String}
*/
const getGender = name => {
name = cleanName(name);
if (male[name]) {
return 'MALE';
} else if (female[name]) {
return 'FEMALE';
} else {
return 'UNKNOWN';
}
};
/**
* Detect language of name, like farsi and fingilish
* @param {String} name
* @return {String}
*/
const getLang = name => {
name = cleanName(name);
if (male[name]) {
return male[name].lang;
} else if (female[name]) {
return female[name].lang;
} else {
return 'UNKNOWN';
}
};
module.exports = {
cleanName,
getGender,
getLang,
};