command_search 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0fcd48aa860c52997a4d321cbd60da14cf1655a7def93428da30ab593992a25
4
- data.tar.gz: '093079892f0bb5dfeb7dc0e041349e106a791a4e0dfcc94a66fff0e4100c7058'
3
+ metadata.gz: 1ab34234037969a83820730a8a209e1fe0ca29ed9a2b34f1f1c3f0a72b570b5c
4
+ data.tar.gz: f3c4af7b842ae0a59057a2d700bb9c39b739c96fe18248377deecdc38c70aede
5
5
  SHA512:
6
- metadata.gz: 5d2638ace20b629f1e4ff71358e2573293d3e08c397a216479eefb3f5a995f50d84b14cfcfafbc1c4d8c76bb48a45cc8cb20a2aa300ac7f3dfca7809396ae3c8
7
- data.tar.gz: db0bc500aa61bda6dcb1f28c29b1822b1fcfb0b712c708aecb4b4badfcd72b840f42c3a1e90ae60efa84784cc800ccf5e7372289b1dd2e8702dc57b5541dc054
6
+ metadata.gz: d1e7ffc036d37d54217a78184a2c18705ad85529cad90e75851c6512d10a0214d22d083da7cf504eed6257bc28576c885a56dd383bc4e37cb39919ac1e8e665d
7
+ data.tar.gz: d4ce49e9747f21e06ced4884b4a72f03c6c335ab9d20e91f7e6ebd985a3a9cbca11dc0cacf1422d0ced8f4d322b9b48f95327099769030fa820eade6da4f5d7c
@@ -9,31 +9,27 @@ module CommandSearch
9
9
  match = nil
10
10
  case input[i..-1]
11
11
  when /^\s+/
12
- i += Regexp.last_match[0].length
13
- next
12
+ type = :space
14
13
  when /^"(.*?)"/
15
14
  match = Regexp.last_match[1]
16
15
  type = :quoted_str
17
16
  when /^'(.*?)'/
18
17
  match = Regexp.last_match[1]
19
18
  type = :quoted_str
20
- when /^\-?\d+(\.\d+)?(?=$|[\s"':|<>)(])/
19
+ when /^\-?\d+(\.\d+)?(?=$|[\s"':|<>()])/
21
20
  type = :number
22
- when /^\|+/
23
- type = :pipe
24
21
  when /^-/
25
22
  type = :minus
26
- when /^:/
27
- type = :colon
23
+ when /^[^\s:"|<>()]+/
24
+ type = :str
25
+ when /^\|+/
26
+ type = :pipe
28
27
  when /^[()]/
29
28
  type = :paren
29
+ when /^:/
30
+ type = :colon
30
31
  when /^[<>]=?/
31
32
  type = :compare
32
- # when /^([^\s"|<>()]+):/
33
- # match = Regexp.last_match[1]
34
- # type = :command
35
- when /^[^\s:"|<>()]+/
36
- type = :str
37
33
  when /^./
38
34
  type = :str
39
35
  end
@@ -4,9 +4,9 @@ module CommandSearch
4
4
 
5
5
  def parens_rindex(input)
6
6
  val_list = input.map { |x| x[:value] }
7
- open_i = val_list.rindex('(')
7
+ open_i = input.rindex { |x| x[:value] == '(' && x[:type] == :paren}
8
8
  return unless open_i
9
- close_offset = val_list.drop(open_i).index(')')
9
+ close_offset = input.drop(open_i).index { |x| x[:value] == ')' && x[:type] == :paren}
10
10
  return unless close_offset
11
11
  [open_i, close_offset + open_i]
12
12
  end
@@ -46,40 +46,81 @@ module CommandSearch
46
46
  end
47
47
  end
48
48
 
49
- def unchain!(type, input)
50
- (input.length - 2).times do |i|
49
+ def unchain!(types, input)
50
+ i = 0
51
+ while i < input.length - 2
51
52
  front = input[i][:type]
52
53
  mid = input[i + 1][:type]
53
54
  back = input[i + 2][:type]
54
- if front == type && mid != type && back == type
55
+ if types.include?(front) && !types.include?(mid) && types.include?(back)
55
56
  input.insert(i + 1, input[i + 1])
56
57
  end
58
+ i += 1
57
59
  end
58
60
  end
59
61
 
60
- def clean_ununused_syntax(input)
61
- out = input.map do |x|
62
+ def merge_strs(input, (x, y))
63
+ return input if input.empty?
64
+ if input[y] && input[y][:type] == :str
65
+ values = input.map { |x| x[:value] }
66
+ { type: :str, value: values.join() }
67
+ else
68
+ input[x][:type] = :str
69
+ input
70
+ end
71
+ end
72
+
73
+ def clean_ununusable!(input)
74
+ return unless input.any?
75
+
76
+ i = 0
77
+ while i < input.length
78
+ next i += 1 unless input[i][:type] == :minus
79
+ next i += 1 unless i > 0 && [:compare, :colon].include?(input[i - 1][:type])
80
+ input[i..i + 1] = merge_strs(input[i..i + 1], [0, 1])
81
+ end
82
+
83
+ i = 0
84
+ while i < input.length
85
+ next i += 1 if ![:compare, :colon].include?(input[i][:type])
86
+ next i += 1 if i > 0 &&
87
+ (i < input.count - 1) &&
88
+ [:str, :number, :quoted_str].include?(input[i - 1][:type]) &&
89
+ [:str, :number, :quoted_str].include?(input[i + 1][:type])
90
+
91
+ input[i..i + 1] = merge_strs(input[i..i + 1], [0, 1])
92
+ input[i - 1..i] = merge_strs(input[i - 1..i], [1, 0]) if i > 0
93
+ end
94
+
95
+ input.select! { |x| x[:type] != :space }
96
+ input[-1][:type] = :str if input[-1] && input[-1][:type] == :minus
97
+ end
98
+
99
+ def clean_ununused!(input)
100
+ input.map! do |x|
62
101
  next if x[:type] == :paren && x[:value].is_a?(String)
63
102
  next if x[:nest_type] == :colon && x[:value].empty?
64
103
  if x[:nest_type] == :compare && x[:value].length < 2
65
- x = clean_ununused_syntax(x[:value]).first
104
+ x = clean_ununused!(x[:value]).first
66
105
  end
67
106
  next x unless x && x[:type] == :nest
68
- x[:value] = clean_ununused_syntax(x[:value])
107
+ x[:value] = clean_ununused!(x[:value])
69
108
  x
70
109
  end
71
- out.compact
110
+ input.compact!
111
+ input
72
112
  end
73
113
 
74
114
  def parse(input)
75
115
  out = input
116
+ clean_ununusable!(out)
117
+ unchain!([:colon, :compare], out)
76
118
  out = group_parens(out)
77
119
  cluster!(:colon, out)
78
- unchain!(:compare, out)
79
120
  cluster!(:compare, out)
80
121
  cluster!(:minus, out, :prefix)
81
122
  cluster!(:pipe, out)
82
- out = clean_ununused_syntax(out)
123
+ clean_ununused!(out)
83
124
  out
84
125
  end
85
126
  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.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zumbalogy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-03 00:00:00.000000000 Z
11
+ date: 2018-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project:
61
- rubygems_version: 3.0.0.beta1
61
+ rubygems_version: 2.7.7
62
62
  signing_key:
63
63
  specification_version: 4
64
64
  summary: Let users query collections with ease.