From 3a082ddbbd4d846a402a89e6f9a2b034359d913e Mon Sep 17 00:00:00 2001 From: Martin Hanzel Date: Mon, 4 May 2015 11:55:14 -0400 Subject: [PATCH] Changed sort_natural filter to use casecmp. Strings only. --- lib/liquid/standardfilters.rb | 13 +++---------- test/integration/filter_test.rb | 7 ++----- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 712e172..745eb94 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -127,19 +127,12 @@ module Liquid def sort_natural(input, property = nil) ary = InputIterator.new(input) - # Quick function that returns the downcased object if it has a downcase, - # otherwise it returns the object itself. - insensitive = lambda do |obj| - obj = obj.downcase if obj.respond_to? :downcase - obj - end - if property.nil? - ary.sort {|a,b| insensitive.call(a) <=> insensitive.call(b) } + ary.sort {|a,b| a.casecmp b } elsif ary.first.respond_to?(:[]) && !ary.first[property].nil? - ary.sort {|a,b| insensitive.call(a[property]) <=> insensitive.call(b[property]) } + ary.sort {|a,b| a[property].casecmp b[property] } elsif ary.first.respond_to?(property) - ary.sort {|a,b| insensitive.call(a.send(property)) <=> insensitive.call(b.send(property)) } + ary.sort {|a,b| a.send(property).casecmp b.send(property) } end end diff --git a/test/integration/filter_test.rb b/test/integration/filter_test.rb index 23f64b7..da2593a 100644 --- a/test/integration/filter_test.rb +++ b/test/integration/filter_test.rb @@ -84,16 +84,13 @@ class FiltersTest < Minitest::Test end def test_sort_natural - @context['value'] = 3 - @context['numbers'] = [2,1,4,3] @context['words'] = ['case', 'Assert', 'Insensitive'] # This specific syntax forces hashes to have string keys. Colons won't work. @context['hashes'] = [{ 'a' => 'A'}, { 'a' => 'b'}, { 'a' => 'C' }] @context['objects'] = [TestObject.new('A'), TestObject.new('b'), TestObject.new('C')] - assert_equal [1,2,3,4], Variable.new("numbers | sort_natural").render(@context) + # Test strings assert_equal ['Assert', 'case', 'Insensitive'], Variable.new("words | sort_natural").render(@context) - assert_equal [3], Variable.new("value | sort_natural").render(@context) # Test hashes sorted = Variable.new("hashes | sort_natural: 'a'").render(@context) @@ -164,7 +161,7 @@ class FiltersInTemplate < Minitest::Test end end # FiltersTest -# Simple object that may be passed into a filter. +# Simple object that gmay be passed into a filter. # Note to test subjects: do not smuggle test objects out of the testing area. class TestObject attr_accessor :a