lib/regexp-examples/helpers.rb in regexp-examples-1.1.4 vs lib/regexp-examples/helpers.rb in regexp-examples-1.2.0

- old
+ new

@@ -4,14 +4,18 @@ # for strings, created by joining one element from each array # # For example: # permutations_of_strings [ ['a'], ['b'], ['c', 'd', 'e'] ] #=> ['abc', 'abd', 'abe'] # permutations_of_strings [ ['a', 'b'], ['c', 'd'] ] #=> [ 'ac', 'ad', 'bc', 'bd' ] - def self.permutations_of_strings(arrays_of_strings) - first = arrays_of_strings.shift - return first if arrays_of_strings.empty? - first.product(permutations_of_strings(arrays_of_strings)).map do |result| + # + # Edge case: + # permutations_of_strings [ [] ] #=> nil + # (For example, ths occurs during /[^\d\D]/.examples #=> []) + def self.permutations_of_strings(arrays_of_strings, max_results_limiter = MaxResultsLimiterByProduct.new) + partial_result = max_results_limiter.limit_results(arrays_of_strings.shift) + return partial_result if arrays_of_strings.empty? + partial_result.product(permutations_of_strings(arrays_of_strings, max_results_limiter)).map do |result| join_preserving_capture_groups(result) end end def self.join_preserving_capture_groups(result) @@ -23,23 +27,11 @@ .uniq(&:group_id) GroupResult.new(result.join, nil, subgroups) end - def self.map_results(repeaters) - generic_map_result(repeaters, :result) - end - - def self.map_random_result(repeaters) - generic_map_result(repeaters, :random_result) - end - - private - def self.generic_map_result(repeaters, method) - repeaters - .map { |repeater| repeater.public_send(method) } - .instance_eval do |partial_results| - RegexpExamples.permutations_of_strings(partial_results) - end + permutations_of_strings( + repeaters.map { |repeater| repeater.public_send(method) } + ) end end