human_name_parser 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ module HumanNameParser
3
3
  PREFIXES = ['mr', 'ms', 'miss', 'mrs', 'sir', 'prof', 'professor', 'md', 'dr']
4
4
  SUFFIXES = ['esq','esquire','jr','sr','2', 'i', 'ii','iii','iv', 'v', 'phd', 'md', 'do', 'dc', 'dds']
5
5
  LAST_PREFIXES = ['al', 'bar','ben','bin','da','dal','de la', 'de', 'del', 'der', 'di', 'el', 'ibn', 'la', 'le', 'mc', 'san', 'st', 'ste', 'van', 'van der', 'van den', 'vel','von']
6
+ NAME_JOINERS = ['&', 'and']
6
7
 
7
8
  attr_accessor :first, :middle, :last, :prefix, :suffix
8
9
 
@@ -43,7 +44,7 @@ module HumanNameParser
43
44
  self.prefix = @split_name.shift
44
45
  end
45
46
 
46
- return self
47
+ self
47
48
  end
48
49
 
49
50
  def parse_suffix
@@ -53,40 +54,43 @@ module HumanNameParser
53
54
  end
54
55
  self.suffix = self.suffix.join(' ')
55
56
 
56
- return self
57
+ self
57
58
  end
58
59
 
59
60
  def parse_last_name
60
- self.last = []
61
- self.last.unshift @split_name.pop
62
-
63
- while is_last_name_prefix?(@split_name.last)
61
+ if !is_name_joiner? @split_name[-2]
62
+ self.last = []
64
63
  self.last.unshift @split_name.pop
65
- end
66
64
 
67
- self.last = self.last.join(' ')
65
+ while is_last_name_prefix?(@split_name.last)
66
+ self.last.unshift @split_name.pop
67
+ end
68
+
69
+ self.last = self.last.join(' ')
70
+ end
68
71
 
69
- return self
72
+ self
70
73
  end
71
74
 
72
75
  def parse_first_name
73
- self.first = @split_name.shift || ''
76
+ shift_distance = is_name_joiner?(@split_name[1]) ? 3 : 1
77
+ self.first = @split_name.shift(shift_distance).join(' ')
74
78
 
75
- return self
79
+ self
76
80
  end
77
81
 
78
82
  def parse_middle_name
79
83
  # whatever's left
80
84
  self.middle = @split_name.join ' '
81
85
 
82
- return self
86
+ self
83
87
  end
84
88
 
85
89
  def initials
86
90
  _i = ''
87
- _i += self.first.slice(0,1) if self.first && self.first.length > 0
88
- _i += self.middle.slice(0,1) if self.middle && self.middle.length > 0
89
- _i += self.last.slice(0,1) if self.last && self.last.length > 0
91
+ _i += self.first.slice(0,1) if !blank?(self.first)
92
+ _i += self.middle.slice(0,1) if !blank?(self.middle)
93
+ _i += self.last.slice(0,1) if !blank?(self.last)
90
94
  _i.upcase
91
95
  end
92
96
 
@@ -97,7 +101,7 @@ module HumanNameParser
97
101
  self.middle,
98
102
  self.last,
99
103
  self.suffix
100
- ].reject {|n| n.length == 0}.join(' ')
104
+ ].reject( &method(:blank?) ).join(' ')
101
105
  end
102
106
 
103
107
  private
@@ -116,7 +120,7 @@ module HumanNameParser
116
120
  @split_name = split_first_middle_last
117
121
  end
118
122
 
119
- return self
123
+ self
120
124
  end
121
125
 
122
126
  # check whether each part is a suffix
@@ -154,14 +158,20 @@ module HumanNameParser
154
158
  is_ix?(SUFFIXES, string)
155
159
  end
156
160
 
161
+ def is_name_joiner?(string)
162
+ is_ix?(NAME_JOINERS, string)
163
+ end
164
+
157
165
  def is_last_name_prefix?(string)
158
- return false if string.nil? || string == ""
159
- LAST_PREFIXES.any? { |p| string.downcase.match(/^#{p}$/) }
166
+ !blank?(string) && LAST_PREFIXES.include?(string.downcase)
160
167
  end
161
168
 
162
169
  def is_ix?(kind, string)
163
- return false if string.nil? || string == ""
164
- kind.any? {|k| string.downcase.match(/^#{k}\.?$/)}
170
+ !blank?(string) && kind.any? { |k| string =~ /\A#{k}\.?\z/i }
171
+ end
172
+
173
+ def blank?(string)
174
+ string.nil? || string == ''
165
175
  end
166
176
 
167
177
  end
@@ -1,3 +1,3 @@
1
1
  module HumanNameParser
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/spec/name_spec.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'human_name_parser/name'
2
3
 
3
4
  NAMES = [
@@ -9,6 +10,8 @@ NAMES = [
9
10
  ['Marley Mante', ['Marley', '', 'Mante', '', '', 'MM']],
10
11
  ['Marley Mante PhD', ['Marley', '', 'Mante', '', 'PhD', 'MM']],
11
12
  ['Marley Mante, esq.', ['Marley', '', 'Mante', '', 'esq.', 'MM']],
13
+ ['Amy and Brian Smith', ['Amy and Brian', '', 'Smith', '', '', 'AS']],
14
+ ['Amy & Brian', ['Amy & Brian', '', '', '', '', 'A']],
12
15
  ["Björn Charles van der O'Malley", ['Björn', 'Charles', "van der O'Malley", '', '', 'BCV']],
13
16
  ['Alex Rothlenson-ben-Elsburghmohampton', ['Alex', '', 'Rothlenson-ben-Elsburghmohampton', '', '', 'AR']],
14
17
  ['1234 Anywhere St., North Pole, SD 22323', ['', '', '', '', '', '']]
metadata CHANGED
@@ -1,61 +1,55 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: human_name_parser
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Adam Bachman
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-08-07 00:00:00 -04:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-05-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rake
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: rspec
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
39
33
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
47
38
  type: :development
48
- version_requirements: *id002
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
49
46
  description: human_name_parser is intended to split names into their component parts.
50
- email:
47
+ email:
51
48
  - adam.bachman@gmail.com
52
49
  executables: []
53
-
54
50
  extensions: []
55
-
56
51
  extra_rdoc_files: []
57
-
58
- files:
52
+ files:
59
53
  - .gitignore
60
54
  - CHANGELOG.md
61
55
  - Gemfile
@@ -71,41 +65,31 @@ files:
71
65
  - spec/human_name_parser_spec.rb
72
66
  - spec/name_spec.rb
73
67
  - spec/spec_helper.rb
74
- has_rdoc: true
75
68
  homepage: https://github.com/abachman/human_name_parser
76
69
  licenses: []
77
-
78
70
  post_install_message:
79
71
  rdoc_options: []
80
-
81
- require_paths:
72
+ require_paths:
82
73
  - lib
83
- required_ruby_version: !ruby/object:Gem::Requirement
74
+ required_ruby_version: !ruby/object:Gem::Requirement
84
75
  none: false
85
- requirements:
86
- - - ">="
87
- - !ruby/object:Gem::Version
88
- hash: 3
89
- segments:
90
- - 0
91
- version: "0"
92
- required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
81
  none: false
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- hash: 3
98
- segments:
99
- - 0
100
- version: "0"
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
101
86
  requirements: []
102
-
103
87
  rubyforge_project:
104
- rubygems_version: 1.6.2
88
+ rubygems_version: 1.8.24
105
89
  signing_key:
106
90
  specification_version: 3
107
91
  summary: Split most American names into their component parts.
108
- test_files:
92
+ test_files:
109
93
  - spec/human_name_parser_spec.rb
110
94
  - spec/name_spec.rb
111
95
  - spec/spec_helper.rb