lib/regexp-examples/helpers.rb in regexp-examples-1.1.3 vs lib/regexp-examples/helpers.rb in regexp-examples-1.1.4
- old
+ new
@@ -1,5 +1,6 @@
+# :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:
@@ -12,18 +13,16 @@
join_preserving_capture_groups(result)
end
end
def self.join_preserving_capture_groups(result)
- result.flatten!
+ # Only save the LAST group from repeated capture groups, e.g. /([ab]){2}/
+ # (Hence the need for "reverse"!)
subgroups = result
- .map(&:all_subgroups)
- .flatten
+ .flat_map(&:all_subgroups)
+ .reverse
+ .uniq(&:group_id)
- # Only save the LAST group from repeated capture groups, e.g. /([ab]){2}/
- subgroups.delete_if do |subgroup|
- subgroups.count { |other_subgroup| other_subgroup.group_id == subgroup.group_id } > 1
- end
GroupResult.new(result.join, nil, subgroups)
end
def self.map_results(repeaters)
generic_map_result(repeaters, :result)