Skip to content

Commit

Permalink
Bug fix for Strings.truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
dkln committed May 2, 2024
1 parent 7ddb962 commit a8dfe93
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/wuunder_utils/strings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,21 @@ defmodule WuunderUtils.Strings do
iex> WuunderUtils.Strings.truncate("this is a long string", 30)
"this is a long string"
iex> WuunderUtils.Strings.truncate("this is a long string", 21)
"this is a long string"
iex> WuunderUtils.Strings.truncate("this is a long string", 20)
"this is a long st..."
iex> WuunderUtils.Strings.truncate("this is a long string", 10)
"this is ..."
"this is..."
"""
@spec truncate(String.t(), integer()) :: String.t()
def truncate(value, max_length)
when is_binary(value) and is_integer(max_length) and max_length > 3 do
if String.length(value) > max_length - 3 do
String.slice(value, 0..(max_length - 3)) <> "..."
if String.length(value) > max_length do
String.slice(value, 0..(max_length - 4)) <> "..."
else
value
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule WuunderUtils.MixProject do
def project do
[
app: :wuunder_utils,
version: "0.2.3",
version: "0.2.4",
elixir: "~> 1.14",
organization: "wuunder",
name: "Wuunder Utils",
Expand Down

0 comments on commit a8dfe93

Please sign in to comment.