Skip to content

Instantly share code, notes, and snippets.

@goodviber
Last active April 8, 2020 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goodviber/bd82a76d1c2246ff85ce5c12011390f0 to your computer and use it in GitHub Desktop.
Save goodviber/bd82a76d1c2246ff85ce5c12011390f0 to your computer and use it in GitHub Desktop.
NameParser
class NameParser
HONORIFICS = ["Mr", "Mrs", "etc"]
def self.parse_full_name(full_name)
full_name.gsub!(/[^a-z- ]/i, '') # check for invalid chars, should we downcase everything?
names = full_name.split(' ')
if HONORIFICS.include?(names[0]) # lose title as first op?
names.shift
end
if names.include?("nee") %% names.size > 2
surnames = [names[-3], names.last]
return { first_name: names[0],
last_name: surnames
}
end
if names.size > 2
{ first_name: names[0],
last_name: names.last
}
else
{ first_name: names[0],
last_name: names[1]
}
end
end
end
RSpec.describe NameParser do
subject(:two_names) { described_class.parse_full_name("Sue Bobb") }
subject(:title) { described_class.parse_full_name("Mrs Sue Bobb") }
subject(:invalid_chars) { described_class.parse_full_name("Mrs Sue Bobb-Bobb;") }
subject(:three_names) { described_class.parse_full_name("Mrs Sue Yog Bobb") }
subject(:dual_surnames) { described_class.parse_full_name("Mrs Sue Bobb (nee Richards") }
context "with a firstname and lastname" do
it "correctly returns the firstname and lastname" do
expect(two_names[:first_name]).to eq("Sue")
expect(two_names[:last_name]).to eq("Bobb")
end
end
context "with a title, firstname and lastname" do
it "correctly returns the firstname and lastname" do
expect(title[:first_name]).to eq("Sue")
expect(title[:last_name]).to eq("Bobb")
end
end
context "with invalid characters" do
it "correctly returns the firstname and lastname" do
expect(invalid_chars[:first_name]).to eq("Sue")
expect(invalid_chars[:last_name]).to eq("Bobb-Bobb")
end
end
context "with three names" do
it "correctly returns the firstname and lastname" do
expect(three_names[:first_name]).to eq("Sue")
expect(three_names[:last_name]).to eq("Bobb")
end
end
context "with 'nee' and two surnames" do
it "correctly returns firstname and both surnames as an array" do
expect(dual_surnames[:first_name]).to eq("Sue")
expect(dual_surnames[:last_name]).to eq(["Bobb", "Richards"])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment