Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,9 @@ def apply_operation(input, operand, operation)
end

def nil_safe_compare(a, b)
a = a ? 1 : 0 if boolean?(a)
b = b ? 1 : 0 if boolean?(b)

result = a <=> b

if result
Expand All @@ -1029,6 +1032,10 @@ def nil_safe_casecmp(a, b)
end
end

def boolean?(value)
value.is_a?(TrueClass) || value.is_a?(FalseClass)
end

class InputIterator
include Enumerable

Expand Down
3 changes: 3 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,15 @@ def to_liquid

def test_sort
assert_equal([1, 2, 3, 4], @filters.sort([4, 3, 2, 1]))
assert_equal([false, false, true, true], @filters.sort([true, false, true, false]))
assert_equal([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a"))
assert_equal([{ "a" => false }, { "a" => false }, { "a" => true }, { "a" => true }], @filters.sort([{ "a" => true }, { "a" => false }, { "a" => true }, { "a" => false }], "a"))
end

def test_sort_with_nils
assert_equal([1, 2, 3, 4, nil], @filters.sort([nil, 4, 3, 2, 1]))
assert_equal([{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }, {}], @filters.sort([{ "a" => 4 }, { "a" => 3 }, {}, { "a" => 1 }, { "a" => 2 }], "a"))
assert_equal([{ "a" => false }, { "a" => false }, { "a" => true }, { "a" => true }, {}], @filters.sort([{ "a" => true }, {}, { "a" => false }, { "a" => true }, { "a" => false }], "a"))
end

def test_sort_when_property_is_sometimes_missing_puts_nils_last
Expand Down