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 sentence case #38

Merged
merged 1 commit into from
Dec 24, 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
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