Skip to content

Commit 1b29fc4

Browse files
author
Tom Lord
committed
Documented configuration options
1 parent 1317f34 commit 1b29fc4

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This method generates a list of (some\*) strings that will match the given regul
99

1010
\* If the regex has an infinite number of possible srings that match it, such as `/a*b+c{2,}/`,
1111
or a huge number of possible matches, such as `/.\w/`, then only a subset of these will be listed.
12+
For more detail on this, see [configuration options](#configuration_options)
1213

1314
## Usage
1415

@@ -63,6 +64,39 @@ Using any of the following will raise a RegexpExamples::IllegalSyntax exception:
6364

6465
(Note: Backreferences are not really "regular" either, but I got these to work with a bit of hackery!)
6566

67+
<a name="configuration_options"/>
68+
##Configuration Options
69+
70+
When generating examples, the gem uses 2 configurable values to limit how many examples are listed:
71+
72+
* `max_repeater_variance` (default = `2`) restricts how many examples to return for each repeater. For example:
73+
* .\* is equivalent to .{0,2}
74+
* .+ is equivalent to .{1,3}
75+
* .{2,} is equivalent to .{2,4}
76+
* .{,3} is equivalent to .{0,2}
77+
* .{3,8} is equivalent to .{3,5}
78+
79+
* `max_group_results` (default = `5`) restricts how many characters to return for each "set". For example:
80+
* \d = ["0", "1", "2", "3", "4"]
81+
* \w = ["a", "b", "c", "d", "e"]
82+
* [h-s] = ["h", "i", "j", "k", "l"]
83+
* (1|2|3|4|5|6|7|8) = ["1", "2", "3", "4", "5"]
84+
85+
To use an alternative value, simply pass the configuration option as follows:
86+
87+
```ruby
88+
/a*/.examples(max_repeater_variance: 5) #=> [''. 'a', 'aa', 'aaa', 'aaaa']
89+
/[F-X]/.examples(max_group_results: 10) #=> ['F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
90+
```
91+
92+
**_WARNING_**: Choosing huge numbers, along with a "complex" regex, could easily cause your system to freeze!
93+
94+
For example, if you try to generate a list of _all_ 5-letter words: `/\w{5}/.examples(max_group_results: 999)`, then - since there are actually `63` "word" characters (upper/lower case letters, numbers and "\_"), this will try to generate `63**5 #=> 992436543` (almost 1 trillion) examples!
95+
96+
In other words, think twice before playing around with this config!
97+
98+
A more sensible use case might be, for example, to generate one random 1-4 digit string: `/\d{1,4}/.examples(max_repeater_variance: 3, max_group_results: 10).sample(1)`. (Note: I may develop a much more efficient way to "generate one example" in a later release of this gem.)
99+
66100
## Known Bugs
67101

68102
There are a few obscure bugs that have yet to be resolved:

0 commit comments

Comments
 (0)