Skip to content

Commit

Permalink
Fixes #1012 - deletingAllPathComponents can now handle empty paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
guykogus committed Jun 11, 2022
1 parent 618f7de commit e267b8c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 10 additions & 6 deletions Sources/SwifterSwift/Foundation/URLExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// URLExtensions.swift - Copyright 2020 SwifterSwift
// URLExtensions.swift - Copyright 2022 SwifterSwift

#if canImport(Foundation)
import Foundation
Expand All @@ -14,7 +14,7 @@ public extension URL {
/// SwifterSwift: Dictionary of the URL's query parameters.
var queryParameters: [String: String]? {
guard let components = URLComponents(url: self, resolvingAgainstBaseURL: false),
let queryItems = components.queryItems else { return nil }
let queryItems = components.queryItems else { return nil }

var items: [String: String] = [:]

Expand All @@ -37,11 +37,11 @@ public extension URL {
guard let string = string else { return nil }
self.init(string: string, relativeTo: url)
}

/**
SwifterSwift: Initializes a forced unwrapped `URL` from string. Can potentially crash if string is invalid.
- Parameter unsafeString: The URL string used to initialize the `URL`object.
*/
SwifterSwift: Initializes a forced unwrapped `URL` from string. Can potentially crash if string is invalid.
- Parameter unsafeString: The URL string used to initialize the `URL`object.
*/
init(unsafeString: String) {
self.init(string: unsafeString)!
}
Expand Down Expand Up @@ -97,6 +97,8 @@ public extension URL {
///
/// - Returns: URL with all path components removed.
func deletingAllPathComponents() -> URL {
guard !pathComponents.isEmpty else { return self }

var url: URL = self
for _ in 0..<pathComponents.count - 1 {
url.deleteLastPathComponent()
Expand All @@ -110,6 +112,8 @@ public extension URL {
/// url.deleteAllPathComponents()
/// print(url) // prints "https://domain.com/"
mutating func deleteAllPathComponents() {
guard !pathComponents.isEmpty else { return }

for _ in 0..<pathComponents.count - 1 {
deleteLastPathComponent()
}
Expand Down
12 changes: 10 additions & 2 deletions Tests/FoundationTests/URLExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// URLExtensionsTests.swift - Copyright 2020 SwifterSwift
// URLExtensionsTests.swift - Copyright 2022 SwifterSwift

@testable import SwifterSwift
import XCTest
Expand Down Expand Up @@ -65,12 +65,20 @@ final class URLExtensionsTests: XCTestCase {
let url = URL(string: "https://domain.com/path/other/")!
let result = url.deletingAllPathComponents()
XCTAssertEqual(result.absoluteString, "https://domain.com/")

let pathlessURL = URL(string: "https://domain.com")!
let pathlessResult = pathlessURL.deletingAllPathComponents()
XCTAssertEqual(pathlessResult.absoluteString, "https://domain.com")
}

func testDeleteAllPathComponents() {
var url = URL(string: "https://domain.com/path/other/")!
url.deleteAllPathComponents()
XCTAssertEqual(url.absoluteString, "https://domain.com/")

var pathlessURL = URL(string: "https://domain.com")!
pathlessURL.deleteAllPathComponents()
XCTAssertEqual(pathlessURL.absoluteString, "https://domain.com")
}

#if os(iOS) || os(tvOS)
Expand Down Expand Up @@ -102,7 +110,7 @@ final class URLExtensionsTests: XCTestCase {
XCTAssertEqual(url.droppedScheme()?.absoluteString, expected, "input url: \(input)")
}
}

func testStringInitializer() throws {
let testURL = try XCTUnwrap(URL(string: "https://google.com"))
let extensionURL = URL(unsafeString: "https://google.com")
Expand Down

0 comments on commit e267b8c

Please sign in to comment.