Compare commits

..

5 Commits

Author SHA1 Message Date
Mike Angell
9bb533985e styling fixes 2019-10-04 15:25:06 +10:00
Mike Angell
1c6a5d91fe Merge branch 'master' into sort-numeric 2019-10-04 15:20:00 +10:00
Mike Angell
1aa7d3d2ba Change registers to by symbols (#1178) 2019-09-27 04:32:24 +10:00
Mike Angell
0db9c56f34 Disable rendering of tag based on register (#1162)
* Disable rendering of tag based on register

* Improvements to disable tag

* Resolve disbale tag tests

* Test disable_tags register

* disabled_tags is now always avaiable

* Allow multiple tags to be disabled at once

* Move disabled check to block_body

* Code improvements

* Remove redundant nil check

* Improve disabled tag error output

* Improve disable tag API

* Code improvements

* Switch disabled? to not mutate output

* Fix array handling shortcut in disable_tags
2019-09-26 00:18:30 +10:00
Justin Li
2f0a2c66f3 Add sort_numeric filter 2018-09-13 17:16:50 -04:00
6 changed files with 31 additions and 4 deletions

View File

@@ -170,7 +170,7 @@ module Liquid
def disable_tags(context, tags, &block)
return yield if tags.empty?
context.registers['disabled_tags'].disable(tags, &block)
context.registers[:disabled_tags].disable(tags, &block)
end
def raise_if_resource_limits_reached(context, length)

View File

@@ -28,5 +28,5 @@ module Liquid
end
end
Template.add_register('disabled_tags', DisabledTags.new)
Template.add_register(:disabled_tags, DisabledTags.new)
end

View File

@@ -193,6 +193,23 @@ module Liquid
end
end
# Sort elements of an array in numeric order
# provide optional property with which to sort an array of hashes or drops
def sort_numeric(input, property = nil)
ary = InputIterator.new(input)
if property.nil?
ary.sort do |a, b|
Utils.to_number(a) <=> Utils.to_number(b)
end
elsif ary.empty? # The next two cases assume a non-empty array.
[]
elsif ary.first.respond_to?(:[]) && !ary.first[property].nil?
ary.sort do |a, b|
Utils.to_number(a[property]) <=> Utils.to_number(b[property])
end
end
end
# Remove duplicate elements from an array
# provide optional property with which to determine uniqueness
def uniq(input, property = nil)

View File

@@ -47,7 +47,7 @@ module Liquid
end
def disabled?(context)
context.registers['disabled_tags'].disabled?(tag_name)
context.registers[:disabled_tags].disabled?(tag_name)
end
def disabled_error_message

View File

@@ -93,7 +93,7 @@ module Liquid
end
def add_register(name, klass)
registers[name.to_s] = klass
registers[name.to_sym] = klass
end
def registers

View File

@@ -198,6 +198,12 @@ 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")
end
def test_sort_numeric
assert_equal ['1', '2', '3', '10'], @filters.sort_numeric(['10', '3', '2', '1'])
assert_equal [{ "a" => '1' }, { "a" => '2' }, { "a" => '3' }, { "a" => '10' }],
@filters.sort_numeric([{ "a" => '10' }, { "a" => '3' }, { "a" => '1' }, { "a" => '2' }], "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")
@@ -292,6 +298,10 @@ class StandardFiltersTest < Minitest::Test
assert_equal [], @filters.sort_natural([], "a")
end
def test_sort_numeric_empty_array
assert_equal [], @filters.sort_numeric([], "a")
end
def test_sort_natural_invalid_property
foo = [
[1],