Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 96598b5

Browse files
committedNov 9, 2020
Update PackageIdentity to account for URL query and fragment components
1 parent 3e62fab commit 96598b5

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
 

‎Sources/PackageModel/PackageIdentity.swift

+21
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public struct PackageIdentity: LosslessStringConvertible {
3535
string.replaceFirstOccurenceIfPresent(of: ":", with: "/")
3636
}
3737

38+
string.removeFragmentComponentIfPresent()
39+
string.removeQueryComponentIfPresent()
40+
3841
var components = string.split(omittingEmptySubsequences: true, whereSeparator: isSeparator)
3942

4043
var lastPathComponent = components.popLast() ?? ""
@@ -166,6 +169,24 @@ private extension String {
166169
return false
167170
}
168171

172+
@discardableResult
173+
mutating func removeFragmentComponentIfPresent() -> Bool {
174+
if let index = firstIndex(of: "#") {
175+
self.removeSubrange(index...)
176+
}
177+
178+
return false
179+
}
180+
181+
@discardableResult
182+
mutating func removeQueryComponentIfPresent() -> Bool {
183+
if let index = firstIndex(of: "?") {
184+
self.removeSubrange(index...)
185+
}
186+
187+
return false
188+
}
189+
169190
@discardableResult
170191
mutating func replaceFirstOccurenceIfPresent<T: StringProtocol, U: StringProtocol>(of string: T, with replacement: U) -> Bool {
171192
guard let range = range(of: string) else { return false }

‎Tests/PackageModelTests/PackageIdentityTests.swift

+49
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,55 @@ final class PackageIdentityTests: XCTestCase {
7171
)
7272
}
7373

74+
func testQuery() {
75+
XCTAssertEqual(
76+
PackageIdentity("https://example.com/mona/LinkedList?utm_source=forums.swift.org"),
77+
"example.com/mona/LinkedList"
78+
)
79+
}
80+
81+
func testQueryWithTrailingSlash() {
82+
XCTAssertEqual(
83+
PackageIdentity("https://example.com/mona/LinkedList/?utm_source=forums.swift.org"),
84+
"example.com/mona/LinkedList"
85+
)
86+
}
87+
88+
func testQueryWithGitSuffix() {
89+
XCTAssertEqual(
90+
PackageIdentity("https://example.com/mona/LinkedList.git?utm_source=forums.swift.org"),
91+
"example.com/mona/LinkedList"
92+
)
93+
}
94+
95+
func testFragment() {
96+
XCTAssertEqual(
97+
PackageIdentity("https://example.com/mona/LinkedList#installation"),
98+
"example.com/mona/LinkedList"
99+
)
100+
}
101+
102+
func testFragmentWithTrailingSlash() {
103+
XCTAssertEqual(
104+
PackageIdentity("https://example.com/mona/LinkedList/#installation"),
105+
"example.com/mona/LinkedList"
106+
)
107+
}
108+
109+
func testFragmentWithGitSuffix() {
110+
XCTAssertEqual(
111+
PackageIdentity("https://example.com/mona/LinkedList.git#installation"),
112+
"example.com/mona/LinkedList"
113+
)
114+
}
115+
116+
func testFragmentAndQuery() {
117+
XCTAssertEqual(
118+
PackageIdentity("https://example.com/mona/LinkedList.git#installation?utm_source=forums.swift.org"),
119+
"example.com/mona/LinkedList"
120+
)
121+
}
122+
74123
func testSSHScheme() {
75124
XCTAssertEqual(
76125
PackageIdentity("ssh://git@example.com/mona/LinkedList.git"),

0 commit comments

Comments
 (0)
Please sign in to comment.