Sha256: b72c523cfb838578466c1e92eb82ac4cf64ff03f2b5e95db839d5aba4315e1a2

Contents?: true

Size: 845 Bytes

Versions: 7

Compression:

Stored size: 845 Bytes

Contents

# frozen_string_literal: true
#

module Meta
  module Utils
    class Path
      class << self
        # 规范化 path 结构,确保 path 以 '/' 开头,不以 '/' 结尾。
        # 仅有一个例外,如果 path 为 nil 或空字符串,则返回空字符串 ''.
        def normalize_path(path)
          path = '/' unless path
          path = '/' + path unless path.start_with?('/')
          path = path.delete_suffix('/') if path.end_with?('/')
          path
        end

        # 合并两个 path. 有且只有一个例外,如果 p1 或 p2 其中之一为 '/',则返回另一个。
        def join(*parts)
          parts = parts.map { |p| (p || '').delete_prefix('/').delete_suffix('/') }
          parts = parts.reject { |p| p.nil? || p.empty? }
          '/' + parts.join('/')
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
meta-api-0.2.0 lib//meta/utils/path.rb
meta-api-0.1.2 lib//meta/utils/path.rb
meta-api-0.1.1 lib//meta/utils/path.rb
meta-api-0.1.0 lib//meta/utils/path.rb
meta-api-0.0.9 lib//meta/utils/path.rb
meta-api-0.0.8 lib/meta/utils/path.rb
meta-api-0.0.7 lib/meta/utils/path.rb