Created
December 23, 2015 22:38
-
-
Save stephenhowells/6242296699a3d86f6f3b to your computer and use it in GitHub Desktop.
Remove widows from blog post titles by adding a non breaking space in Elixir.
This file contains hidden or 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
defmodule Text do | |
def widows(some_str) do | |
title_list = some_str |> String.split(" ") |> Enum.map(&String.capitalize/1) | |
title_str = Enum.join(title_list, " ") | |
if Enum.count(title_list) > 2 do | |
String.replace(title_str, ~r/\s+\S*$/, " " <> List.last(title_list)) | |
else | |
title_str | |
end | |
end | |
end | |
IO.inspect Text.widows "hello there world" | |
# "Hello There World" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment