Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Liquid Change Log

## 5.1.1 (unreleased)
## 5.2.0 (unreleased)

### Fixes
### Features
* Add `remove_last`, and `replace_last` filters (#1422) [Anders Hagbard]

* Fix some internal errors in filters from invalid input [Dylan Thacker-Smith]
### Fixes
* Fix some internal errors in filters from invalid input (#1476) [Dylan Thacker-Smith]

## 5.1.0 / 2021-09-09

Expand Down
24 changes: 22 additions & 2 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,34 @@ 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 = input.to_s
string = string.to_s
replacement = replacement.to_s

start_index = input.rindex(string)

return input unless start_index

output = input.dup
output[start_index, string.length] = replacement
output
end

# remove a substring
def remove(input, string)
input.to_s.gsub(string.to_s, '')
replace(input, string, '')
end

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

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

# add one string to another
Expand Down
24 changes: 18 additions & 6 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,19 +539,31 @@ 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_template_result('2 2 2 2', "{{ '1 1 1 1' | replace: '1', 2 }}")

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_equal('1 1 1 1', @filters.replace_first('1 1 1 1', 2, 3))
assert_template_result('2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}")

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('1 1 1 2', "{{ '1 1 1 1' | replace_last: '1', 2 }}")
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(' 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_template_result(' ', "{{ '1 1 1 1' | remove: 1 }}")

assert_equal('b a a', @filters.remove_first("a b a a", 'a '))
assert_template_result(' 1 1 1', "{{ '1 1 1 1' | remove_first: 1 }}")

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

def test_pipes_in_string_arguments
Expand Down