Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add reject filter #1573

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 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
bfdcfce
Add reject filter
andershagbard May 12, 2022
dae331f
Merge branch 'master' into filter-reject
andershagbard Jun 2, 2022
4db8ef9
Merge branch 'Shopify:master' into filter-reject
andershagbard Jun 7, 2022
7254023
Update History.md
andershagbard Jun 7, 2022
80e557d
Merge branch 'master' into filter-reject
andershagbard Jun 29, 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
23 changes: 23 additions & 0 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ def where(input, property, target_value = nil)
end
end

# Reject the elements of an array to those with a certain property value.
# By default the target is any falsy value.
def reject(input, properties, target_value = nil)
raise_property_error(properties) unless properties.is_a?(String)

properties = properties.to_s.split('.')
ary = InputIterator.new(input, context)

ary.reject do |item|
if item.is_a?(Hash)
value = item.dig(*properties)

if target_value.nil?
!value
else
value == target_value
end
else
true
end
end
end
andershagbard marked this conversation as resolved.
Show resolved Hide resolved

# Remove duplicate elements from an array
# provide optional property with which to determine uniqueness
def uniq(input, property = nil)
Expand Down
75 changes: 75 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,81 @@ def test_where_array_of_only_unindexable_values
assert_nil(@filters.where([nil], "ok"))
end

def test_reject
input = [
{ "handle" => "alpha", "ok" => true },
{ "handle" => "beta", "ok" => false },
{ "handle" => "gamma", "ok" => false },
{ "handle" => "delta", "ok" => true },
]

expectation = [
{ "handle" => "alpha", "ok" => true },
{ "handle" => "delta", "ok" => true },
]

assert_equal(expectation, @filters.reject(input, "ok", false))
assert_equal(expectation, @filters.reject(input, "ok"))
end

def test_reject_no_key_set
input = [
{ "handle" => "alpha", "ok" => true },
{ "handle" => "beta" },
{ "handle" => "gamma" },
{ "handle" => "delta", "ok" => true },
]

expectation = [
{ "handle" => "alpha", "ok" => true },
{ "handle" => "delta", "ok" => true },
]

assert_equal(expectation, @filters.reject(input, "ok"))
end

def test_reject_non_array_map_input
assert_equal([], @filters.reject({ "foo" => "bar" }, "foo", "bar"))
assert_equal([{ "foo" => "baz" }], @filters.reject({ "foo" => "baz" }, "foo", "bar"))
end

def test_reject_indexable_but_non_map_value
assert_equal([], @filters.reject(1, "ok", true))
assert_equal([], @filters.reject(1, "ok"))
end

def test_reject_non_boolean_value
input = [
{ "message" => "Bonjour!", "language" => "French" },
{ "message" => "Hello!", "language" => "English" },
]

assert_equal([{ "message" => "Hello!", "language" => "English" }], @filters.reject(input, "language", "French"))
assert_equal([{ "message" => "Bonjour!", "language" => "French" }], @filters.reject(input, "language", "English"))
end

def test_reject_array_of_only_unindexable_values
assert_equal([], @filters.reject([nil, nil], "ok", true))
assert_equal([], @filters.reject([nil, nil], "ok"))
end

def test_reject_deep
input = [
{ "item" => { "handle" => "alpha", "ok" => true } },
{ "item" => { "handle" => "beta", "ok" => false } },
{ "item" => { "handle" => "gamma", "ok" => false } },
{ "item" => { "handle" => "delta", "ok" => true } },
]

expectation = [
{ "item" => { "handle" => "alpha", "ok" => true } },
{ "item" => { "handle" => "delta", "ok" => true } },
]

assert_equal(expectation, @filters.reject(input, "item.ok", false))
assert_equal(expectation, @filters.reject(input, "item.ok"))
end

def test_all_filters_never_raise_non_liquid_exception
test_drop = TestDrop.new(value: "test")
test_drop.context = Context.new
Expand Down