Class: NameFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/name_formatter.rb

Constant Summary collapse

VERSION =
NameFormatterModule::VERSION
PREFIXES =
Set.new(["Mr", "Mrs", "Ms", "Miss", "The Hon", "Rev", "Dr", "Fr", "Pres", "Prof", "Msgr", "Sen", "Gov", "Rep", "Amb"]).freeze
SUFFIXES =
Set.new([
  "Esq", "Jr", "Sr", "III", "II", "I", "V", "IV", "MD", "DC", "DO", "DVM", "LLD", "VM", "DDS", "Ret", "CPA", "JD", "PhD",
  "LLC", "Inc", "Corp", "Ltd", "Co", "LLC", "PLC", "GmbH", "AG", "SA", "SARL", "SRL", "BV", "CV", "NV", "SE", "SC", "SL", "SLL", "SLLC", "SCS", "SCA", "SCRL", "SCA"
]).freeze
NAME_REGEX =
/^(?<prefix>(#{PREFIXES.join("|")}?)\.?)?\s*(?<first_name>[\w-]+)\s+(?<last_name>[\w\s'-]+)\s*(?<suffix>(#{SUFFIXES.join("|")})\.?)?$/ix
COMPANY_SUFFIX_REGEX =
/^(.+)\s+(Inc\.?|Corp\.?|Ltd\.?|LLC|LLP|LP|Limited|Corporation|Company)$/i
FAMILY_BUSINESS_REGEX =
/^(.+)\s+(?:and|&)\s+Sons$/i
MULTIPLE_FAMILY_NAMES_REGEX =
/^[\w']+,\s+[\w']+\s+(?:and|&)\s+[\w']+$/i
DOUBLE_BARRELLED_NAME_REGEX =
/^[\w']+-[\w']+$/
LAW_FIRM_REGEX =
/^(?:[A-Z][a-z]+\s+){2,}(?:LLP|LLC|PC|PLLC)?$/
COMPANY_CO_REGEX =
/^(.+)\s+(?:Company|Co\.)$/i
GROUP_HOLDINGS_REGEX =
/^(.+)\s+(?:Group|Holdings)$/i
GEOGRAPHIC_COMPANY_REGEX =
/^(.+)\s+of\s+[A-Z][a-z]+$/i
TRADING_ENTERPRISES_REGEX =
/^(.+)\s+(?:Trading|Enterprises)$/i
DUNAME_REGEX =
/^Du[b](?=[aeiou])/i
DENAME_REGEX =
/^De[bfghjlmnpvw][aeioulr](?!(?:sik|a)(?:[-,])?$)/i
PARTICLE_REGEX =
/^(de[rsl]|d[aiu]|v[oa]n|te[nr]|la|les|y|and|zu|dell[ao])$/i

Instance Method Summary collapse

Instance Method Details

#format(name) ⇒ Object



71
72
73
# File 'lib/name_formatter.rb', line 71

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

#parse(name, last_name_only: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/name_formatter.rb', line 29

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 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?
  elsif prefix && parts.size == 1
    first_name = nil
    last_name = parts.first
  else
    first_name = parts.first
    last_name = nil
  end

  {
    prefix: prefix,
    first_name: first_name,
    last_name: last_name,
    suffix: suffix
  }
end

#parse_formatted(name) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/name_formatter.rb', line 61

def parse_formatted(name, **)
  parsed = parse(name, **)
  {
    prefix: format_prefix(parsed[:prefix]),
    first_name: format_first_name(parsed[:first_name]),
    last_name: format_last_name(parsed[:last_name]),
    suffix: format_suffix(parsed[:suffix])
  }
end