mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 07:05:12 -07:00
add a input iterator to standard filter
This commit is contained in:
@@ -98,7 +98,7 @@ module Liquid
|
|||||||
# Sort elements of the array
|
# Sort elements of the array
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort(input, property = nil)
|
def sort(input, property = nil)
|
||||||
ary = map_input(input)
|
ary = InputIterator.new(input).to_a
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.sort
|
ary.sort
|
||||||
elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil?
|
elsif ary.first.respond_to?('[]'.freeze) and !ary.first[property].nil?
|
||||||
@@ -116,7 +116,7 @@ module Liquid
|
|||||||
|
|
||||||
# map/collect on a given property
|
# map/collect on a given property
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
map_input(input).map do |e|
|
InputIterator.new(input).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
if property == "to_liquid".freeze
|
if property == "to_liquid".freeze
|
||||||
@@ -265,15 +265,6 @@ module Liquid
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def map_input(input)
|
|
||||||
ary = if input.is_a?(Array) || input.kind_of?(Enumerable)
|
|
||||||
input
|
|
||||||
else
|
|
||||||
Array(input)
|
|
||||||
end
|
|
||||||
ary.map{ |e| e.respond_to?(:to_liquid) ? e.to_liquid : e }
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_number(obj)
|
def to_number(obj)
|
||||||
case obj
|
case obj
|
||||||
when Float
|
when Float
|
||||||
@@ -308,6 +299,25 @@ module Liquid
|
|||||||
result = to_number(input).send(operation, to_number(operand))
|
result = to_number(input).send(operation, to_number(operand))
|
||||||
result.is_a?(BigDecimal) ? result.to_f : result
|
result.is_a?(BigDecimal) ? result.to_f : result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class InputIterator
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
|
def initialize(input)
|
||||||
|
@input =
|
||||||
|
if input.is_a?(Array) || input.kind_of?(Enumerable)
|
||||||
|
input
|
||||||
|
else
|
||||||
|
Array(input)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def each
|
||||||
|
@input.each do |e|
|
||||||
|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_filter(StandardFilters)
|
Template.register_filter(StandardFilters)
|
||||||
|
|||||||
Reference in New Issue
Block a user