Module: CommonPathHelper

Defined in:
lib/license_finder/shared_helpers/common_path.rb

Class Method Summary collapse

Class Method Details

.longest_common_paths(paths) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/license_finder/shared_helpers/common_path.rb', line 4

def self.longest_common_paths(paths)
  [].tap do |common_paths|
    # organize by matching root paths
    paths_with_roots = paths.group_by { |path| path.split('/').first }
    paths_with_roots.each do |common_root, full_paths|
      # use the shortest path as the 'template'
      shortest_path = full_paths.min_by { |path| path.split('/').length }
      longest_common_path = common_root

      # iterate through each subpath of the 'template'
      shortest_path.split('/').each_with_index do |subpath, i|
        potential_path = i.zero? ? longest_common_path : [longest_common_path, subpath].join('/')

        # check each for the existence of the subsequent subpath
        mismatch = full_paths.any? { |path| !path.start_with?(potential_path) }
        break if mismatch

        longest_common_path = potential_path
      end

      longest_common_path = full_paths if longest_common_path.split('/').length == 1
      (common_paths << longest_common_path).flatten!
    end
  end
end