Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
832516f
Add replace_last and remove_last tests
andershagbard Mar 30, 2021
e575c1f
Add replace_last and remove_last filters
andershagbard Mar 30, 2021
7754a0f
Use correct method
andershagbard Mar 30, 2021
38b7364
Improve tests
andershagbard Apr 2, 2021
effee1e
Update tests
andershagbard Apr 2, 2021
b3d14e5
Update tests
andershagbard Apr 6, 2021
973aa9f
Merge branch 'Shopify:master' into master
andershagbard Sep 9, 2021
d5ecf00
Update History.md
andershagbard Sep 9, 2021
f72cfb1
Delegate functions to corresponding replace functions
andershagbard Sep 10, 2021
dc7818f
Update filter to use rindex
andershagbard Sep 10, 2021
f5e77b6
Update test to support third argument
andershagbard Sep 10, 2021
b5abd14
Remove default value for replacement argument
andershagbard Sep 10, 2021
986391c
start_index will never be -1
andershagbard Sep 13, 2021
e3c82a9
Favor unless over if for negative conditions
andershagbard Sep 13, 2021
d81f7f0
Add tests to make sure it returns original string on no replacement
andershagbard Sep 16, 2021
aead4c5
Refactor filter
andershagbard Sep 16, 2021
f17c497
Merge branch 'Shopify:master' into master
andershagbard Sep 16, 2021
5187399
Update lib/liquid/standardfilters.rb
andershagbard Sep 17, 2021
45f186b
Remove string formatter
andershagbard Sep 17, 2021
0f17ed0
Use rindex again
andershagbard Sep 17, 2021
873ca15
Merge remote-tracking branch 'origin/master' into pr-1422
dylanahsmith Feb 24, 2022
50c88fe
History.md: Add missing PR number to previous changelog entry
dylanahsmith Feb 24, 2022
588d407
Fix new changelog entry so it is under unreleased
dylanahsmith Feb 24, 2022
63ae4cc
Remove redundant test assertions
dylanahsmith Feb 24, 2022
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
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 5.1.0 / 2021-09-09

