@@ -34,12 +34,12 @@ public extension URL {
34
34
Adds height, width and mode paramters to an url. To be used when fetching an image from a CDN, for example.
35
35
Choose the `size` and the `mode` for the image url to define how an image will be provided from the backend.
36
36
37
- - Parameters :
38
- - size: Set `size` as `CGSize` to define the size of the image that will be provided.
39
- - mode: Select a mode from predefined `ImageUrlMode` to set up a mode and define how an image will be provided.
40
- - heightParameterName: the name of the height paramter. Default is 'h'
41
- - widthParameterName: the name of the width paramter. Default is 'h'
42
- - Returns : `URL` as a `NSURL`.
37
+ - parameters :
38
+ - size: Set `size` as `CGSize` to define the size of the image that will be provided.
39
+ - mode: Select a mode from predefined `ImageUrlMode` to set up a mode and define how an image will be provided.
40
+ - heightParameterName: the name of the height paramter. Default is 'h'
41
+ - widthParameterName: the name of the width paramter. Default is 'h'
42
+ - returns : `URL` as a `NSURL`.
43
43
*/
44
44
public func appendingAssetSize( _ size: CGSize , mode: ImageUrlMode = . default, heightParameterName : String = " h " , widthParameterName : String = " w " ) -> URL ? {
45
45
guard var urlComponents = URLComponents ( url: self , resolvingAgainstBaseURL: false ) else { return nil }
@@ -55,10 +55,10 @@ public extension URL {
55
55
}
56
56
57
57
/**
58
- Finds the first value for a URL parameter in a `URL`
59
- - Parameters :
58
+ Finds the first value for a URL parameter in a `URL`
59
+ - parameters :
60
60
- name: the URL parameter to look for
61
- - Returns : the first value found for `name` or nil if no value was found
61
+ - returns : the first value found for `name` or nil if no value was found
62
62
*/
63
63
public func value( forParameter name: String ) -> String ? {
64
64
guard let urlComponents = URLComponents ( url: self , resolvingAgainstBaseURL: true ) ,
@@ -68,5 +68,22 @@ public extension URL {
68
68
let items = queryItems. filter ( { $0. name == name } )
69
69
return items. first? . value
70
70
}
71
-
71
+
72
+ /**
73
+ Appends queryParameters to a `URL`
74
+ - parameters:
75
+ - queryParameters: a `String` : `String` dictionary containing the queryParameters to append
76
+ - returns: a new `URL` instance with the appended queryParameters or nil if the appending failed
77
+ */
78
+ public func append( queryParameters: [ String : String ] ) -> URL ? {
79
+ guard var urlComponents = URLComponents ( url: self , resolvingAgainstBaseURL: true ) else {
80
+ return nil
81
+ }
82
+
83
+ let urlQueryItems = queryParameters. map {
84
+ return URLQueryItem ( name: $0, value: $1)
85
+ }
86
+ urlComponents. queryItems = urlQueryItems
87
+ return urlComponents. url
88
+ }
72
89
}
0 commit comments