Skip to content

Commit

Permalink
Backport #7878 for v4.0.x (#8160)
Browse files Browse the repository at this point in the history
Update item_property to recognize integers
This backports eb81dc0 to 4.0-stable

Co-authored-by: Ivan Gromov <summer.is.gone@gmail.com>
  • Loading branch information
ashmaroli and summerisgone committed May 6, 2020
1 parent 3845850 commit 587cb9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/jekyll/filters.rb
Expand Up @@ -368,15 +368,18 @@ def item_property(item, property)
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
def parse_sort_input(property)
number_like = %r!\A\s*-?(?:\d+\.?\d*|\.\d+)\s*\Z!
return property.to_f if property.to_s =~ number_like
stringified = property.to_s
return property.to_i if INTEGER_LIKE.match?(stringified)
return property.to_f if FLOAT_LIKE.match?(stringified)

property
end
# rubocop:enable Performance/RegexpMatch

def as_liquid(item)
case item
Expand Down
10 changes: 10 additions & 0 deletions test/test_filters.rb
Expand Up @@ -831,6 +831,16 @@ def to_liquid
)
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

context "where filter" do
Expand Down

0 comments on commit 587cb9b

Please sign in to comment.