Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove widows from blog post titles by adding a non breaking space in Elixir.
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*$/, "&nbsp;" <> List.last(title_list))
else
title_str
end
end
end
IO.inspect Text.widows "hello there world"
# "Hello There&nbsp;World"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment