lib/regexp-examples/repeaters.rb in regexp-examples-1.3.2 vs lib/regexp-examples/repeaters.rb in regexp-examples-1.4.0

- old
+ new

@@ -8,11 +8,11 @@ def initialize(group) @group = group end def result - group_results = group.result.first(RegexpExamples.max_group_results) + group_results = group.result.first(RegexpExamples::Config.max_group_results) results = [] max_results_limiter = MaxResultsLimiterBySum.new min_repeats.upto(max_repeats) do |repeats| result = if repeats.zero? [GroupResult.new('')] @@ -49,21 +49,21 @@ # Equivalent to `/a{0,}/` class StarRepeater < BaseRepeater def initialize(group) super @min_repeats = 0 - @max_repeats = RegexpExamples.max_repeater_variance + @max_repeats = RegexpExamples::Config.max_repeater_variance end end # When a plus is used, e.g. `/a+/` # Equivalent to `/a{1,}/` class PlusRepeater < BaseRepeater def initialize(group) super @min_repeats = 1 - @max_repeats = RegexpExamples.max_repeater_variance + 1 + @max_repeats = RegexpExamples::Config.max_repeater_variance + 1 end end # When a question mark is used, e.g. `/a?/` # Equivalent to `/a{0,1}/` @@ -78,21 +78,16 @@ # When a range is used, e.g. `/a{1}/`, `/a{1,}/`, `/a{1,3}/`, `/a{,3}/` class RangeRepeater < BaseRepeater def initialize(group, min, has_comma, max) super(group) @min_repeats = min || 0 - if max # e.g. {1,100} --> Treat as {1,3} (by default max_repeater_variance) - @max_repeats = smallest(max, @min_repeats + RegexpExamples.max_repeater_variance) - elsif has_comma # e.g. {2,} --> Treat as {2,4} (by default max_repeater_variance) - @max_repeats = @min_repeats + RegexpExamples.max_repeater_variance - else # e.g. {3} --> Treat as {3,3} - @max_repeats = @min_repeats - end - end - - private - - def smallest(x, y) - x < y ? x : y + @max_repeats = if !has_comma + @min_repeats + else + [ + max, + @min_repeats + RegexpExamples::Config.max_repeater_variance + ].compact.min + end end end end