node_query 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c7363cd71167f293f046d95014611ceb1f695c6639036a3cc0e202ab0ae5b2b
4
- data.tar.gz: 161c5784daae011699a1300b0cedc2f18bdc7235f71b7e89e1b1dce41caef71a
3
+ metadata.gz: 95dcf07d501c704e59bb792454be0bae1ddfafb915c77b6a062c6331d654564b
4
+ data.tar.gz: 7b835b8479ff5239ea77dcb5ed09f4868b1a09cbdf5edc2f4ab9413d352030be
5
5
  SHA512:
6
- metadata.gz: 356e94651bf61cb5c244399bbfe0aa353aafe3a3506fc7e6b1efba9cc4c853a0b963850c9d64e9b9de26dd3986f10967b2752a3dc7ec627dfd7466ddcf9a70f8
7
- data.tar.gz: 0fdacf2a86292f60968a02054ce43e02a131d3a1da7c528d1e6df2e8279afd5de19afe714a80bd1b9ab17143c433b4e219c7be2cf8b55e3f28e45ff82a25d274
6
+ metadata.gz: e2dbea64418363ad059ea2b6e9d93b3c2d010bdba59317327904b6a48411517a511ab6737fe40bdf4847b2833ec4d81eb5d474be7db7d831a6119c89fe904fea
7
+ data.tar.gz: c2cf0bfec6edfa28b644a38957dce1276cc287507c3c3034a39e0ce018f25e420faded6ad0c7c3c3838757df5107082c30d05fcf9f4323c5d5d3f32bc68337f1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.4.0 (2022-09-14)
4
+
5
+ * Add options `including_self`, `stop_on_match` and `recursive`
6
+ * Fix regex to match evaluated value
7
+
3
8
  ## 1.3.0 (2022-09-13)
4
9
 
5
10
  * Rename `NodeQuery#parse` to `NodeQuery#query_nodes`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- node_query (1.3.0)
