Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test where filter handling numeric property values #7821

Merged
merged 3 commits into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/jekyll/filters.rb
Expand Up @@ -337,12 +337,23 @@ def compare_property_vs_target(property, target)
target = target.to_s
return true if property == target || Array(property).join == target
else
target = target.to_s
if property.is_a? String
return true if property == target
# Since `property` may be a Float via `#parse_sort_input`, it is imperative that we compare
# against the *floatified* value as well.
#
# For example, in a scenario where an item's property and `target` value are both `1234`,
# *floatifying* renders the `property` as `1234.0` which wouldn't equal the `target`.
# Therefore, we need to check against `"1234"`, `1234.0` and `"1234.0"`
target_string = target.to_s
parsed_target = parse_sort_input(target_string)
parsed_string = parsed_target.to_s

references = [target_string, parsed_string, parsed_target]

if property.is_a?(String)
return true if references.include?(property)
else
Array(property).each do |prop|
return true if prop.to_s == target
return true if references.include?(prop) || references.include?(prop.to_s)
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_filters.rb
Expand Up @@ -853,6 +853,17 @@ def to_liquid
assert_equal 2, @filter.where(array, "color", nil).length
end

should "filter objects with numerical properties appropriately" do
array = [
{ "value" => "555" },
{ "value" => 555 },
{ "value" => 24.625 },
{ "value" => "24.625" },
]
assert_equal 2, @filter.where(array, "value", 24.625).length
assert_equal 2, @filter.where(array, "value", 555).length
end

should "filter array properties appropriately" do
hash = {
"a" => { "tags"=>%w(x y) },
Expand Down