33// ported to JavaScript by Mark Pemburn (pemburnia.com)
44// released under Apache 2.0 license
55
6- Array . prototype . in_array = function ( value ) {
7- for ( var i = 0 ; i < this . length ; i ++ ) {
8- if ( this [ i ] == value ) {
9- return true ;
10- }
11- }
12- return false ;
13- } ;
146if ( ! String . prototype . trim ) {
157 String . prototype . trim = function ( ) {
168 return this . replace ( / ^ \s + | \s + $ / gm, '' ) ;
@@ -30,20 +22,20 @@ function NameParse() {}
3022 // - suffix (II, Phd, Jr, etc)
3123NameParse . parse = function ( fullastName ) {
3224 fullastName = fullastName . trim ( ) ;
33- // split into words
34- var unfilteredNameParts = fullastName . split ( " " ) ;
25+
3526 var nameParts = [ ] ;
3627 var lastName = "" ;
3728 var firstName = "" ;
3829 var initials = "" ;
3930 var j = 0 ;
4031 var i = 0 ;
32+
33+ // split into words
4134 // completely ignore any words in parentheses
42- for ( i = 0 ; i < unfilteredNameParts . length ; i ++ ) {
43- if ( unfilteredNameParts [ i ] . indexOf ( "(" ) == - 1 ) {
44- nameParts [ j ++ ] = unfilteredNameParts [ i ] ;
45- }
46- }
35+ nameParts = fullastName . split ( " " ) . filter ( function ( namePart ) {
36+ return ( namePart . indexOf ( "(" ) === - 1 ) ;
37+ } ) ;
38+
4739 var numWords = nameParts . length ;
4840 // is the first word a title? (Mr. Mrs, etc)
4941 var salutation = this . is_salutation ( nameParts [ 0 ] ) ;
@@ -132,20 +124,24 @@ NameParse.is_suffix = function (word) {
132124 word = word . replace ( / \. / g, "" ) . toLowerCase ( ) ;
133125 // these are some common suffixes - what am I missing?
134126 var suffixArray = [ 'I' , 'II' , 'III' , 'IV' , 'V' , 'Senior' , 'Junior' , 'Jr' , 'Sr' , 'PhD' , 'APR' , 'RPh' , 'PE' , 'MD' , 'MA' , 'DMD' , 'CME' ] ;
135- for ( var i = 0 ; i < suffixArray . length ; i ++ ) {
136- if ( suffixArray [ i ] . toLowerCase ( ) == word ) {
137- return suffixArray [ i ] ;
138- }
127+
128+ var suffixIndex = suffixArray . map ( function ( suffix ) {
129+ return suffix . toLowerCase ( ) ;
130+ } ) . indexOf ( word ) ;
131+
132+ if ( suffixIndex >= 0 ) {
133+ return suffixArray [ suffixIndex ] ;
134+ } else {
135+ return false ;
139136 }
140- return false ;
141137} ;
142138
143139// detect compound last names like "Von Fange"
144140NameParse . is_compound_lastName = function ( word ) {
145141 word = word . toLowerCase ( ) ;
146142 // these are some common prefixes that identify a compound last names - what am I missing?
147143 var words = [ 'vere' , 'von' , 'van' , 'de' , 'del' , 'della' , 'di' , 'da' , 'pietro' , 'vanden' , 'du' , 'st.' , 'st' , 'la' , 'lo' , 'ter' ] ;
148- return words . in_array ( word ) ;
144+ return words . indexOf ( word ) >= 0 ;
149145} ;
150146
151147// single letter, possibly followed by a period
@@ -175,14 +171,10 @@ NameParse.fix_case = function (word) {
175171 return word ;
176172} ;
177173
178- // helper this.for fix_case
174+ // helper for this.fix_case
175+ // uppercase words split by the seperator (ex. dashes or periods)
179176NameParse . safe_ucfirst = function ( seperator , word ) {
180- var words = [ ] ;
181- // uppercase words split by the seperator (ex. dashes or periods)
182- parts = word . split ( seperator ) ;
183- for ( var i = 0 ; i < parts . length ; i ++ ) {
184- var thisWord = parts [ i ] ;
185- words [ i ] = ( this . is_camel_case ( thisWord ) ) ? thisWord : thisWord . ucfirst ( ) ;
186- }
187- return words . join ( seperator ) ;
177+ return word . split ( seperator ) . map ( function ( thisWord ) {
178+ return this . is_camel_case ( thisWord ) ? thisWord : thisWord . ucfirst ( ) ;
179+ } , this ) . join ( seperator ) ;
188180} ;
0 commit comments