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

Fix for negating a filter script #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/jsonpath/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def parse_exp(exp)
elements.inject(@_current_node, &:__send__)
end

return !el if operator&.strip == '!' && operand.nil?
return (el ? true : false) if el.nil? || operator.nil?

el = Float(el) rescue el
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonpath/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class JsonPath
VERSION = '1.1.5'
VERSION = '1.1.6'
end
2 changes: 2 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_recognize_array_comma

def test_recognize_filters
assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@['isbn'])]").on(@object)
assert_equal [@object['store']['book'][0], @object['store']['book'][1], @object['store']['book'][4], @object['store']['book'][5], @object['store']['book'][6]], JsonPath.new("$..book[?(!@['isbn'])]").on(@object)
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object)
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] == 9)]").on(@object)
assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object)
Expand Down Expand Up @@ -157,6 +158,7 @@ def test_paths_with_numbers

def test_recognized_dot_notation_in_filters
assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[?(@.isbn)]').on(@object)
assert_equal [@object['store']['book'][0], @object['store']['book'][1], @object['store']['book'][4], @object['store']['book'][5], @object['store']['book'][6]], JsonPath.new("$..book[?([email protected])]").on(@object)
end

def test_works_on_non_hash
Expand Down