forked from joshfraser/JavaScript-Name-Parser
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnameparser.js
More file actions
Latest commit
194 lines (177 loc) · 6.25 KB
/
Copy pathnameparser.js
File metadata and controls
194 lines (177 loc) · 6.25 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// based on PHP Name Parser by Josh Fraser (joshfraser.com)
// http://www.onlineaspect.com/2009/08/17/splitting-names/
// ported to JavaScript by Mark Pemburn (pemburnia.com)
// released under Apache 2.0 license
//forked from https://github.com/joshfraser/JavaScript-Name-Parser
//current source now at https://github.com/mchotin/JavaScript-Name-Parser
functionNameParser(){}
// split full names into the following parts:
// - prefix / salutation (Mr., Mrs., etc)
// - given name / first name
// - middle initials
// - surname / last name
// - suffix (II, Phd, Jr, etc)
NameParser.prototype.parse=function(fullname){
fullname=fullname.trim();
// split into words
varunfilteredNameParts=fullname.split(" ");
varname={};
varnameParts=[];
varlastName="";
varfirstName="";
varinitials="";
varj=0;
vari=0;
// completely ignore any words in parentheses
for(i=0;i<unfilteredNameParts.length;i++){
if(unfilteredNameParts[i].indexOf("(")==-1){
nameParts[j++]=unfilteredNameParts[i];
}
}
varnumWords=nameParts.length;
//if somehow we ended up finding no real parts go ahead and
//just use the whole string and hopefully it will parse into something
if(numWords==0)nameParts=unfilteredNameParts;
// is the first word a title? (Mr. Mrs, etc)
varsalutation=this.is_salutation(nameParts[0]);
varsuffix=this.is_suffix(nameParts[nameParts.length-1]);
// set the range for the middle part of the name (trim prefixes &
// suffixes)
varstart=(salutation) ? 1 : 0;
varend=(suffix) ? numWords-1 : numWords;
// concat the first name
for(i=start;i<(end-1);i++){
varword=nameParts[i];
// move on to parsing the last name if we find an indicator of a
// compound last name (Von, Van, etc)
// we use i != start to allow for rare cases where an indicator is
// actually the first name (like "Von Fabella")
if(this.is_compound_lastName(word)&&i!=start){
break;
}
// is it a middle initial or part of their first name?
// if we start off with an initial, we'll call it the first name
if(this.is_initial(word)){
// is the initial the first word?
if(i==start){
// if so, do a look-ahead to see if they go by their middle
// name
// for ex: "R. Jason Smith" => "Jason Smith" & "R." is
// stored as an initial
// but "R. J. Smith" => "R. Smith" and "J." is stored as
// an initial
if(this.is_initial(nameParts[i+1])){
firstName+=" "+word.toUpperCase();
}else{
initials+=" "+word.toUpperCase();
}
// otherwise, just go ahead and save the initial
}else{
initials+=" "+word.toUpperCase();
}
}else{
firstName+=" "+this.fix_case(word);
}
}
// check that we have more than 1 word in our string
if((end-start)>1){
// concat the last name
for(j=i;j<end;j++){
lastName+=" "+this.fix_case(nameParts[j]);
}
}else{
// otherwise, single word strings are assumed to be first names
firstName=this.fix_case(nameParts[i]);
}
// return the various parts in an array
name.salutation=(salutation!=false) ? salutation : "";
name.firstName=(firstName!="") ? firstName.trim() : "";
name.initials=(initials!="") ? initials.trim() : "";
name.lastName=(lastName!="") ? lastName.trim() : "";
name.suffix=(suffix!=false) ? suffix : "";
returnname;
};
// detect and format standard salutations
// I'm only considering english honorifics for now & not words like
NameParser.prototype.is_salutation=function(word){
// ignore periods
word=word.replace(".","").toLowerCase();
// returns normalized values
if(word=="mr"||word=="master"||word=="mister"){
return"Mr.";
}elseif(word=="mrs"){
return"Mrs.";
}elseif(word=="miss"||word=="ms"){
return"Ms.";
}elseif(word=="dr"){
return"Dr.";
}elseif(word=="rev"){
return"Rev.";
}elseif(word=="fr"){
return"Fr.";
}else{
returnfalse;
}
};
// detect and format common suffixes
NameParser.prototype.is_suffix=function(word){
// ignore periods
word=word.replace(/\./g,"").toLowerCase();
// these are some common suffixes - what am I missing?
varsuffixArray=['I','II','III','IV','V','Senior','Junior','Jr','Sr',
'PhD','APR','RPh','PE','MD','MA','DMD','CME'];
for(vari=0;i<suffixArray.length;i++){
if(suffixArray[i].toLowerCase()==word){
returnsuffixArray[i];
}
}
returnfalse;
};
// detect compound last names like "Von Fange"
NameParser.prototype.is_compound_lastName=function(word){
word=word.toLowerCase();
// these are some common prefixes that identify a compound last names - what am I missing?
varwords=['vere','von','van','de','del','della','di','da','pietro',
'vanden','du','st.','st','la','lo','ter'];
returnwords.indexOf(word)>-1;
};
// single letter, possibly followed by a period
NameParser.prototype.is_initial=function(word){
// ignore periods
word=word.replace(".","");
return(word.length==1);
};
// detect mixed case words like "McDonald"
// returns false if the string is all one case
NameParser.prototype.is_camel_case=function(word){
varucReg=/[A-Z]+/;
varlcReg=/[a-z]+/;
returnucReg.test(word)&&lcReg.test(word);
};
// ucfirst words split by dashes or periods
// ucfirst all upper/lower strings, but leave camelcase words alone
NameParser.prototype.fix_case=function(word){
// uppercase words split by dashes, like "Kimura-Fay"
word=this.safe_ucfirst("-",word);
// uppercase words split by periods, like "J.P."
word=this.safe_ucfirst(".",word);
returnword;
};
// helper for NameParser.prototype.fix_case
NameParser.prototype.safe_ucfirst=function(seperator,word){
varwords=[];
// uppercase words split by the seperator (ex. dashes or periods)
varparts=word.split(seperator);
for(vari=0;i<parts.length;i++){
varthisWord=parts[i];
words[i]=(this.is_camel_case(thisWord))
? thisWord
: this.ucfirst(thisWord);
}
returnwords.join(seperator);
};
NameParser.prototype.ucfirst=function(word){
returnword.substr(0,1).toUpperCase()+
word.substr(1,word.length-1).toLowerCase();
}
module.exports=NameParser;