command_search 0.1.1 → 0.1.3

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
- SHA1:
3
- metadata.gz: 0ff69a298e109d79d1cf70f7bc0d1470129d447d
4
- data.tar.gz: 37bfc67ff6702e307087611b5e75db52ce6334c7
2
+ SHA256:
3
+ metadata.gz: e2547eed6e11ca87fb0673811fe0e71c8b46438cb4608973965bb8861519a5fe
4
+ data.tar.gz: 0b89c973dbb4a2ee846bed3b3a8fd68de382db2a1ba3bab8a75ca29bb8803e87
5
5
  SHA512:
6
- metadata.gz: b53dbc26c9a8db5cd13c48b4def9065f29031fab0ed1336cefbb94f6e197ce7172db18e5b81937a4ee68d08a0ec700cd90145bf0996f1d5af32d70423b6f10ec
7
- data.tar.gz: 5f5a24aed587abe88c7408c3855360826b7fedcc00ab7f242e161717a8858465f82196aacb5d046ed9b98b985e6e88f843aeeb97b2b47dfcb4da00038cd6b318
6
+ metadata.gz: cf30f8ae6d579a5c9be52aca78fb622b6a12f3ced8c431226e467b7917fe75c5f87f14527a3f69567529ec8413a4f2634bd252652198ba6caaa8b2b0e2d6b972
7
+ data.tar.gz: 631d41b06396eb6db76ce6338707ee53bef5ecc1a26f5667854ff583b42091fcde5e1ff2670ccd73659c0e683d572195706f8bf46d3d03ca390b8accc2b6572e
@@ -7,10 +7,10 @@ module CommandSearch
7
7
  key.to_s
8
8
  end
9
9
 
10
- def dealias_values((key_node, seach_node), aliases)
10
+ def dealias_values((key_node, search_node), aliases)
11
11
  new_key = dealias_key(key_node[:value], aliases)
12
12
  key_node[:value] = new_key
13
- [key_node, seach_node]
13
+ [key_node, search_node]
14
14
  end
15
15
 
16
16
  def unnest_unaliased(node, aliases)
@@ -2,103 +2,43 @@ module CommandSearch
2
2
  module Lexer
3
3
  module_function
4
4
 
