string_pattern 2.2.2 → 2.3.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: 9048ba0746bbb3985f18678744891fac6b77956eb66f4d60ef4bff6a2ef16768
4
- data.tar.gz: e1f50e98a7d47bf4fcaf955c7c5fe3efec6ff505e9b68a0cfd1e612c336d3f29
3
+ metadata.gz: 7b9ea7310d3fb561b37d8b0753360ddd5742188a2f71b68560a121d716d65083
4
+ data.tar.gz: a0504e61ff66a749d7c0b0fcc36ad629474d793abd78bc0456bdd6a9ddeae7f7
5
5
  SHA512:
6
- metadata.gz: bb82b702765edbbfc69f767074f3e713e4f036e7b9651993a8b1aee59bd8075a61dc7866fbbff6a2095f3ea3f5d5841711a62ce3f397d3364e451efa27e59b0c
7
- data.tar.gz: 1f2683a231a390361011343315f8f42674ed359199d353f246bc5eba30a79a52b13f47e2c1625a3eb28a31a0126808cef61e359a3cfe18514afd47a77a859641
6
+ metadata.gz: 3ade8ead8f7434fb1893f62e44523245fb52776a24ffc9f9f548a16f790ec035f2411d223203886571decd7c7455b6f56cf41a7e62c491910762c210754e11f7
7
+ data.tar.gz: bbb68322396f6017f6f6f1b6190b1e14cc0a3cea804efa66f6527f892be032fe5cebc321e39ea399e0af9f69ee6f5e4ef1c7a9241ac97541cf6d4aac3191c658
data/README.md CHANGED
@@ -428,6 +428,17 @@ StringPattern.optimistic = true
428
428
  #>SAAERfixedtext988
