Sha256: 90a7a8f22ff5814723b81482e9cfab0773380d2ad2d87e4ef51fe73a8d80679e

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# :nodoc:
module RegexpExamples
  # Given an array of arrays of strings, returns all possible perutations
  # 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|
      join_preserving_capture_groups(result)
    end
  end

  def self.join_preserving_capture_groups(result)
    # Only save the LAST group from repeated capture groups, e.g. /([ab]){2}/
    # (Hence the need for "reverse"!)
    subgroups = result
                .flat_map(&:all_subgroups)
                .reverse
                .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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regexp-examples-1.1.4 lib/regexp-examples/helpers.rb