5
- # This class takes a string and returns it tokenized into
6
- # atoms/words, along with their type. It is coupled to the
7
- # parser in names of char_types and output data structure.
8
-
9
- # This currently does not support numbers with commas in them
10
-
11
- def char_type(char)
12
- case char
13
- when /["']/
14
- :quote
15
- when /[()]/
16
- :paren
17
- when /[<>]/
18
- :compare
19
- when /\s/
20
- :space
21
- when /\d/
22
- :number
23
- when '.'
24
- :period
25
- when '-'
26
- :minus
27
- when ':'
28
- :colon
29
- when '='
30
- :equal
31
- when '|'
32
- :pipe
33
- else
34
- :str
35
- end
36
- end
37
-
38
- def char_token(char)
39
- { type: char_type(char), value: char }
40
- end
41
-
42
- def value_indices(match, list)
43
- list.each_index.select { |i| list[i][:value] == match }
44
- end
45
-
46
- def group_quoted_strings(input)
47
- out = input
48
- while value_indices("'", out).length >= 2 || value_indices('"', out).length >= 2
49
- (a, b) = value_indices("'", out).first(2)
50
- (c, d) = value_indices('"', out).first(2)
51
- if a && b && (c.nil? || (a < c))
52
- (x, y) = [a, b]
53
- else
54
- (x, y) = [c, d]
5
+ def lex(input)
6
+ out = []
7
+ i = 0
8
+ while i < input.length
9
+ match = nil
10
+ case input[i..-1]
11
+ when /^\s+/
12
+ i += Regexp.last_match[0].length
13
+ next
14
+ when /^"(.*?)"/
15
+ match = Regexp.last_match[1]
16
+ type = :quoted_str
17
+ when /^'(.*?)'/
18
+ match = Regexp.last_match[1]
19
+ type = :quoted_str
20
+ when /^\-?\d+(\.\d+)?(?=$|[\s"':|<>)(])/
21
+ type = :number
22
+ when /^\|+/
23
+ type = :pipe
24
+ when /^-/
25
+ type = :minus
26
+ when /^:/
27
+ type = :colon
28
+ when /^[()]/
29
+ type = :paren
30
+ when /^[<>]=?/
31
+ type = :compare
32
+ when /^\d*[^\d\s:)][^\s"'|:<>)(]*/
33
+ type = :str
34
+ when /^./
35
+ type = :str
55
36
  end
56
- vals = out[x..y].map { |i| i[:value] }
57
- trimmed_vals = vals.take(vals.length - 1).drop(1)
58
- out[x..y] = { type: :quoted_str, value: trimmed_vals.join }
37
+ match = match || Regexp.last_match[0]
38
+ out.push(type: type, value: match)
39
+ i += Regexp.last_match[0].length
59
40
  end
60
41
  out
61
42
  end
62
-
63
- def group_pattern(input, group_type, pattern)
64
- out = input
65
- len = pattern.count
66
- while (out.map { |x| x[:type] }).each_cons(len).find_index(pattern)
67
- i = (out.map { |x| x[:type] }).each_cons(len).find_index(pattern)
68
- span = i..(i + len - 1)
69
- val = out[span].map { |x| x[:value] }.join()
70
- out[span] = { type: group_type, value: val }
71
- end
72
- out
73
- end
74
-
75
- def full_tokens(char_token_list)
76
- out = char_token_list.clone
77
-
78
- out = group_quoted_strings(out)
79
-
80
- out = group_pattern(out, :pipe, [:pipe, :pipe])
81
- out = group_pattern(out, :compare, [:compare, :equal])
82
-
83
- out = group_pattern(out, :number, [:number, :period, :number])
84
- out = group_pattern(out, :number, [:number, :number])
85
- out = group_pattern(out, :number, [:minus, :number])
86
-
87
- out = group_pattern(out, :str, [:equal])
88
- out = group_pattern(out, :str, [:period])
89
- out = group_pattern(out, :str, [:number, :str])
90
- out = group_pattern(out, :str, [:number, :minus])
91
- out = group_pattern(out, :str, [:str, :number])
92
- out = group_pattern(out, :str, [:str, :minus])
93
- out = group_pattern(out, :str, [:str, :str])
94
-
95
- out = out.reject { |x| x[:type] == :space }
96
- out
97
- end
98
-
99
- def lex(input)
100
- char_tokens = input.split('').map(&method(:char_token))
101
- full_tokens(char_tokens)
102
- end
103
43
  end
104
44
  end
@@ -29,7 +29,7 @@ module CommandSearch
29
29
  item[cmd][/#{Regexp.escape(cmd_search)}/i]
30
30
  elsif val[1][:type] == :quoted_str
31
31
  regex = /\b#{Regexp.escape(cmd_search)}\b/
32
- if cmd_search.first[/\W/] || cmd_search.last[/\W/]
32
+ if cmd_search[/(^\W)|(\W$)/]
33
33
  head_border = '(?<=^|[^:+\w])'
34
34
  tail_border = '(?=$|[^:+\w])'
35
35
  regex = Regexp.new(head_border + Regexp.escape(cmd_search) + tail_border)
@@ -84,7 +84,7 @@ module CommandSearch
84
84
  when nil
85
85
  if node[:type] == :quoted_str
86
86
  regex = /\b#{Regexp.escape(val)}\b/
87
- if val.first[/\W/] || val.last[/\W/]
87
+ if val[/(^\W)|(\W$)/]
88
88
  head_border = '(?<=^|[^:+\w])'
89
89
  tail_border = '(?=$|[^:+\w])'
90
90
  regex = Regexp.new(head_border + Regexp.escape(val) + tail_border)
@@ -9,13 +9,15 @@ module CommandSearch
9
9
  fields = [fields] unless fields.is_a?(Array)
10
10
  if ast_node[:type] == :quoted_str
11
11
  regex = /\b#{Regexp.escape(str)}\b/
12
- if str.first[/\W/] || str.last[/\W/]
12
+ if str[/(^\W)|(\W$)/]
13
13
  head_border = '(?<=^|[^:+\w])'
14
14
  tail_border = '(?=$|[^:+\w])'
15
15
  regex = Regexp.new(head_border + Regexp.escape(str) + tail_border)
16
16
  end
17
17
  else
18
- regex = /#{Regexp.escape(str)}/i
18
+ # TODO: This OR check is only needed due to mutable objects between subclasses of command_search,
19
+ # and is only needed for outside use or benchmarking.
20
+ regex = /#{Regexp.escape(str || '')}/i
19
21
  end
20
22
  if ast_node[:negate]
21
23
  forms = fields.map { |f| { f => { '$not' => regex } } }
@@ -87,7 +89,7 @@ module CommandSearch
87
89
  elsif type == String
88
90
  if search_type == :quoted_str
89
91
  val = /\b#{Regexp.escape(raw_val)}\b/
90
- if raw_val.first[/\W/] || raw_val.last[/\W/]
92
+ if raw_val[/(^\W)|(\W$)/]
91
93
  head_border = '(?<=^|[^:+\w])'
92
94
  tail_border = '(?=$|[^:+\w])'
93
95
  val = Regexp.new(head_border + Regexp.escape(raw_val) + tail_border)
@@ -60,6 +60,7 @@ module CommandSearch
60
60
  def clean_ununused_syntax(input)
61
61
  out = input.map do |x|
62
62
  next if x[:type] == :paren && x[:value].is_a?(String)
63
+ next if x[:nest_type] == :colon && x[:value].empty?
63
64
  if x[:nest_type] == :compare && x[:value].length < 2
64
65
  x = clean_ununused_syntax(x[:value]).first
65
66
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - zumbalogy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-03 00:00:00.000000000 Z
11
+ date: 2018-11-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Query collections with ease and without an engine like Elasticsearch.
14
14
  email:
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
44
  version: '0'
45
45
  requirements: []
46
46
  rubyforge_project:
47
- rubygems_version: 2.5.2
47
+ rubygems_version: 2.7.7
48
48
  signing_key:
49
49
  specification_version: 4
50
50
  summary: A friendly search gem for users and developers.