429
429
  ```
430
430
 
431
+ #### block_list
432
+
433
+ To specify which words will be avoided from the results
434
+
435
+ ```ruby
436
+ StringPattern.block_list = ['example', 'wrong', 'ugly']
437
+ StringPattern.block_list_enabled = true
438
+ "2-20:Tn".gen #>AAñ34Ef99éNOP
439
+ ```
440
+
441
+
431
442
  ## Contributing
432
443
 
433
444
  Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/string_pattern.
@@ -40,6 +40,17 @@ class String
40
40
  gsub(/[^a-zA-Z0-9ññÑáéíóúÁÉÍÓÚüÜ_]/, "_")
41
41
  .split("_").map(&:capitalize).join
42
42
  end
43
+
44
+ ########################################################
45
+ # Convert to snake_case a string
46
+ ########################################################
47
+ def to_snake_case
48
+ gsub(/\W/, '_')
49
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
50
+ .gsub(/([a-z])([A-Z])/, '\1_\2')
51
+ .downcase
52
+ .gsub(/_+/, '_')
53
+ end
43
54
  end
44
55
 
45
56
  class Symbol
@@ -79,11 +90,14 @@ class Regexp
79
90
  pats = ""
80
91
  patg = [] # for (aa|bb|cc) group
81
92
  set = false
93
+ set_negate = false
94
+ options = []
82
95
  capture = false
83
96
 
84
97
  range = ""
85
98
  fixed_text = false
86
- last_char = (regexp.to_s.gsub("?-mix:", "").length) - 2
99
+ options = regexp.to_s.scan(/\A\(\?([mix]*)\-[mix]*:/).join.split('')
100
+ last_char = (regexp.to_s.gsub(/\A\(\?[mix]*\-[mix]*:/, "").length) - 2
87
101
  Regexp::Scanner.scan regexp do |type, token, text, ts, te|
88
102
  if type == :escape
89
103
  if token == :dot
@@ -115,9 +129,9 @@ class Regexp
115
129
  pata[-1] += pats.chop
116
130
  else
117
131
  if pats.size == 2
118
- pata << pats.chop #jal
132
+ pata << pats.chop
119
133
  else
120
- pata << "1:[#{pats}" #jal
134
+ pata << "1:[#{pats}"
121
135
  end
122
136
  if last_char == te and type == :literal and token == :literal
123
137
  pata << text
@@ -136,7 +150,6 @@ class Regexp
136
150
  pats = ""
137
151
  end
138
152
  fixed_text = false
139
-
140
153
  case token
141
154
  when :open
142
155
  set = true
@@ -147,7 +160,13 @@ class Regexp
147
160
  if pats[-1] == "["
148
161
  pats.chop!
149
162
  else
150
- pats += "]"
163
+ if set_negate
164
+ pats+="%]*"
165
+ set_negate = false
166
+ else
167
+ pats += "]"
168
+ end
169
+
151
170
  end
152
171
  elsif type == :group
153
172
  capture = false
@@ -158,6 +177,11 @@ class Regexp
158
177
  pats = ""
159
178
  end
160
179
  end
180
+ when :negate
181
+ if set and pats[-1] == '['
182
+ pats+="%"
183
+ set_negate = true
184
+ end
161
185
  when :capture
162
186
  capture = true if type == :group
163
187
  when :alternation
@@ -171,6 +195,7 @@ class Regexp
171
195
  end
172
196
  end
173
197
  when :range
198
+ pats.chop! if options.include?('i')
174
199
  range = pats[-1]
175
200
  pats.chop!
176
201
  when :digit
@@ -201,28 +226,44 @@ class Regexp
201
226
  pats = text[-1]
202
227
  else
203
228
  pats += text
229
+ pats += text.upcase if options.include?('i')
204
230
  end
205
231
  else
206
232
  range = range + "-" + text
207
233
  if range == "a-z"
208
- pats = "x" + pats
234
+ if options.include?('i')
235
+ pats = "L" + pats
236
+ else
237
+ pats = "x" + pats
238
+ end
209
239
  elsif range == "A-Z"
210
- pats = "X" + pats
240
+ if options.include?('i')
241
+ pats = "L" + pats
242
+ else
243
+ pats = "X" + pats
244
+ end
211
245
  elsif range == "0-9"
212
246
  pats = "n" + pats
213
247
  else
214
- pats += if set
215
- (range[0]..range[2]).to_a.join
216
- else
217
- "[" + (range[0]..range[2]).to_a.join + "]"
218
- end
248
+ if set
249
+ pats += (range[0]..range[2]).to_a.join
250
+ if options.include?('i')
251
+ pats += (range[0]..range[2]).to_a.join.upcase
252
+ end
253
+ else
254
+ trange = (range[0]..range[2]).to_a.join
255
+ if options.include?('i')
256
+ trange += trange.upcase
257
+ end
258
+ pats += "[" + trange + "]"
259
+ end
219
260
  end
220
261
  range = ""
221
262
  end
222
263
  pats = "[" + pats + "]" unless set
223
264
  when :interval
224
265
  size = text.sub(",", "-").sub("{", "").sub("}", "")
225
- size.chop! if size[-1] == "-"
266
+ size+=(default_infinite+size.chop.to_i).to_s if size[-1] == "-"
226
267
  pats = size + ":" + pats
227
268
  if !patg.empty?
228
269
  patg << pats
@@ -568,6 +568,16 @@ class StringPattern
568
568
  good_result = true
569
569
  end
570
570
  end
571
+ if @block_list_enabled
572
+ if @block_list.is_a?(Array)
573
+ @block_list.each do |bl|
574
+ if string.match?(/#{bl}/i)
575
+ good_result = false
576
+ break
577
+ end
578
+ end
579
+ end
580
+ end
571
581
  end until good_result or tries > 10000
572
582
  unless good_result
573
583
  puts "Not possible to generate the string on StringPattern.generate: #{pattern.inspect}, expected_errors: #{expected_errors.inspect}"
@@ -25,9 +25,13 @@ require_relative "string/pattern/validate"
25
25
  # In case using regular expressions the maximum when using * or + for repetitions
26
26
  # word_separator: (String, default: '_')
27
27
  # When generating words using symbol types 'w' or 'p' the character to separate the english or spanish words.
28
+ # block_list: (Array, default: empty)
29
+ # Array of words to be avoided from resultant strings.
30
+ # block_list_enabled: (TrueFalse, default: false)
31
+ # If true block_list will be take in consideration
28
32
  class StringPattern
29
33
  class << self
30
- attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values, :default_infinite, :word_separator
34
+ attr_accessor :national_chars, :optimistic, :dont_repeat, :cache, :cache_values, :default_infinite, :word_separator, :block_list, :block_list_enabled
31
35
  end
32
36
  @national_chars = (("a".."z").to_a + ("A".."Z").to_a).join
33
37
  @optimistic = true
@@ -36,6 +40,8 @@ class StringPattern
36
40
  @dont_repeat = false
37
41
  @default_infinite = 10
38
42
  @word_separator = "_"
43
+ @block_list_enabled = false
44
+ @block_list = []
39
45
  NUMBER_SET = ("0".."9").to_a
40
46
  SPECIAL_SET = [" ", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "{", "}", "[", "]", "'", ";", ":", "?", ">", "<", "`", "|", "/", '"']
41
47
  ALPHA_SET_LOWER = ("a".."z").to_a
@@ -56,4 +62,5 @@ class StringPattern
56
62
  @cache = Hash.new()
57
63
  @national_chars = par
58
64
  end
65
+
59
66
  end
metadata CHANGED
@@ -1,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_pattern
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-20 00:00:00.000000000 Z
11
+ date: 2022-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: regexp_parser
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.3.0
20
17
  - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: '1.3'
19
+ version: '2.5'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.5.0
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: 1.3.0
30
27
  - - "~>"
31
28
  - !ruby/object:Gem::Version
32
- version: '1.3'
29
+ version: '2.5'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.5.0
33
33
  description: 'Easily generate strings supplying a very simple pattern. ''10-20:Xn/x/''.generate
34
34
  #>qBstvc6JN8ra. Generate random strings using a regular expression (Regexp): /[a-z0-9]{2,5}w+/.gen
35
35
  . Also generate words in English or Spanish. Perfect to be used in test data factories.
@@ -68,7 +68,7 @@ homepage: https://github.com/MarioRuiz/string_pattern
68
68
  licenses:
69
69
  - MIT
70
70
  metadata: {}
71
- post_install_message:
71
+ post_install_message:
72
72
  rdoc_options: []
73
73
  require_paths:
74
74
  - lib
@@ -83,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 3.0.3
87
- signing_key:
86
+ rubygems_version: 3.2.15
87
+ signing_key:
88
88
  specification_version: 4
89
89
  summary: 'Generate easily random strings following a simple pattern or regular expression.
90
90
  ''10-20:Xn/x/''.generate #>qBstvc6JN8ra. Also generate words in English or Spanish.