Skip to content
Closed
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
18 changes: 1 addition & 17 deletions lib/liquid/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,8 @@ def call_method_literal(literal, value)
end
end

# Implement blank? semantics matching ActiveSupport
# blank? returns true for nil, false, empty strings, whitespace-only strings,
# empty arrays, and empty hashes
def liquid_blank?(value)
case value
when NilClass, FalseClass
true
when TrueClass, Numeric
false
when String
# Blank if empty or whitespace only (matches ActiveSupport)
value.empty? || value.match?(/\A\s*\z/)
when Array, Hash
value.empty?
else
# Fall back to empty? if available, otherwise false
value.respond_to?(:empty?) ? value.empty? : false
end
Liquid::Utils.blank?(value)
end

# Implement empty? semantics
Expand Down
17 changes: 17 additions & 0 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,23 @@ def squish(input)
Utils.to_s(input).strip.gsub(/\s+/, ' ')
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category array
# @liquid_summary
# Converts a value into an array. Arrays are returned unchanged, blank values (`nil`, `false`,
# empty or whitespace-only strings, and empty arrays) become an empty array, and any other
# scalar value is wrapped in a single-element array.
# @liquid_syntax variable | to_array
# @liquid_return [array[untyped]]
def to_array(input)
return input if input.is_a?(Array)
return [] if Utils.blank?(input)
return [input] if input.is_a?(String) || input.is_a?(Numeric) || input.is_a?(TrueClass) || input.respond_to?(:strftime)

raise Liquid::ArgumentError, "cannot convert object into array"
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
Expand Down
18 changes: 18 additions & 0 deletions lib/liquid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ def self.to_date(obj)
nil
end

# Implement blank? semantics matching ActiveSupport
# blank? returns true for nil, false, empty strings, whitespace-only strings,
# empty arrays, and empty hashes
def self.blank?(value)
case value
when NilClass, FalseClass
true
when TrueClass, Numeric
false
when String
value.empty? || value.match?(/\A\s*\z/)
when Array, Hash
value.empty?
else
value.respond_to?(:empty?) ? value.empty? : false
end
end

def self.to_liquid_value(obj)
# Enable "obj" to represent itself as a primitive value like integer, string, or boolean
return obj.to_liquid_value if obj.respond_to?(:to_liquid_value)
Expand Down
20 changes: 20 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ def test_squish_filter
assert_equal("", Liquid::Template.parse('{{ " " | squish }}').render)
end

def test_to_array
assert_equal(['abc'], @filters.to_array('abc'))
assert_equal([1], @filters.to_array(1))
assert_equal([1, 2, 3], @filters.to_array([1, 2, 3]))
assert_equal([true], @filters.to_array(true))
end

def test_to_array_with_blank_values
assert_equal([], @filters.to_array(nil))
assert_equal([], @filters.to_array(false))
assert_equal([], @filters.to_array(''))
assert_equal([], @filters.to_array(' '))
assert_equal([], @filters.to_array([]))
end

def test_to_array_with_object_raises
assert_raises(Liquid::ArgumentError) { @filters.to_array({ 'a' => 1 }) }
assert_raises(Liquid::ArgumentError) { @filters.to_array(TestDrop.new(value: 'x')) }
end

def test_escape
assert_equal('&lt;strong&gt;', @filters.escape('<strong>'))
assert_equal('1', @filters.escape(1))
Expand Down
Loading