4
+ node_query (1.4.0)
5
5
  activesupport (< 7.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -62,7 +62,7 @@ It provides two apis: `query_nodes` and `match_node?`
62
62
 
63
63
  ```ruby
64
64
  node_query = NodeQuery.new(nqlOrRules: String | Hash) # Initialize NodeQuery
65
- node_query.query_nodes(node: Node, including_self = true): Node[] # Get the matching nodes.
65
+ node_query.query_nodes(node: Node, options = { including_self: true, stop_on_match: false, recursive: true }): Node[] # Get the matching nodes.
66
66
  node_query.match_node?(node: Node): boolean # Check if the node matches nql or rules.
67
67
  ```
68
68
 
@@ -13,14 +13,17 @@ module NodeQuery::Compiler
13
13
 
14
14
  # Query nodes by the selector and the rest expression.
15
15
  # @param node [Node] node to match
16
- # @params including_self [boolean] if query the current node.
16
+ # @param options [Hash] if query the current node
17
+ # @option options [boolean] :including_self if query the current node, default is ture
18
+ # @option options [boolean] :stop_on_match if stop on first match, default is false
19
+ # @option options [boolean] :recursive if stop on first match, default is true
17
20
  # @return [Array<Node>] matching nodes.
18
- def query_nodes(node, including_self = true)
19
- matching_nodes = @selector.query_nodes(node, including_self)
21
+ def query_nodes(node, options = {})
22
+ matching_nodes = @selector.query_nodes(node, options)
20
23
  return matching_nodes if @rest.nil?
21
24
 
22
25
  matching_nodes.flat_map do |matching_node|
23
- @rest.query_nodes(matching_node, including_self)
26
+ @rest.query_nodes(matching_node, options)
24
27
  end
25
28
  end
26
29
 
@@ -13,13 +13,16 @@ module NodeQuery::Compiler
13
13
 
14
14
  # Query nodes by the current and the rest expression.
15
15
  # @param node [Node] node to match
16
- # @params including_self [boolean] if query the current node.
16
+ # @param options [Hash] if query the current node
17
+ # @option options [boolean] :including_self if query the current node, default is ture
18
+ # @option options [boolean] :stop_on_match if stop on first match, default is false
19
+ # @option options [boolean] :recursive if stop on first match, default is true
17
20
  # @return [Array<Node>] matching nodes.
18
- def query_nodes(node, including_self = true)
19
- matching_nodes = @expression.query_nodes(node, including_self)
21
+ def query_nodes(node, options = {})
22
+ matching_nodes = @expression.query_nodes(node, options)
20
23
  return matching_nodes if @rest.nil?
21
24
 
22
- matching_nodes + @rest.query_nodes(node, including_self)
25
+ matching_nodes + @rest.query_nodes(node, options)
23
26
  end
24
27
 
25
28
  # Check if the node matches the expression list.
@@ -33,9 +33,13 @@ module NodeQuery::Compiler
33
33
  # * If relationship is next sibling, it try to match next sibling node.
34
34
  # * If relationship is subsequent sibling, it will match in all sibling nodes.
35
35
  # @param node [Node] node to match
36
- # @params including_self [boolean] if query the current node.
36
+ # @param options [Hash] if query the current node
37
+ # @option options [boolean] :including_self if query the current node, default is ture
38
+ # @option options [boolean] :stop_on_match if stop on first match, default is false
39
+ # @option options [boolean] :recursive if stop on first match, default is true
37
40
  # @return [Array<Node>] matching nodes.
38
- def query_nodes(node, including_self = true)
41
+ def query_nodes(node, options = {})
42
+ options = { including_self: true, stop_on_match: false, recursive: true }.merge(options)
39
43
  return find_nodes_by_relationship(node) if @relationship
40
44
 
41
45
  if node.is_a?(::Array)
@@ -45,12 +49,25 @@ module NodeQuery::Compiler
45
49
  return find_nodes_by_goto_scope(node) if @goto_scope
46
50
 
47
51
  nodes = []
48
- if including_self && match?(node)
52
+ if options[:including_self] && match?(node)
49
53
  nodes << node
54
+ return matching_nodes if options[:stop_on_match]
50
55
  end
51
56
  if @basic_selector
52
- NodeQuery::Helper.handle_recursive_child(node) do |child_node|
53
- nodes << child_node if match?(child_node)
57
+ if options[:recursive]
58
+ NodeQuery::Helper.handle_recursive_child(node) do |child_node|
59
+ if match?(child_node)
60
+ nodes << child_node
61
+ break if options[:stop_on_match]
62
+ end
63
+ end
64
+ else
65
+ NodeQuery.adapter.get_children(node).each do |child_node|
66
+ if match?(child_node)
67
+ nodes << child_node
68
+ break if options[:stop_on_match]
69
+ end
70
+ end
54
71
  end
55
72
  end
56
73
  nodes
@@ -48,7 +48,7 @@ class NodeQuery::Helper
48
48
  # @param str [String] string to be evaluated
49
49
  # @return [String] evaluated string
50
50
  def evaluate_node_value(node, str)
51
- str.scan(/{{(.*)}}/).each do |match_data|
51
+ str.scan(/{{(.*?)}}/).each do |match_data|
52
52
  target_node = NodeQuery::Helper.get_target_node(node, match_data.first)
53
53
  str = str.sub("{{#{match_data.first}}}", NodeQuery.adapter.get_source(target_node))
54
54
  end
@@ -11,16 +11,31 @@ class NodeQuery::NodeRules
11
11
 
12
12
  # Query nodes by the rules.
13
13
  # @param node [Node] node to query
14
- # @params including_self [boolean] if query the current node.
14
+ # @param options [Hash] if query the current node
15
+ # @option options [boolean] :including_self if query the current node, default is ture
16
+ # @option options [boolean] :stop_on_match if stop on first match, default is false
17
+ # @option options [boolean] :recursive if stop on first match, default is true
15
18
  # @return [Array<Node>] matching nodes.
16
- def query_nodes(node, including_self = true)
19
+ def query_nodes(node, options = {})
20
+ options = { including_self: true, stop_on_match: false, recursive: true }.merge(options)
17
21
  matching_nodes = []
18
- if (including_self && match_node?(node))
22
+ if options[:including_self] && match_node?(node)
19
23
  matching_nodes.push(node)
24
+ return matching_nodes if options[:stop_on_match]
20
25
  end
21
- NodeQuery::Helper.handle_recursive_child(node) do |child_node|
22
- if (match_node?(child_node))
23
- matching_nodes.push(child_node)
26
+ if options[:recursive]
27
+ NodeQuery::Helper.handle_recursive_child(node) do |child_node|
28
+ if match_node?(child_node)
29
+ matching_nodes.push(child_node)
30
+ break if options[:stop_on_match]
31
+ end
32
+ end
33
+ else
34
+ NodeQuery.adapter.get_children(node).each do |child_node|
35
+ if match_node?(child_node)
36
+ matching_nodes.push(child_node)
37
+ break if options[:stop_on_match]
38
+ end
24
39
  end
25
40
  end
26
41
  matching_nodes
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class NodeQuery
4
- VERSION = "1.3.0"
4
+ VERSION = "1.4.0"
5
5
  end
data/lib/node_query.rb CHANGED
@@ -38,13 +38,16 @@ class NodeQuery
38
38
 
39
39
  # Query matching nodes.
40
40
  # @param node [Node] ast node
41
- # @param including_self [boolean] if check the node itself
41
+ # @param options [Hash] if query the current node
42
+ # @option options [boolean] :including_self if query the current node, default is ture
43
+ # @option options [boolean] :stop_on_match if stop on first match, default is false
44
+ # @option options [boolean] :recursive if stop on first match, default is true
42
45
  # @return [Array<Node>] matching child nodes
43
- def query_nodes(node, including_self = true)
46
+ def query_nodes(node, options = {})
44
47
  if @expression
45
- @expression.query_nodes(node, including_self)
48
+ @expression.query_nodes(node, options)
46
49
  elsif @rules
47
- @rules.query_nodes(node, including_self)
50
+ @rules.query_nodes(node, options)
48
51
  else
49
52
  []
50
53
  end
data/sig/node_query.rbs CHANGED
@@ -7,7 +7,7 @@ class NodeQuery[T]
7
7
 
8
8
  def initialize: (nqlOrRues: String | Hash) -> NodeQuery
9
9
 
10
- def query_nodes: (node: T, including_self: boolean) -> Array[T]
10
+ def query_nodes: (node: T, options: Hash) -> Array[T]
11
11
 
12
12
  def match_node?: (node: T) -> boolean
13
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: node_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-13 00:00:00.000000000 Z
11
+ date: 2022-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport