mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 00:55:11 -07:00
Merge pull request #601 from Shopify/safe-to-integer
Use to_integer instead of to_i on arguments
This commit is contained in:
@@ -114,3 +114,7 @@ Style/PerlBackrefs:
|
|||||||
|
|
||||||
Style/WordArray:
|
Style/WordArray:
|
||||||
Enabled: false
|
Enabled: false
|
||||||
|
|
||||||
|
Style/ModuleLength:
|
||||||
|
Exclude:
|
||||||
|
- lib/liquid/standardfilters.rb
|
||||||
|
|||||||
@@ -59,15 +59,17 @@ module Liquid
|
|||||||
# Truncate a string down to x characters
|
# Truncate a string down to x characters
|
||||||
def truncate(input, length = 50, truncate_string = "...".freeze)
|
def truncate(input, length = 50, truncate_string = "...".freeze)
|
||||||
return if input.nil?
|
return if input.nil?
|
||||||
l = length.to_i - truncate_string.length
|
length = to_integer(length)
|
||||||
|
l = length - truncate_string.length
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
input.length > length.to_i ? input[0...l] + truncate_string : input
|
input.length > length ? input[0...l] + truncate_string : input
|
||||||
end
|
end
|
||||||
|
|
||||||
def truncatewords(input, words = 15, truncate_string = "...".freeze)
|
def truncatewords(input, words = 15, truncate_string = "...".freeze)
|
||||||
return if input.nil?
|
return if input.nil?
|
||||||
wordlist = input.to_s.split
|
wordlist = input.to_s.split
|
||||||
l = words.to_i - 1
|
words = to_integer(words)
|
||||||
|
l = words - 1
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input
|
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user