Skip to content

Commit

Permalink
Merge pull request #39 from mariosant/add-title-case
Browse files Browse the repository at this point in the history
Add title case
  • Loading branch information
sobolevn authored Dec 25, 2018
2 parents 25d9f3e + 691adf4 commit e044204
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
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

0 comments on commit e044204

Please sign in to comment.