protocol-url 0.2.0 → 0.4.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/reference.rb +0 -8
- data/lib/protocol/url/relative.rb +24 -0
- data/lib/protocol/url/version.rb +1 -1
- data/readme.md +16 -0
- data/releases.md +16 -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: ff6accb882e5c993ee454290f6d282d67df6b4d13ee01bdd24b042ea764ee03c
|
|
4
|
+
data.tar.gz: abc0495ee011bccd902d3f60bebcf4d8231e3559b14305fe5594d62df285eeae
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1a1852872d62c65b0d78f8cd698deaaca69ceea65b79fb2e9d04b0b1b347cf6555ba41c27b0c836e2b963981f0c9be9a8feeec55622cdede7018a01321d7bca6
|
|
7
|
+
data.tar.gz: 4c6a01694dda0e059d944e5155f365c27c00c244e8e5ea7310288b875f2f307e4ef0c610dd8ea906e30fd5338d13f33fb1d2b83dcd5c076d57e34a60eb515b3b
|
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
|
|
@@ -95,14 +95,6 @@ module Protocol
|
|
|
95
95
|
[@path, @query, @fragment, @parameters]
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
-
# Compare two references.
|
|
99
|
-
#
|
|
100
|
-
# @parameter other [Reference] The other reference to compare.
|
|
101
|
-
# @returns [Integer] -1, 0, 1 if the reference is less than, equal to, or greater than the other reference.
|
|
102
|
-
def <=> other
|
|
103
|
-
to_ary <=> other.to_ary
|
|
104
|
-
end
|
|
105
|
-
|
|
106
98
|
# @returns [Boolean] Whether the reference has parameters.
|
|
107
99
|
def parameters?
|
|
108
100
|
@parameters and !@parameters.empty?
|
|
@@ -135,14 +135,38 @@ module Protocol
|
|
|
135
135
|
[@path, @query, @fragment]
|
|
136
136
|
end
|
|
137
137
|
|
|
138
|
+
def hash
|
|
139
|
+
to_ary.hash
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def equal?(other)
|
|
143
|
+
to_ary == other.to_ary
|
|
144
|
+
end
|
|
145
|
+
|
|
138
146
|
def <=>(other)
|
|
139
147
|
to_ary <=> other.to_ary
|
|
140
148
|
end
|
|
141
149
|
|
|
150
|
+
def ==(other)
|
|
151
|
+
to_ary == other.to_ary
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def ===(other)
|
|
155
|
+
to_s === other
|
|
156
|
+
end
|
|
157
|
+
|
|
142
158
|
def to_s
|
|
143
159
|
append
|
|
144
160
|
end
|
|
145
161
|
|
|
162
|
+
def as_json(...)
|
|
163
|
+
to_s
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def to_json(...)
|
|
167
|
+
as_json.to_json(...)
|
|
168
|
+
end
|
|
169
|
+
|
|
146
170
|
def inspect
|
|
147
171
|
"#<#{self.class} #{to_s}>"
|
|
148
172
|
end
|
data/lib/protocol/url/version.rb
CHANGED
data/readme.md
CHANGED
|
@@ -34,6 +34,22 @@ 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.4.0
|
|
38
|
+
|
|
39
|
+
- Add comparison methods to `Protocol::URL::Relative` (and by inheritance to `Protocol::URL::Absolute`):
|
|
40
|
+
- `#==` for structural equality comparison (compares path, query, fragment components).
|
|
41
|
+
- `#===` for string equality comparison (enables case statement matching).
|
|
42
|
+
- `#<=>` for ordering and sorting.
|
|
43
|
+
- `#hash` for hash key support.
|
|
44
|
+
- `#equal?` for component-based equality checking.
|
|
45
|
+
- Add JSON serialization support to `Protocol::URL::Relative`:
|
|
46
|
+
- `#as_json` returns the string representation.
|
|
47
|
+
- `#to_json` returns a JSON-encoded string.
|
|
48
|
+
|
|
49
|
+
### v0.3.0
|
|
50
|
+
|
|
51
|
+
- Add `relative(target, from)` for computing relative paths between URLs.
|
|
52
|
+
|
|
37
53
|
### v0.2.0
|
|
38
54
|
|
|
39
55
|
- Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
|
data/releases.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Releases
|
|
2
2
|
|
|
3
|
+
## v0.4.0
|
|
4
|
+
|
|
5
|
+
- Add comparison methods to `Protocol::URL::Relative` (and by inheritance to `Protocol::URL::Absolute`):
|
|
6
|
+
- `#==` for structural equality comparison (compares path, query, fragment components).
|
|
7
|
+
- `#===` for string equality comparison (enables case statement matching).
|
|
8
|
+
- `#<=>` for ordering and sorting.
|
|
9
|
+
- `#hash` for hash key support.
|
|
10
|
+
- `#equal?` for component-based equality checking.
|
|
11
|
+
- Add JSON serialization support to `Protocol::URL::Relative`:
|
|
12
|
+
- `#as_json` returns the string representation.
|
|
13
|
+
- `#to_json` returns a JSON-encoded string.
|
|
14
|
+
|
|
15
|
+
## v0.3.0
|
|
16
|
+
|
|
17
|
+
- Add `relative(target, from)` for computing relative paths between URLs.
|
|
18
|
+
|
|
3
19
|
## v0.2.0
|
|
4
20
|
|
|
5
21
|
- Move `Protocol::URL::PATTERN` to `protocol/url/pattern.rb` so it can be shared more easily.
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
|
Binary file
|