module Pakyow::Support::Refinements::String::Normalization

Public Instance Methods

collapse_path(path) click to toggle source

Collapses a string into a version without tokens.

String.collapse_path("/foo/:bar/baz")
# => "/foo/baz"
# File lib/pakyow/support/core_refinements/string/normalization.rb, line 36
def collapse_path(path)
  if path == "/"
    return path
  end

  path.to_s.split("/").keep_if { |part|
    part[0] != ":"
  }.join("/")
end
normalize_path(path) click to toggle source

Normalizes a string into a predictable path.

String.normalize_path("foo//bar/")
# => "/foo/bar"
# File lib/pakyow/support/core_refinements/string/normalization.rb, line 14
def normalize_path(path)
  path = path.to_s

  unless path.start_with?("/")
    path = "/#{path}"
  end

  if path.include?("//")
    path = path.to_s.gsub("//", "/")
  end

  unless path == "/"
    path = path.chomp("/")
  end

  path
end