-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from mariosant/add-title-case
Add title case
- Loading branch information
Showing
6 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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 |
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
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
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 |