Skip to content

Commit 3e76244

Browse files
authored
Merge pull request #2050 from bakura10/squish-filter
Add squish filter
2 parents d897899 + d589c51 commit 3e76244

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/liquid/standardfilters.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,19 @@ def split(input, pattern)
293293
input.split(pattern)
294294
end
295295

296+
# @liquid_public_docs
297+
# @liquid_type filter
298+
# @liquid_category string
299+
# @liquid_summary
300+
# Removes leading and trailing whitespace and collapses consecutive whitespace to a single space.
301+
# @liquid_syntax string | squish
302+
# @liquid_return [string]
303+
def squish(input)
304+
return if input.nil?
305+
306+
Utils.to_s(input).strip.gsub(/\s+/, ' ')
307+
end
308+
296309
# @liquid_public_docs
297310
# @liquid_type filter
298311
# @liquid_category string

test/integration/standard_filter_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def test_split
164164
assert_equal(['A', 'Z'], @filters.split('A1Z', 1))
165165
end
166166

167+
def test_squish_filter
168+
assert_equal("foo bar boo", Liquid::Template.parse(%Q({{ " foo bar
169+
\t boo " | squish }})).render)
170+
assert_equal("", Liquid::Template.parse('{{ nil | squish }}').render)
171+
assert_equal("", Liquid::Template.parse('{{ " " | squish }}').render)
172+
end
173+
167174
def test_escape
168175
assert_equal('&lt;strong&gt;', @filters.escape('<strong>'))
169176
assert_equal('1', @filters.escape(1))

0 commit comments

Comments
 (0)