-
Notifications
You must be signed in to change notification settings - Fork 13
Added function for changing a query parameter in an URL #60
Conversation
merging master into develop
siemensikkema
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks handy! I made some suggestions that are hopefully a bit helpful...
| shouldUseLaunchSchemeArgsEnv = "YES" | ||
| codeCoverageEnabled = "NO"> | ||
| codeCoverageEnabled = "YES" | ||
| shouldUseLaunchSchemeArgsEnv = "YES"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these changes by accident?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I showed him how to enable code coverage. One could argue if it should be part of the entire framework or just set up locally
| - withName: The `String` representation of the name of the queryParameter you want to change the value of | ||
| - toValue: The `String` representation of the new value for the queryParameter | ||
| - returns: a new `URL` instance with the changed queryParameters or nil if the change failed | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tip: Xcode can help creating the doc section for a function. Editor > Structure > Add Documentation. Or Command Option /
It saves time and helps create a standardized format for docs.
| - toValue: The `String` representation of the new value for the queryParameter | ||
| - returns: a new `URL` instance with the changed queryParameters or nil if the change failed | ||
| */ | ||
| public func changeQueryParamValue(for url: URL, withName: String, toValue: String) -> URL? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can change the internal names for parameters like this:
public func changeQueryParamValue(for url: URL, withName name: String, toValue value: String) -> URL?
In the function withName and toValue don't make as much sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered changing the semantics from "change if it's there" to "add or replace"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you passing in the url parameter? You are already working on an URL right?
Here is an example from your test
let url = URL(string: "http://example.com?token=addtokenhere")
let queryParamUrl = url.changeQueryParamValue(for: url, withName: "token", toValue: "usertoken")
I would expect that I could call it like so instead:
let queryParamUrl = url.changeQueryParamValue(for: "token", to: "usertoken")
| if | ||
| var component = URLComponents(url: url, resolvingAgainstBaseURL: false), | ||
| var queryItems = component.queryItems, | ||
| var firstParam = component.queryItems?.filter({$0.name == withName}).first, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about: let index = queryItems.firstIndex(where: {$0.name == withName}) and then var queryItem = queryItems[index]
That uses the just unwrapped queryItems and doesn't go through the whole list before returning only the first item and it traverses the array only once (instead of first finding the query item and then finding the index of that query item).
| - toValue: The `String` representation of the new value for the queryParameter | ||
| - returns: a new `URL` instance with the changed queryParameters or nil if the change failed | ||
| */ | ||
| public func changeQueryParamValue(for url: URL, withName: String, toValue: String) -> URL? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you passing in the url parameter? You are already working on an URL right?
Here is an example from your test
let url = URL(string: "http://example.com?token=addtokenhere")
let queryParamUrl = url.changeQueryParamValue(for: url, withName: "token", toValue: "usertoken")
I would expect that I could call it like so instead:
let queryParamUrl = url.changeQueryParamValue(for: "token", to: "usertoken")
| shouldUseLaunchSchemeArgsEnv = "YES" | ||
| codeCoverageEnabled = "NO"> | ||
| codeCoverageEnabled = "YES" | ||
| shouldUseLaunchSchemeArgsEnv = "YES"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I showed him how to enable code coverage. One could argue if it should be part of the entire framework or just set up locally
| - returns: a new `URL` instance with the changed queryParameters or nil if the change failed | ||
| */ | ||
| public func changeQueryParamValue(for url: URL, withName: String, toValue: String) -> URL? { | ||
| var internalUrl = url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here you could just say var internalURL = self...you are adding this extension to the URL
| */ | ||
| public func changeQueryParamValue(for url: URL, withName: String, toValue: String) -> URL? { | ||
| var internalUrl = url | ||
| if let _ = internalUrl.value(forParameter: withName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about checking against nil instead?
if internalUrl.value(forParameter: name) != nil {
siemensikkema
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
pbodsk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, nothing further
🚀
Added Table of contents Added the source code to the README Added missing usage examples Added Description of functionality for each section
Testing approach for linking to section with emoji
Removing emojis from section headers because they break the linking in the table of contents
Updated REAME.md
fixes tests
Feature/migrate to swift 4
No description provided.