Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Pass all compact filter golden tests. #71

Merged
merged 1 commit into from
Jan 18, 2023
Merged
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
2 changes: 0 additions & 2 deletions spec/integration/golden_liquid.pending
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ liquid.golden.base64_url_safe_encode_filter from string
liquid.golden.base64_url_safe_encode_filter from string with URL unsafe
liquid.golden.base64_url_safe_encode_filter not a string
liquid.golden.base64_url_safe_encode_filter unexpected argument
liquid.golden.compact_filter array of objects with key property
liquid.golden.compact_filter too many arguments
liquid.golden.concat_filter left value contains non string
liquid.golden.concat_filter left value is not array-like
liquid.golden.concat_filter missing argument is an error
Expand Down
5 changes: 5 additions & 0 deletions src/liquid/drop.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ module Liquid
def call(method : String) : Liquid::Any
raise Liquid::InvalidExpression.new("Method #{method} not found for #{self.class.name}.")
end

# Alias to `#call`.
def [](method : String) : Liquid::Any
call(method)
end
end
end

Expand Down
21 changes: 17 additions & 4 deletions src/liquid/filters/compact.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ module Liquid::Filters
extend Filter

def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any
if d = data.as_a?
Any.new(d.reject(&.raw.nil?))
else
data
raise FilterArgumentException.new("compact filter expects at most 1 argument.") if args.size > 1

array = data.as_a?
return data if array.nil?

key = args.first?
return Any.new(array.reject(&.raw.nil?)) if key.nil? || !key.as_s?

key = key.as_s
result = array.reject do |item|
raw_item = item.raw
if raw_item.is_a?(Drop) || raw_item.is_a?(Hash)
raw_item[key].raw.nil?
else
raw_item.nil?
end
end
Any.new(result)
end
end

Expand Down