Sha256: ca451f8937ce88f88305251724ff8f4a8fa7e6caa35ffe038d5a66c9e018404e

Contents?: true

Size: 965 Bytes

Versions: 12

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

module JsDependency
  module Extractor
    class ExtractSrcPath
      # @param [String] str
      def initialize(str)
        @str = str
      end

      # Extract JavaScript file (.js) in src path
      # @return [Array<String>]
      def call
        str = @str
        # <script src="module-name.js">
        paths = str.gsub(/<script\s+src="([^']+)">/).with_object([]) { |_, list| list << Regexp.last_match(1) }
        paths += str.gsub(/<script\s+src='([^']+)'>/).with_object([]) { |_, list| list << Regexp.last_match(1) }

        filter_javascript_paths(paths).uniq.sort
      end

      def self.call(str)
        new(str).call
      end

      private

      # Filter JavaScript file (.js) from Array of String
      # @param [Array<String>] paths
      # @return [Array<String>]
      def filter_javascript_paths(paths)
        paths.filter do |path|
          path.end_with?(".js")
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
js_dependency-0.4.1 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.4.0 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.15 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.14 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.13 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.12 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.11 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.10 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.9 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.8 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.7 lib/js_dependency/extractor/extract_src_path.rb
js_dependency-0.3.6 lib/js_dependency/extractor/extract_src_path.rb