protocol-url 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/protocol/url/path.rb +39 -0
- data/lib/protocol/url/version.rb +1 -1
- data/readme.md +4 -0
- data/releases.md +4 -0
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12a81d3a05c932207c77a583df784431d7120f3f1f9ee68c9ea593a47c666831
|
|
4
|
+
data.tar.gz: 3307c15e0d16d4907259cd186c0483d46d78358e0b1bfee99e3cf3186a1c74d2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8e948fff9470b6676b5b28638af7fdc64eaa31e3565c980071a539eadbf3651b8c10037ed1bcec70b43360a30fbb595f14bf965898e6ab4f1b4e8baf1e942491
|
|
7
|
+
data.tar.gz: 86de1d30699c4c3f7c85878ca3c0edf45da61991593d4600bfc83be2b667525ccc8bb01ea5a51ddf82248045933b49a3ef8879947100a5ac88d67d94a2947786
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/lib/protocol/url/path.rb
CHANGED
|
@@ -119,6 +119,45 @@ module Protocol
|
|
|
119
119
|
return join(simplify(components))
|
|
120
120
|
end
|
|
121
121
|
|
|
122
|
+
# Calculate the relative path from one absolute path to another.
|
|
123
|
+
#
|
|
124
|
+
# This is useful for generating relative URLs from one location to another,
|
|
125
|
+
# such as creating page-specific import maps or relative links.
|
|
126
|
+
#
|
|
127
|
+
# @parameter target [String] The destination path (where you want to go).
|
|
128
|
+
# @parameter from [String] The source path (where you are starting from).
|
|
129
|
+
# @returns [String] The relative path from `from` to `target`.
|
|
130
|
+
#
|
|
131
|
+
# @example Calculate relative path between pages.
|
|
132
|
+
# Path.relative("/_components/app.js", "/foo/bar/")
|
|
133
|
+
# # => "../../_components/app.js"
|
|
134
|
+
#
|
|
135
|
+
# @example Calculate relative path in same directory.
|
|
136
|
+
# Path.relative("/docs/guide.html", "/docs/index.html")
|
|
137
|
+
# # => "guide.html"
|
|
138
|
+
def self.relative(target, from)
|
|
139
|
+
target_components = split(target)
|
|
140
|
+
from_components = split(from)
|
|
141
|
+
|
|
142
|
+
# Remove the last component from 'from' to get the directory
|
|
143
|
+
from_components = from_components[0...-1] if from_components.size > 0
|
|
144
|
+
|
|
145
|
+
# Find the common prefix
|
|
146
|
+
common_length = 0
|
|
147
|
+
[target_components.size, from_components.size].min.times do |i|
|
|
148
|
+
break if target_components[i] != from_components[i]
|
|
149
|
+
common_length = i + 1
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Calculate how many levels to go up
|
|
153
|
+
up_levels = from_components.size - common_length
|
|
154
|
+
|
|
155
|
+
# Build the relative path components
|
|
156
|
+
relative_components = [".."] * up_levels + target_components[common_length..-1]
|
|
157
|
+
|
|
158
|
+
return join(relative_components)
|
|
159
|
+
end
|
|
160
|
+
|
|
122
161
|
# Convert a URL path to a local file system path using the platform's file separator.
|
|
123
162
|
#
|
|
124
163
|
# This method splits the URL path on `/` characters, unescapes each component using
|
data/lib/protocol/url/version.rb
CHANGED
data/readme.md
CHANGED
|
@@ -34,6 +34,10 @@ This project is best served by a collaborative and respectful environment. Treat
|
|
|
34
34
|
|
|
35
35
|
Please see the [project releases](https://github.com/socketry/protocol-urlreleases/index) for all releases.
|
|
36
36
|
|
|
37
|
+
### v0.3.0
|
|
38
|
+
|
|
39
|
+
- Add `relative(target, from)` for computing relative paths between URLs.
|
|
40
|
+
|
|
37
41
|
### v0.2.0
|
|
38
42
|
|
|
39
43
|
- Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
|
data/releases.md
CHANGED
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
|
Binary file
|