Skip to content

Commit

Permalink
Merge pull request #38 from mariosant/add-sentence-case
Browse files Browse the repository at this point in the history
Add sentence case
  • Loading branch information
sobolevn authored Dec 24, 2018
2 parents 42acf0a + 188aebb commit 7e74120
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/recase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Recase do
KebabCase,
PascalCase,
PathCase,
SentenceCase,
SnakeCase
}

Expand Down Expand Up @@ -114,4 +115,18 @@ defmodule Recase do
"""
@spec to_dot(String.t) :: String.t
def to_dot(value), do: DotCase.convert(value)

@doc """
Converts string to Sentence case
## Examples
iex> Recase.to_sentence("SomeValue")
"Some value"
iex> Recase.to_sentence("some value")
"Some value"
"""
@spec to_sentence(String.t) :: String.t
def to_sentence(value), do: SentenceCase.convert(value)
end
23 changes: 23 additions & 0 deletions lib/recase/cases/sentence_case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule Recase.SentenceCase do
@moduledoc """
Module to convert strings to `Sentence case`.
This module should not be used directly.
Read about `Sentence case` here:
https://en.wikipedia.org/wiki/Letter_case#Sentence_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.capitalize
end
end
4 changes: 4 additions & 0 deletions test/recase_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ defmodule RecaseTest do
test "should convert to dot.case" do
assert Recase.to_dot("Some Value") == "some.value"
end

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

import Recase.SentenceCase

doctest Recase.SentenceCase

test "should sentence case usual text" do
assert convert("sentence case") == "Sentence case"
assert convert("sentenceCase") == "Sentence case"
assert convert("sentence_Case") == "Sentence case"
assert convert("sentenceCase") == "Sentence case"
assert convert("sentence.Case") == "Sentence case"
assert convert("Sentence case") == "Sentence case"
assert convert("--sentence-case--") == "Sentence case"
assert convert("sentence#case") == "Sentence case"
assert convert("sentence?!case") == "Sentence case"
end

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

0 comments on commit 7e74120

Please sign in to comment.