mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 02:40:41 -07:00
Sorting support for data with undefined values
This commit is contained in:
@@ -121,17 +121,23 @@ module Liquid
|
|||||||
def sort(input, property = nil)
|
def sort(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input)
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.sort
|
ary.sort do |a, b|
|
||||||
|
if !a.nil? && !b.nil?
|
||||||
|
a <=> b
|
||||||
|
else
|
||||||
|
a.nil? ? 1 : -1
|
||||||
|
end
|
||||||
|
end
|
||||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||||
[]
|
[]
|
||||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
elsif ary.all? { |el| el.respond_to?(:[]) }
|
||||||
ary.sort do |a, b|
|
ary.sort do |a, b|
|
||||||
a = a[property]
|
a = a[property]
|
||||||
b = b[property]
|
b = b[property]
|
||||||
if a && b
|
if !a.nil? && !b.nil?
|
||||||
a <=> b
|
a <=> b
|
||||||
else
|
else
|
||||||
a ? -1 : 1
|
a.nil? ? 1 : -1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -143,17 +149,23 @@ module Liquid
|
|||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.sort { |a, b| a.to_s.casecmp(b.to_s) }
|
ary.sort do |a, b|
|
||||||
|
if !a.nil? && !b.nil?
|
||||||
|
a.to_s.casecmp(b.to_s)
|
||||||
|
else
|
||||||
|
a.nil? ? 1 : -1
|
||||||
|
end
|
||||||
|
end
|
||||||
elsif ary.empty? # The next two cases assume a non-empty array.
|
elsif ary.empty? # The next two cases assume a non-empty array.
|
||||||
[]
|
[]
|
||||||
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
|
elsif ary.all? { |el| el.respond_to?(:[]) }
|
||||||
ary.sort do |a, b|
|
ary.sort do |a, b|
|
||||||
a = a[property]
|
a = a[property]
|
||||||
b = b[property]
|
b = b[property]
|
||||||
if a && b
|
if !a.nil? && !b.nil?
|
||||||
a.to_s.casecmp(b.to_s)
|
a.to_s.casecmp(b.to_s)
|
||||||
else
|
else
|
||||||
a ? -1 : 1
|
a.nil? ? 1 : -1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -190,6 +190,11 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], @filters.sort([{ "a" => 4 }, { "a" => 3 }, { "a" => 1 }, { "a" => 2 }], "a")
|
||||||
end
|
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")
|
||||||
|
end
|
||||||
|
|
||||||
def test_sort_when_property_is_sometimes_missing_puts_nils_last
|
def test_sort_when_property_is_sometimes_missing_puts_nils_last
|
||||||
input = [
|
input = [
|
||||||
{ "price" => 4, "handle" => "alpha" },
|
{ "price" => 4, "handle" => "alpha" },
|
||||||
@@ -208,6 +213,16 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal expectation, @filters.sort(input, "price")
|
assert_equal expectation, @filters.sort(input, "price")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_sort_natural
|
||||||
|
assert_equal ["a", "B", "c", "D"], @filters.sort_natural(["c", "D", "a", "B"])
|
||||||
|
assert_equal [{ "a" => "a" }, { "a" => "B" }, { "a" => "c" }, { "a" => "D" }], @filters.sort_natural([{ "a" => "D" }, { "a" => "c" }, { "a" => "a" }, { "a" => "B" }], "a")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_sort_natural_with_nils
|
||||||
|
assert_equal ["a", "B", "c", "D", nil], @filters.sort_natural([nil, "c", "D", "a", "B"])
|
||||||
|
assert_equal [{ "a" => "a" }, { "a" => "B" }, { "a" => "c" }, { "a" => "D" }, {}], @filters.sort_natural([{ "a" => "D" }, { "a" => "c" }, {}, { "a" => "a" }, { "a" => "B" }], "a")
|
||||||
|
end
|
||||||
|
|
||||||
def test_sort_natural_when_property_is_sometimes_missing_puts_nils_last
|
def test_sort_natural_when_property_is_sometimes_missing_puts_nils_last
|
||||||
input = [
|
input = [
|
||||||
{ "price" => "4", "handle" => "alpha" },
|
{ "price" => "4", "handle" => "alpha" },
|
||||||
|
|||||||
Reference in New Issue
Block a user