Skip to content

Commit

Permalink
Merge pull request #16 from infiton/v1.2
Browse files Browse the repository at this point in the history
V1.2
  • Loading branch information
infiton authored Jul 4, 2022
2 parents e24222a + 61afa58 commit 5ce0a7e
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 144 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.gem
pkg
Gemfile.lock
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inherit_gem:
rubocop-shopify: rubocop.yml
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'https://rubygems.org'
# frozen_string_literal: true

gemspec
source "https://rubygems.org"

gemspec
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Circle CI](https://circleci.com/gh/infiton/capitalize-names.svg?style=shield)](https://circleci.com/gh/infiton/capitalize-names)
# capitalize-names
Simple proper name capitalization that handles edge cases. Based off of http://dzone.com/snippets/capitalize-proper-names
Simple proper name capitalization that handles edge cases. Originally based off of http://dzone.com/snippets/capitalize-proper-names

## Installation
```
Expand All @@ -9,12 +9,56 @@ gem install capitalize-names

## Quick Start

```
```ruby
require 'capitalize_names'

CapitalizeNames.capitalize("TATE") # => "Tate"
CapitalizeNames.capitalize("JoHn O'NEILL") # => "John O'Neill"
CapitalizeNames.capitalize("macarthur") # => "MacArthur"
CapitalizeNames.capitalize("rick johnson-smith") # => "Rick Johnson-Smith"
CapitalizeNames.capitalize("bob jones, iii") # => "Bob Jones, III"
```

## Name Formats

`CapitalizeNames` uses different rules depending on whether the name being capitalized is thought to be a given name or a surname. For given names the only difference from [Ruby's capitalize](https://apidock.com/ruby/String/capitalize) is that they will be capitalized around hyphens:

```ruby
CapitalizeNames.capitalize("jean-louis", format: :firstname) # "Jean-Louis"
```

For surnames the capitalization will also be done across hyphens however a series of further rules are applied:

- If the name is included in the [list](https://github.com/infiton/capitalize-names/blob/master/lib/capitalize_names/surnames.rb) of `CapitalizeNames::SURNAMES` it will be capitalized as it is in the list.
- Otherwise a series of rules are applied each of which can be skipped:
- The letter after an initial "Mc" will be capitalized (to skip pass `skip_mc: true`).
- The letter after an initial "Mac" will be capitalized (to skip pass `skip_mac: true`).
- The letter after an initial "O'" will be capitalized (to skip pass `skip_o_apostrophe: true`).
- An initial "van " will be downcased (to skip pass `skip_van_space: true`).
- An initial "dit " will be downcased (to skip pass `skip_dit_space: true`).
- An initial "de la " will be downcased (to skip pass `skip_de_la_space: true`).
- An initial "de " will be downcased (to skip pass `skip_de_space: true`).

By default `CapitalizeNames` will use `format: :fullname` which will use surname rules on the last name in the string being capitalized (excluding suffixes) and given name rules on all other names. If there is only one name, `format: :fullname` will format it as a surname. To format all names as given names pass `format: :givenname` or `format: :firstname`. To format all names as surnames pass `format: :surname` or `format: :lastname`.

Examples:

```ruby
CapitalizeNames.capitalize("macarthur macarthur") # => "Macarthur MacArthur"
CapitalizeNames.capitalize("macarthur macarthur", format: :fullname) # => "Macarthur MacArthur"
CapitalizeNames.capitalize("macarthur macarthur", format: :surname) # => "MacArthur MacArthur"
CapitalizeNames.capitalize("macarthur macarthur", format: :givenname) # => "Macarthur Macarthur"
CapitalizeNames.capitalize("macarthur macarthur", skip_mac: true) # => "Macarthur Macarthur"
```

## Exceptions

`CapitalizeNames` comes with two methods `capitalize!` which will raise an exception if the input or options are invalid and `capitalize` which will return the input without doing anything if the input or options are invalid:

```ruby
CapitalizeNames.capitalize!(nil) # => raises an exception
CapitalizeNames.capitalize(nil) # => nil

CapitalizeNames.capitalize!("bob sacamano", format: :junk) # => raises an exception
CapitalizeNames.capitalize("bob sacamano", format: :junk) # => "bob sacamano"
```
10 changes: 6 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'bundler/gem_tasks'
require 'rake/testtask'
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new(:test) do |test|
test.pattern = 'test/test_*.rb'
test.pattern = "test/test_*.rb"
end

desc "Run tests"
task :default => :test
task default: :test
28 changes: 15 additions & 13 deletions capitalize_names.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/capitalize_names/version', __FILE__)
# frozen_string_literal: true

require File.expand_path("../lib/capitalize_names/version", __FILE__)

Gem::Specification.new do |s|
s.name = 'capitalize-names'
s.name = "capitalize-names"
s.version = CapitalizeNames::VERSION
s.date = '2017-09-20'
s.summary = 'Capitalizes names; handles edge cases.'
s.description = 'A simple gem to capitalize names, based off of: http://dzone.com/snippets/capitalize-proper-names'
s.authors = ['Kyle Tate']
s.email = '[email protected]'
s.summary = "Capitalizes names; handles edge cases."
s.description = "A simple gem to capitalize names, based off of: http://dzone.com/snippets/capitalize-proper-names"
s.authors = ["Kyle Tate"]
s.email = "[email protected]"
s.files = Dir.glob("{lib}/**/*")

s.required_ruby_version = '> 2.0'
s.add_runtime_dependency "activesupport", [">= 3"]
s.add_development_dependency 'rake'
s.add_development_dependency 'minitest'
s.homepage = 'http://github.com/infiton/capitalize-names'
s.license = 'MIT'
s.required_ruby_version = ">= 2.4"
s.add_development_dependency("minitest")
s.add_development_dependency("rake")
s.add_development_dependency("rubocop")
s.add_development_dependency("rubocop-shopify")
s.homepage = "http://github.com/infiton/capitalize-names"
s.license = "MIT"
end
25 changes: 12 additions & 13 deletions lib/capitalize_names.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#based on http://dzone.com/snippets/capitalize-proper-names
require 'capitalize_names/errors'
require 'capitalize_names/capitalizer'
require 'capitalize_names/suffixes'
require 'capitalize_names/surnames'
require 'active_support/core_ext/string/multibyte'
# frozen_string_literal: true

module CapitalizeNames
# originally based on http://dzone.com/snippets/capitalize-proper-names
require "capitalize_names/errors"
require "capitalize_names/suffixes"
require "capitalize_names/surnames"
require "capitalize_names/capitalizer"

module CapitalizeNames
class << self

def capitalize!(name)
Capitalizer.new(name).capitalize!
def capitalize!(name, options = {})
Capitalizer.new(name, options).capitalize!
end

def capitalize(name)
Capitalizer.new(name).capitalize
def capitalize(name, options = {})
Capitalizer.new(name, options).capitalize
end
end
end
end
Loading

0 comments on commit 5ce0a7e

Please sign in to comment.