Forked from saabdoli/normalize-participant-names.rb
Last active
February 2, 2016 14:23
-
-
Save anthonycrumley/c86905e8a80df7892586 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('mongo') | |
def client | |
@client ||= Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'network-dev') | |
end | |
def participants | |
client[:participants] | |
end | |
participants.find().each do |participant| | |
puts participant | |
numerals = ['i', 'ii', 'iii', 'iv', 'v', 'vi', 'vii', 'viii'] | |
if participant[:firstName] and participant[:lastName] | |
names = [ participant[:firstName], participant[:lastName] ] | |
names.map! do |name| | |
# Handle hyphenated names | |
if name.include? '-' | |
name.split('-').map(&:capitalize).join('-') | |
# Handle split up names | |
elsif name.include? ' ' | |
name_parts = name.split(' ').map(&:capitalize) | |
# Handle numerals | |
name_parts.map! do |name_part| | |
if numerals.include? name_part.downcase | |
name_part.upcase | |
else | |
name_part | |
end | |
end | |
name_parts.join(' ') | |
else | |
name.capitalize | |
end | |
end | |
participants.update_one({ :_id => participant[:_id] }, { "$set" => {:firstName => names[0], :lastName => names[1], :displayName => names[0] + " " + names[1] } }) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked for all my test cases