Is there a way to match on a integer range? I am looking to strip characters after a certain number of characters, and add a ellipsis. This is what I want to do, but doesn't match on 1..32
.
def cutoff(title) do
case byte_size(title) do
_ -> title
1..32 -> String.slice(title, 1..32) <> " ..."
end
end
There are 2 issues here:
_
) above your number range.1..32
- byte_size
won't return a range , it will return an integer. If you want to check within a range then you must use a guard.If you swap the order of your matches and use a guard then it will work:
def cutoff(title) do
case byte_size(title) do
x when x in 1..32 -> String.slice(title, 1..32) <> " ..."
_ -> title
end
end
You may also want to slice from 0
instead of 1
so the first character doesn't get cut off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With