capitalize-names 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 75fa2857650e6d84b88143ebac713f71b98f3144
4
- data.tar.gz: 376d9a6c349ca0f8537ada0a22ab07106eab87b5
2
+ SHA256:
3
+ metadata.gz: a8e58cd8d2216549bc0bd47a2f3ce851abbcf8eed7e404a84ec43593b81fe959
4
+ data.tar.gz: 77f0ce726f15d3ca5e793be5485c282b71fac70a9b7bd01df4d0c648fcb4e66b
5
5
  SHA512:
6
- metadata.gz: fb7ed73dac87a16ffa610e8aae8ccbd7c40a1095d337028471c9d0a538e1a49ef7165e71f7c33e7a4dfcb961865d665edb00cf6eedc60db17ad44d232c28acb5
7
- data.tar.gz: d26ff4306c8f47912d8038276c82e4f8418b25f07fdc527727b0fccbb56bda63995aab13881df7a73620ff05f12d13cc81b86c5c7b7c51af6e72313474182029
6
+ metadata.gz: e6dd722c7e906cef3d58c203da2a52d2274ddc115dd902169b02f6f80c34f3ff81c2484a3f86ca11a6cf41f0ffabad3a3dbe48911daa2b899dc539cf0e0c4738
7
+ data.tar.gz: f438bda12d9d4eac554b297f9e4044f4c7663197e32ed355fcb2701a1d1048252088057f82b19c2c8b3838bfe64468e18857251e7f62d872065333dd615cbf7f
@@ -3,7 +3,7 @@ require 'capitalize_names/errors'
3
3
  require 'capitalize_names/capitalizer'
4
4
  require 'capitalize_names/suffixes'
5
5
  require 'capitalize_names/surnames'
6
- require 'active_support/all'
6
+ require 'active_support/core_ext/string/multibyte'
7
7
 
8
8
  module CapitalizeNames
9
9
 
@@ -2,6 +2,12 @@ module CapitalizeNames
2
2
  class Capitalizer
3
3
  attr_accessor :name
4
4
 
5
+ HYPHEN = /(\s*-\s*)/
6
+
7
+ MC = /^Mc(\w)(?=\w)/i
8
+ MAC = /^Mac(\w)(?=\w)/i
9
+ O_APOSTROPHE = /^O'(\w)(?=\w)/i
10
+
5
11
  def initialize(name)
6
12
  @name = name
7
13
  end
@@ -14,7 +20,7 @@ module CapitalizeNames
14
20
  def capitalize
15
21
  begin
16
22
  capitalize!
17
- rescue *CapitalizeNames::Errors.all_exceptions
23
+ rescue CapitalizeNames::Errors::GenericError
18
24
  name
19
25
  end
20
26
  end
@@ -25,59 +31,70 @@ module CapitalizeNames
25
31
  true
26
32
  end
27
33
 
34
+ def surname_suffix_position?(position, name, surname_or_suffix)
35
+ # surname/suffix must be:
36
+ # 1. at start of name or after a space or a -
37
+ # -and-
38
+ # 2. followed by the end of string or a space or a -
39
+ (
40
+ (position == 0) || \
41
+ (
42
+ position > 0 && (name[position-1] == ' ' || name[position-1] == '-')
43
+ )
44
+ ) && (
45
+ (name.length == position+surname_or_suffix.length) || \
46
+ (name[position+surname_or_suffix.length] == ' ') || (name[position+surname_or_suffix.length] == '-')
47
+ )
48
+ end
49
+
28
50
  def _capitalize
29
- nm = name.mb_chars
30
- mc = /^Mc(\w)(?=\w)/i
31
- mac = /^Mac(\w)(?=\w)/i
32
- oap = /^O'(\w)(?=\w)/i
33
- hyphens_index = []
34
- while nm.index('-') do
35
- index = nm.index('-')
36
- hyphens_index << index
37
- nm = nm[0...index] + ' ' + nm[index+1..-1]
38
- end
51
+ _name = name
39
52
 
