Skip to content

Commit

Permalink
feat: Allow formatting for Last Name only
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewelsby committed Jun 28, 2024
1 parent 3cee767 commit 856eea6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Support for parsing Gaelic names
- Allow formatting for Last Name only

## [0.1.0] - 2024-06-28
Initial realase
Expand Down
6 changes: 3 additions & 3 deletions lib/gaelic_name_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class GaelicNameFormatter
PREFIXES = %w(Mac Mc Ó O' Nic Bean Mhic Vuic De Gil Gille).freeze
SUFFIXES = %w(ach an àr nam riss aigh aidh ain án éis ín).freeze
PREFIXES = %w[Mac Mc Ó O' Nic Bean Mhic Vuic De Gil Gille].freeze
SUFFIXES = %w[ach an àr nam riss aigh aidh ain án éis ín].freeze

PREFIX_PATTERNS = [
/^(Gill)(An.*)/i,
Expand Down Expand Up @@ -51,7 +51,7 @@ class GaelicNameFormatter
/^(Mac)(Mh.+)([Ìì].+)/i,
/^(Mac)([gdpms]h.*)/i,
/^(Mac)(R[iÌìou].*)/i,
/^(Mac)(R[Ààa][bgiot].*)/i,
/^(Mac)(R[Ààa][bgiot].*)/i
].freeze

attr_accessor :name
Expand Down
16 changes: 10 additions & 6 deletions lib/name_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ class NameFormatter

PARTICLE_REGEX = /^(de[rsl]|d[aiu]|v[oa]n|te[nr]|la|les|y|and|zu|dell[ao])$/i

def parse(name)
def parse(name, last_name_only: false)
return parse_company_name(name) if company_name?(name)

parts = name.strip.split(/\s+/)

prefix = extract_prefix_or_suffix(parts, PREFIXES)
suffix = extract_prefix_or_suffix(parts.reverse, SUFFIXES)

if parts.size > 1
if last_name_only
first_name = nil
last_name = parts.reject { |part| part == suffix }.join(" ")
elsif parts.size > 1

first_name = parts.shift
last_name = parts.reject { |part| part == suffix }.join(" ")
last_name = nil if last_name.empty?
Expand All @@ -54,8 +58,8 @@ def parse(name)
}
end

def parse_formatted(name)
parsed = parse(name)
def parse_formatted(name, **options)
parsed = parse(name, **options)
{
prefix: format_prefix(parsed[:prefix]),
first_name: format_first_name(parsed[:first_name]),
Expand All @@ -64,8 +68,8 @@ def parse_formatted(name)
}
end

def format(name)
parse_formatted(name).values.compact.join(" ")
def format(name, **options)
parse_formatted(name, **options).values.compact.join(" ")
end

private
Expand Down
7 changes: 7 additions & 0 deletions test/name_formatter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def test_edge_cases
assert_formatted "Denesik-Vandervort", "Denesik-Vandervort".upcase
end

def test_last_name_only
formatted = @formatter.format("MacIllDhenby".upcase, last_name_only: true)
assert_equal "MacIllDhenby", formatted
formatted = @formatter.format("MacIllDhenby".upcase, last_name_only: false)
assert_equal "Macilldhenby", formatted
end

def test_stress
skip "Long running test"
100_000.times do
Expand Down

0 comments on commit 856eea6

Please sign in to comment.