-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathHelpers.swift
41 lines (34 loc) · 921 Bytes
/
Helpers.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
import Foundation
import Helpers
/// Extracts parameters encoded in the URL both in the query and fragment.
func extractParams(from url: URL) -> [String: String] {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return [:]
}
var result: [String: String] = [:]
if let fragment = components.fragment {
let items = extractParams(from: fragment)
for item in items {
result[item.name] = item.value
}
}
if let items = components.queryItems {
for item in items {
result[item.name] = item.value
}
}
return result
}
private func extractParams(from fragment: String) -> [URLQueryItem] {
let components =
fragment
.split(separator: "&")
.map { $0.split(separator: "=") }
return
components
.compactMap {
$0.count == 2
? URLQueryItem(name: String($0[0]), value: String($0[1]))
: nil
}
}