Sha256: 5b36ddca26ca53a88b1e6f702bc0709fd51e99206dbb3f904ece050c540cae60

Contents?: true

Size: 934 Bytes

Versions: 18

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

module JsDependency
  class ReplacePathAlias
    # @param [String] str
    # @param [Hash, nil] alias_paths
    def initialize(str, alias_paths = nil)
      @str = str
      @alias_paths = alias_paths
    end

    # @param [String] str
    # @param [Hash, nil] alias_paths
    # @return [String]
    def self.call(str, alias_paths = nil)
      return str if alias_paths.nil? || alias_paths.empty?

      new(str, alias_paths).call
    end

    # @return [String]
    def call
      return @str if safe_list.include?(@str)

      replace_alias_paths(@str, @alias_paths)
    end

    private

    # @param [String] str
    # @param [nil, Hash] alias_paths
    def replace_alias_paths(str, alias_paths)
      alias_paths.each do |(key, value)|
        str = str.gsub(/^#{key}/, value.to_s)
      end
      str
    end

    # @return [Array<String>]
    def safe_list
      ["@rails/ujs"]
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
js_dependency-0.4.1 lib/js_dependency/replace_path_alias.rb
js_dependency-0.4.0 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.15 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.14 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.13 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.12 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.11 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.10 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.9 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.8 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.7 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.6 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.5 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.4 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.3.1 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.3 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.2 lib/js_dependency/replace_path_alias.rb
js_dependency-0.3.1 lib/js_dependency/replace_path_alias.rb