-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURL+Extensions.swift
113 lines (94 loc) · 4.03 KB
/
URL+Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// URL+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import UIKit
extension URL {
/// `try URL.oy_init(string: "https://www.apple.com")`
public static func oy_init(string: String) throws -> Self? {
guard let url = URL(string: string), url.oy_isValid else { throw OYError.urlIsNotValid }
return url
}
/// `url.oy_isValid` → output → true
public var oy_isValid: Bool {
Self.oy_isValid(absoluteString)
}
/// `URL.oy_isValid("https://www.apple.com/mac")` → output → true
public static func oy_isValid(_ string: String) -> Bool {
if let url = URL(string: string) {
return UIApplication.shared.canOpenURL(url)
}
return false
}
/// `url.oy_nonScheme` → output → "www.apple.com/mac"
public var oy_nonScheme: String? {
if let scheme = scheme {
let nonScheme = String(absoluteString.dropFirst(scheme.count + 3))
return nonScheme
}
return nil
}
/// `url.oy_domain` → output → "https://www.apple.com"
public var oy_domain: String? {
guard !pathComponents.isEmpty, var nonQueryItemsUrl = self.oy_nonQueryItems else { return nil }
for _ in 0..<pathComponents.count - 1 {
nonQueryItemsUrl.deleteLastPathComponent()
}
return nonQueryItemsUrl.absoluteString
}
/// `url.oy_queryItems` → output → [key : "lang" value : "en"]
public var oy_queryItems: [String: Any] {
var items: [String: Any] = [:]
guard let components = URLComponents(url: self, resolvingAgainstBaseURL: true), let queryItems = components.queryItems else { return items }
queryItems.forEach { items[$0.name] = $0.value }
return items
}
/// `url.oy_nonQueryItems` → output → "https://www.apple.com"
public var oy_nonQueryItems: URL? {
var components = URLComponents()
components.scheme = scheme
components.host = host
components.port = port
components.path = path
return components.url
}
/// `URL(string: "https://example.com?abc=12&def=34").oy_queryItem(name: "abc")` → output → 12
public func oy_queryItem(name: String) -> String? {
URLComponents(string: absoluteString)?.queryItems?.first { $0.name.caseInsensitiveCompare(name) == .orderedSame }?.value
}
/// `URL(string: "https://example.com").oy_appendQueryItem(name: "abc", value: 12)` → output → "https://example.com?abc=12
public func oy_appendQuery(name: String, value: Any?) -> URL {
guard var urlComponents = URLComponents(string: absoluteString) else {
return self
}
urlComponents.queryItems = urlComponents.queryItems?.filter { $0.name.caseInsensitiveCompare(name) != .orderedSame } ?? []
guard let value else { return self }
urlComponents.queryItems?.append(URLQueryItem(name: name, value: "\(value)"))
return urlComponents.url ?? self
}
/// `URL(string: "https://example.com").oy_appendQuery(items: ["abc": 12])` → output → "https://example.com?abc=12
public func oy_appendQuery(items: [String: Any?]) -> URL {
guard var urlComponents = URLComponents(string: absoluteString), !items.isEmpty else {
return self
}
let keys = items.keys.map { $0.lowercased() }
urlComponents.queryItems = urlComponents.queryItems?.filter { !keys.contains($0.name.lowercased()) } ?? []
urlComponents.queryItems?.append(contentsOf: items.compactMap {
guard let value = $0.value else { return nil }
return URLQueryItem(name: $0.key, value: "\(value)")
})
return urlComponents.url ?? self
}
/// `URL(string: "https://example.com?abc=12").oy_removeQuery(name: "abc")` → output → "https://example.com
public func oy_removeQuery(name: String) -> URL {
oy_appendQuery(name: name, value: nil)
}
}
extension String {
/// `"https://www.google.com.tr".oy_url`
public var oy_toUrl: URL? {
URL(string: self)
}
}