### Features
* Add `remove_last`, and `replace_last` filters (#1422) [Anders Hagbard]
* Add `base64_encode`, `base64_decode`, `base64_url_safe_encode`, and `base64_url_safe_decode` filters (#1450) [Daniel Insley]
* Introduce `to_liquid_value` in `Liquid::Drop` (#1441) [Michael Go]

Expand Down
14 changes: 12 additions & 2 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,24 @@ def replace_first(input, string, replacement = '')
input.to_s.sub(string.to_s, replacement.to_s)
end

# Replace the last occurrences of a string with another
def replace_last(input, string, replacement)
input.to_s.sub(/.*\K#{Regexp.escape(string.to_s)}/, replacement.to_s)
Comment thread
andershagbard marked this conversation as resolved.
Outdated
end

# remove a substring
def remove(input, string)
input.to_s.gsub(string.to_s, '')
replace(input.to_s, string, '')
Comment thread
andershagbard marked this conversation as resolved.
Outdated
end

# remove the first occurrences of a substring
def remove_first(input, string)
input.to_s.sub(string.to_s, '')
replace_first(input.to_s, string, '')
end

# remove the last occurences of a substring
def remove_last(input, string)
replace_last(input.to_s, string, '')
end

# add one string to another
Expand Down
54 changes: 39 additions & 15 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,39 @@ def test_first_last
end

def test_replace
assert_equal('2 2 2 2', @filters.replace('1 1 1 1', '1', 2))
assert_equal('b b b b', @filters.replace('a a a a', 'a', 'b'))
assert_equal('2 2 2 2', @filters.replace('1 1 1 1', 1, 2))
assert_equal('2 1 1 1', @filters.replace_first('1 1 1 1', '1', 2))
assert_equal('1 1 1 1', @filters.replace('1 1 1 1', 2, 3))
assert_equal('b a a a', @filters.replace_first('a a a a', 'a', 'b'))
assert_equal('2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2))
assert_template_result('2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}")
assert_equal('1 1 1 1', @filters.replace_first('1 1 1 1', 2, 3))
assert_equal('a a a b', @filters.replace_last('a a a a', 'a', 'b'))
assert_equal('1 1 1 2', @filters.replace_last('1 1 1 1', 1, 2))
assert_equal('1 1 1 1', @filters.replace_last('1 1 1 1', 2, 3))
assert_template_result('b b b b', "{{ 'a a a a' | replace: 'a', 'b' }}")
assert_template_result('2 2 2 2', "{{ '1 1 1 1' | replace: 1, 2 }}")
assert_template_result('1 1 1 1', "{{ '1 1 1 1' | replace: 2, 3 }}")
assert_template_result('b a a a', "{{ 'a a a a' | replace_first: 'a', 'b' }}")
assert_template_result('2 1 1 1', "{{ '1 1 1 1' | replace_first: 1, 2 }}")
assert_template_result('a a a a', "{{ 'a a a a' | replace_first: 'b', 'c' }}")
assert_template_result('a a a b', "{{ 'a a a a' | replace_last: 'a', 'b' }}")
assert_template_result('1 1 1 2', "{{ '1 1 1 1' | replace_last: 1, 2 }}")
assert_template_result('a a a a', "{{ 'a a a a' | replace_last: 'b', 'c' }}")
end

def test_remove
assert_equal(' ', @filters.remove("a a a a", 'a'))
assert_equal(' ', @filters.remove("1 1 1 1", 1))
assert_equal('a a a', @filters.remove_first("a a a a", 'a '))
assert_equal('b a a', @filters.remove_first("a b a a", 'a '))
assert_equal(' 1 1 1', @filters.remove_first("1 1 1 1", 1))
assert_template_result('a a a', "{{ 'a a a a' | remove_first: 'a ' }}")
assert_equal('a a b', @filters.remove_last("a a b a", ' a'))
assert_equal('1 1 1 ', @filters.remove_last("1 1 1 1", 1))
assert_template_result(' ', "{{ 'a a a a' | remove: 'a' }}")
assert_template_result(' ', "{{ '1 1 1 1' | remove: 1 }}")
assert_template_result('b a a', "{{ 'a b a a' | remove_first: 'a ' }}")
assert_template_result(' 1 1 1', "{{ '1 1 1 1' | remove_first: 1 }}")
assert_template_result('a a b', "{{ 'a a b a' | remove_last: ' a' }}")
assert_template_result('1 1 1 ', "{{ '1 1 1 1' | remove_last: 1 }}")
end

def test_pipes_in_string_arguments
Expand Down Expand Up @@ -853,16 +873,20 @@ def test_all_filters_never_raise_non_liquid_exception
["foo", 123, nil, true, false, Drop, ["foo"], { foo: "bar" }],
]
test_types.each do |first|
test_types.each do |other|
(@filters.methods - Object.methods).each do |method|
arg_count = @filters.method(method).arity
arg_count *= -1 if arg_count < 0
inputs = [first]
inputs << ([other] * (arg_count - 1)) if arg_count > 1
begin
@filters.send(method, *inputs)
rescue Liquid::ArgumentError, Liquid::ZeroDivisionError
nil
test_types.each do |second|
test_types.each do |third|
(@filters.methods - Object.methods).each do |method|
arg_count = @filters.method(method).arity
arg_count *= -1 if arg_count < 0
inputs = [first]
inputs << ([second] * (arg_count - 1)) if arg_count > 1
inputs << ([third] * (arg_count - 1)) if arg_count > 2

begin
@filters.send(method, *inputs)
Comment thread
dylanahsmith marked this conversation as resolved.
Outdated
rescue Liquid::ArgumentError, Liquid::ZeroDivisionError
nil
end
end
end
end
Expand Down