Merge pull request #5679 from jekyll/liquid-4-utils

[liquid 4] Utils
This commit is contained in:
Parker Moore 2016-12-28 21:55:55 -08:00 committed by GitHub
commit 02056f7b15
1 changed files with 13 additions and 18 deletions

View File

@ -280,10 +280,11 @@ module Jekyll
end end
end end
def pop(array, input = 1) def pop(array, num = 1)
return array unless array.is_a?(Array) return array unless array.is_a?(Array)
num = Liquid::Utils.to_integer(num)
new_ary = array.dup new_ary = array.dup
new_ary.pop(input.to_i || 1) new_ary.pop(num)
new_ary new_ary
end end
@ -294,10 +295,11 @@ module Jekyll
new_ary new_ary
end end
def shift(array, input = 1) def shift(array, num = 1)
return array unless array.is_a?(Array) return array unless array.is_a?(Array)
num = Liquid::Utils.to_integer(num)
new_ary = array.dup new_ary = array.dup
new_ary.shift(input.to_i || 1) new_ary.shift(num)
new_ary new_ary
end end
@ -310,11 +312,11 @@ module Jekyll
def sample(input, num = 1) def sample(input, num = 1)
return input unless input.respond_to?(:sample) return input unless input.respond_to?(:sample)
n = num.to_i rescue 1 num = Liquid::Utils.to_integer(num) rescue 1
if n == 1 if num == 1
input.sample input.sample
else else
input.sample(n) input.sample(num)
end end
end end
@ -345,19 +347,12 @@ module Jekyll
private private
def time(input) def time(input)
case input date = Liquid::Utils.to_date(input)
when Time unless date.respond_to?(:to_time)
input.clone
when Date
input.to_time
when String
Time.parse(input) rescue Time.at(input.to_i)
when Numeric
Time.at(input)
else
raise Errors::InvalidDateError, raise Errors::InvalidDateError,
"Invalid Date: '#{input.inspect}' is not a valid datetime." "Invalid Date: '#{input.inspect}' is not a valid datetime."
end.localtime end
date.to_time.localtime
end end
private private