mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
performance: Use split limit in truncatewords (#1361)
This commit is contained in:
committed by
GitHub
parent
efe44a7e6a
commit
dcb5a67089
@@ -89,13 +89,15 @@ module Liquid
|
||||
|
||||
def truncatewords(input, words = 15, truncate_string = "...")
|
||||
return if input.nil?
|
||||
wordlist = input.to_s.split
|
||||
words = Utils.to_integer(words)
|
||||
input = input.to_s
|
||||
words = Utils.to_integer(words)
|
||||
words = 1 if words <= 0
|
||||
|
||||
l = words - 1
|
||||
l = 0 if l < 0
|
||||
wordlist = input.split(" ", words + 1)
|
||||
return input if wordlist.length <= words
|
||||
|
||||
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
|
||||
wordlist.pop
|
||||
wordlist.join(" ").concat(truncate_string.to_s)
|
||||
end
|
||||
|
||||
# Split input string into an array of substrings separated by given pattern.
|
||||
|
||||
Reference in New Issue
Block a user