40
- nm = nm.split.map{|w| w.capitalize}.map{ |w|
41
- begin
42
- w.gsub(mc, "Mc" + w[2].upcase)
43
- rescue
44
- w
45
- end
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
53
+ hyphens = []
54
+
55
+ while (match = _name.match(HYPHEN)) do
56
+ _start = match.begin(1)
57
+ _end = match.end(1)
58
+ _value = match[1]
59
+
60
+ if _start == 0
61
+ _name = _name[_end..-1]
62
+ else
63
+ _name = _name[0..._start] << ' ' << _name[_end..-1]
57
64
  end
65
+
66
+ hyphens << [_start, _end, _value]
67
+ end
68
+
69
+ _name = _name.split.map{ |w|
70
+ w.mb_chars.capitalize.to_str
71
+ }.map{ |w|
72
+ w.gsub(MC) { "Mc#{$1.upcase}" }\
73
+ .gsub(MAC) { "Mac#{$1.upcase}" }\
74
+ .gsub(O_APOSTROPHE) { "O'#{$1.upcase}" }
58
75
  }
59
76
 
60
- nm = nm.join(' ')
61
- hyphens_index.each do |idx|
62
- nm = nm[0...idx] + '-' + nm[idx+1..-1]
77
+ _name = _name.join(" ")
78
+
79
+ hyphens.reverse.each do |_start, _end, _value|
80
+ if _start == 0
81
+ _name = _value << _name
82
+ else
83
+ _name = _name[0..._start] << _value << (_name[_start+1..-1] || "")
84
+ end
63
85
  end
64
86
 
65
- nm = nm.gsub("Van ", "van ").gsub("De ", "de ").gsub("Dit ", "dit ")
66
- nm += ' '
67
-
68
- (CapitalizeNames::SURNAMES + CapitalizeNames::SUFFIXES).each do |surname|
69
- pos = nm.downcase.index(surname.downcase)
70
- if pos
71
- # surname/suffix must be:
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
87
+ _name = _name.gsub("Van ", "van ").gsub("De ", "de ").gsub("Dit ", "dit ")
88
+ _name << " "
89
+
90
+ (CapitalizeNames::SURNAMES + CapitalizeNames::SUFFIXES).each do |surname_or_suffix|
91
+ position = _name.downcase.index(surname_or_suffix.downcase)
92
+ if position and surname_suffix_position?(position, _name, surname_or_suffix)
93
+ _name = _name[0...position] << surname_or_suffix << _name[position+surname_or_suffix.length..-1]
78
94
  end
79
95
  end
80
- nm.strip
96
+
97
+ _name[0...-1]
81
98
  end
82
99
  end
83
100
  end
@@ -1,13 +1,6 @@
1
1
  module CapitalizeNames
2
2
  module Errors
3
- class << self
4
- def all_exceptions
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
@@ -0,0 +1,3 @@
1
+ module CapitalizeNames
2
+ VERSION = "1.1.0"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capitalize-names
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Tate
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-17 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,20 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: minitest
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: 5.8.4
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: 5.8.4
54
+ version: '0'
41
55
  description: 'A simple gem to capitalize names, based off of: http://dzone.com/snippets/capitalize-proper-names'
42
56
  email: kbt.tate@gmail.com
43
57
  executables: []
@@ -49,6 +63,7 @@ files:
49
63
  - lib/capitalize_names/errors.rb
50
64
  - lib/capitalize_names/suffixes.rb
51
65
  - lib/capitalize_names/surnames.rb
66
+ - lib/capitalize_names/version.rb
52
67
  homepage: http://github.com/infiton/capitalize-names
53
68
  licenses:
54
69
  - MIT
@@ -59,7 +74,7 @@ require_paths:
59
74
  - lib
60
75
  required_ruby_version: !ruby/object:Gem::Requirement
61
76
  requirements:
62
- - - "~>"
77
+ - - ">"
63
78
  - !ruby/object:Gem::Version
64
79
  version: '2.0'
65
80
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -69,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
84
  version: '0'
70
85
  requirements: []
71
86
  rubyforge_project:
72
- rubygems_version: 2.4.5.1
87
+ rubygems_version: 2.7.6
73
88
  signing_key:
74
89
  specification_version: 4
75
90
  summary: Capitalizes names; handles edge cases.