Update item_property to recognize integers (#7878)
Merge pull request 7878
This commit is contained in:
parent
7ad84ef3c5
commit
eb81dc0e96
|
@ -368,15 +368,18 @@ module Jekyll
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# rubocop:disable Performance/RegexpMatch
|
FLOAT_LIKE = %r!\A\s*-?(?:\d+\.?\d*|\.\d+)\s*\Z!.freeze
|
||||||
|
INTEGER_LIKE = %r!\A\s*-?\d+\s*\Z!.freeze
|
||||||
|
private_constant :FLOAT_LIKE, :INTEGER_LIKE
|
||||||
|
|
||||||
# return numeric values as numbers for proper sorting
|
# return numeric values as numbers for proper sorting
|
||||||
def parse_sort_input(property)
|
def parse_sort_input(property)
|
||||||
number_like = %r!\A\s*-?(?:\d+\.?\d*|\.\d+)\s*\Z!
|
stringified = property.to_s
|
||||||
return property.to_f if property.to_s =~ number_like
|
return property.to_i if INTEGER_LIKE.match?(stringified)
|
||||||
|
return property.to_f if FLOAT_LIKE.match?(stringified)
|
||||||
|
|
||||||
property
|
property
|
||||||
end
|
end
|
||||||
# rubocop:enable Performance/RegexpMatch
|
|
||||||
|
|
||||||
def as_liquid(item)
|
def as_liquid(item)
|
||||||
case item
|
case item
|
||||||
|
|
|
@ -831,6 +831,16 @@ class TestFilters < JekyllUnitTest
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "should pass integers as is" do
|
||||||
|
grouping = @filter.group_by([
|
||||||
|
{ "name" => "Allison", "year" => 2016 },
|
||||||
|
{ "name" => "Amy", "year" => 2016 },
|
||||||
|
{ "name" => "George", "year" => 2019 },
|
||||||
|
], "year")
|
||||||
|
assert_equal "2016", grouping[0]["name"]
|
||||||
|
assert_equal "2019", grouping[1]["name"]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "where filter" do
|
context "where filter" do
|
||||||
|
|
Loading…
Reference in New Issue