Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add title case #39

Merged
merged 3 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Adds `sentence_case`, see [#38](https://github.com/sobolevn/recase/pull/38)
- Adds `title_case`, see [#38](https://github.com/sobolevn/recase/pull/39)

### Misc

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ Recase.to_sentence("SomeValue") # => "Some value"
Recase.to_sentence("some value") # => "Some value"
```

### Title

Title case applies a "Title Style" to all words in a sentence.


```elixir
Recase.to_title("some-value") # => "Some Value"
Recase.to_title("some value") # => "Some Value"
```

## Changelog

Expand Down
17 changes: 16 additions & 1 deletion lib/recase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ defmodule Recase do
PascalCase,
PathCase,
SentenceCase,
SnakeCase
SnakeCase,
TitleCase
}

@doc """
Expand Down Expand Up @@ -129,4 +130,18 @@ defmodule Recase do
"""
@spec to_sentence(String.t) :: String.t
def to_sentence(value), do: SentenceCase.convert(value)

@doc """
Converts string to Title Case

## Examples

iex> Recase.to_title("SomeValue")
"Some Value"

iex> Recase.to_title("some value")
"Some Value"
"""
@spec to_title(String.t) :: String.t
def to_title(value), do: TitleCase.convert(value)
end
25 changes: 25 additions & 0 deletions lib/recase/cases/title_case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Recase.TitleCase do
@moduledoc """
Module to convert strings to `Title Case`.

This module should not be used directly.

Read about `Title Case` here:
https://en.wikipedia.org/wiki/Letter_case#Title_case
"""

alias Recase.SnakeCase

@sep " "

@spec convert(String.t) :: String.t
def convert(""), do: ""
def convert(value) do
value
|> SnakeCase.convert
|> String.replace("_", @sep)
|> String.split()
|> Enum.map(&(String.capitalize(&1)))
|> Enum.join(@sep)
end
end
4 changes: 4 additions & 0 deletions test/recase_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ defmodule RecaseTest do
test "should convert to Sentence case" do
assert Recase.to_sentence("Some Value") == "Some value"
end

test "should convert to Title Case" do
assert Recase.to_title("Some Value") == "Some Value"
end
end
23 changes: 23 additions & 0 deletions test/recase_test/title_case_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Recase.TitleCaseTest do
use ExUnit.Case

import Recase.TitleCase

doctest Recase.TitleCase

test "should title case usual text" do
assert convert("title case") == "Title Case"
assert convert("titleCase") == "Title Case"
assert convert("title_Case") == "Title Case"
assert convert("titleCase") == "Title Case"
assert convert("title.Case") == "Title Case"
assert convert("title case") == "Title Case"
assert convert("--title-case--") == "Title Case"
assert convert("title#case") == "Title Case"
assert convert("title?!case") == "Title Case"
end

test "should return empty string" do
assert convert("") == ""
end
end