Current section

Files

Jump to
phoenix_html_simplified_helpers lib phoenix_html_simplified_helpers truncate.ex
Raw

lib/phoenix_html_simplified_helpers/truncate.ex

defmodule Phoenix.HTML.SimplifiedHelpers.Truncate do
def truncate(text, options \\ []) do
len = options[:length] || 30
omi = options[:omission] || "..."
case String.length(text) do
x when x < len ->
text
_ ->
len_with_omi = len - String.length(omi)
stop =
if options[:separator] do
rindex(text, options[:separator]) || len_with_omi
else
len_with_omi
end
"#{String.slice(text, 0, stop)}#{omi}"
end
end
defp rindex(text, str) do
{index, _} =
text
|> String.reverse
|> :binary.match(str)
String.length(text) - index - 1
end
end