capitalize-names 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/capitalize_names.rb +1 -1
- data/lib/capitalize_names/capitalizer.rb +42 -41
- data/lib/capitalize_names/errors.rb +3 -10
- data/lib/capitalize_names/version.rb +3 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 952173c8584a6003c41afbb6fed489192388cd33
|
4
|
+
data.tar.gz: 0d041067c287a94e95b37793a937049cf1968dcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13356663ebb5fc5153cecb45dfe0222061bcefe27a21ec4c93c9e5abad001dac72f235215c2ec1927690a4e0904952675096661df6e0b49a96793b03e6238f4e
|
7
|
+
data.tar.gz: 9733023e31a9859ba77b4c4f04e9ff872be83d7cb2c13b4801ecf29c6f705529e7dc4620e10a0a2206075c29b2283794f69ce4a280b8aa78bcf2dbd39d6a1456
|
data/lib/capitalize_names.rb
CHANGED
@@ -2,6 +2,10 @@ module CapitalizeNames
|
|
2
2
|
class Capitalizer
|
3
3
|
attr_accessor :name
|
4
4
|
|
5
|
+
MC = /^Mc(\w)(?=\w)/i
|
6
|
+
MAC = /^Mac(\w)(?=\w)/i
|
7
|
+
O_APOSTROPHE = /^O'(\w)(?=\w)/i
|
8
|
+
|
5
9
|
def initialize(name)
|
6
10
|
@name = name
|
7
11
|
end
|
@@ -14,7 +18,7 @@ module CapitalizeNames
|
|
14
18
|
def capitalize
|
15
19
|
begin
|
16
20
|
capitalize!
|
17
|
-
rescue
|
21
|
+
rescue CapitalizeNames::Errors::GenericError
|
18
22
|
name
|
19
23
|
end
|
20
24
|
end
|
@@ -25,59 +29,56 @@ module CapitalizeNames
|
|
25
29
|
true
|
26
30
|
end
|
27
31
|
|
32
|
+
def surname_suffix_position?(position, name, surname_or_suffix)
|
33
|
+
# surname/suffix must be:
|
34
|
+
# 1. at start of name or after a space or a -
|
35
|
+
# -and-
|
36
|
+
# 2. followed by the end of string or a space or a -
|
37
|
+
(
|
38
|
+
(position == 0) || \
|
39
|
+
(
|
40
|
+
position > 0 && (name[position-1] == ' ' || name[position-1] == '-')
|
41
|
+
)
|
42
|
+
) && (
|
43
|
+
(name.length == position+surname_or_suffix.length) || \
|
44
|
+
(name[position+surname_or_suffix.length] == ' ') || (name[position+surname_or_suffix.length] == '-')
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
28
48
|
def _capitalize
|
29
|
-
|
30
|
-
|
31
|
-
mac = /^Mac(\w)(?=\w)/i
|
32
|
-
oap = /^O'(\w)(?=\w)/i
|
49
|
+
_name = name
|
50
|
+
|
33
51
|
hyphens_index = []
|
34
|
-
|
35
|
-
|
52
|
+
|
53
|
+
while _name.index('-') do
|
54
|
+
index = _name.index('-')
|
36
55
|
hyphens_index << index
|
37
|
-
|
56
|
+
_name = _name[0...index] << ' ' << _name[index+1..-1]
|
38
57
|
end
|
39
58
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
}.map{|w|
|
47
|
-
begin
|
48
|
-
w.gsub(mac, "Mac" + w[3].upcase)
|
49
|
-
rescue
|
50
|
-
w
|
51
|
-
end
|
52
|
-
}.map{|w|
|
53
|
-
begin
|
54
|
-
w.gsub(oap, "O'" + w[2].upcase)
|
55
|
-
rescue
|
56
|
-
w
|
57
|
-
end
|
59
|
+
_name = _name.split.map{ |w|
|
60
|
+
w.mb_chars.capitalize.to_str
|
61
|
+
}.map{ |w|
|
62
|
+
w.gsub(MC) { "Mc#{$1.upcase}" }\
|
63
|
+
.gsub(MAC) { "Mac#{$1.upcase}" }\
|
64
|
+
.gsub(O_APOSTROPHE) { "O'#{$1.upcase}" }
|
58
65
|
}
|
59
66
|
|
60
|
-
|
67
|
+
_name = _name.join(' ')
|
61
68
|
hyphens_index.each do |idx|
|
62
|
-
|
69
|
+
_name = _name[0...idx] << '-' << (_name[idx+1..-1] || "")
|
63
70
|
end
|
64
71
|
|
65
|
-
|
66
|
-
|
72
|
+
_name = _name.gsub("Van ", "van ").gsub("De ", "de ").gsub("Dit ", "dit ")
|
73
|
+
_name << ' '
|
67
74
|
|
68
|
-
(CapitalizeNames::SURNAMES + CapitalizeNames::SUFFIXES).each do |
|
69
|
-
|
70
|
-
if
|
71
|
-
|
72
|
-
# 1. at start of name or after a space or a -
|
73
|
-
# -and-
|
74
|
-
# 2. followed by the end of string or a space or a -
|
75
|
-
if ( ((pos == 0) or (pos > 0 and (nm[pos-1] == ' ' or nm[pos-1] == '-'))) and ((nm.length == pos+surname.length) or (nm[pos+surname.length] == ' ') or (nm[pos+surname.length] == '-')) )
|
76
|
-
nm = nm[0...pos] + surname + nm[pos+surname.length..-1]
|
77
|
-
end
|
75
|
+
(CapitalizeNames::SURNAMES + CapitalizeNames::SUFFIXES).each do |surname_or_suffix|
|
76
|
+
position = _name.downcase.index(surname_or_suffix.downcase)
|
77
|
+
if position and surname_suffix_position?(position, _name, surname_or_suffix)
|
78
|
+
_name = _name[0...position] << surname_or_suffix << _name[position+surname_or_suffix.length..-1]
|
78
79
|
end
|
79
80
|
end
|
80
|
-
|
81
|
+
_name.strip
|
81
82
|
end
|
82
83
|
end
|
83
84
|
end
|
@@ -1,13 +1,6 @@
|
|
1
1
|
module CapitalizeNames
|
2
2
|
module Errors
|
3
|
-
class
|
4
|
-
|
5
|
-
self.constants
|
6
|
-
.map { |c| self.const_get(c) }
|
7
|
-
.select{ |c| c < Exception }
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
class InvalidName < Exception; end;
|
3
|
+
class GenericError < Exception; end
|
4
|
+
class InvalidName < GenericError; end
|
12
5
|
end
|
13
|
-
end
|
6
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capitalize-names
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle Tate
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- lib/capitalize_names/errors.rb
|
50
50
|
- lib/capitalize_names/suffixes.rb
|
51
51
|
- lib/capitalize_names/surnames.rb
|
52
|
+
- lib/capitalize_names/version.rb
|
52
53
|
homepage: http://github.com/infiton/capitalize-names
|
53
54
|
licenses:
|
54
55
|
- MIT
|