Skip to content

Instantly share code, notes, and snippets.

@KyLeggiero
Last active September 21, 2018 22:46
Show Gist options
  • Save KyLeggiero/b2067317ab253aedeab9c3b0ca4270c1 to your computer and use it in GitHub Desktop.
Save KyLeggiero/b2067317ab253aedeab9c3b0ca4270c1 to your computer and use it in GitHub Desktop.
Testing ambiguously-named and -documented URL functions
Control:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => ~/ -- file:///
~/Desktop -- file:/// => ~/Desktop -- file:///
~/.bash_profile -- file:/// => ~/.bash_profile -- file:///
./ -- file:/// => ./ -- file:///
../ -- file:/// => ../ -- file:///
file:///etc/../ => file:///etc/../
~/../ -- file:/// => ~/../ -- file:///
/ => /
~/ => ~/
//example.com => //example.com
https://example.com => https://example.com
https://example.com/ => https://example.com/
https://example.com:443/ => https://example.com:443/
https://example.com/resource?query=true#fragment => https://example.com/resource?query=true#fragment
.standardized:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => ~/ -- file:///
~/Desktop -- file:/// => ~/Desktop -- file:///
~/.bash_profile -- file:/// => ~/.bash_profile -- file:///
./ -- file:/// => -- file:///
../ -- file:/// => -- file:///
file:///etc/../ => file:///
~/../ -- file:/// => -- file:///
/ => /
~/ => ~/
//example.com => example.com
https://example.com => https://example.com
https://example.com/ => https://example.com/
https://example.com:443/ => https://example.com:443/
https://example.com/resource?query=true#fragment => https://example.com/resource?query=true#fragment
.absoluteURL:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => file:///~/
~/Desktop -- file:/// => file:///~/Desktop
~/.bash_profile -- file:/// => file:///~/.bash_profile
./ -- file:/// => file:///
../ -- file:/// => file:///../
file:///etc/../ => file:///etc/../
~/../ -- file:/// => file:///
/ => /
~/ => ~/
//example.com => //example.com
https://example.com => https://example.com
https://example.com/ => https://example.com/
https://example.com:443/ => https://example.com:443/
https://example.com/resource?query=true#fragment => https://example.com/resource?query=true#fragment
.standardizedFileURL:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => file:///~/
~/Desktop -- file:/// => file:///~/Desktop
~/.bash_profile -- file:/// => file:///~/.bash_profile
./ -- file:/// => file:///
../ -- file:/// => file:///
file:///etc/../ => file:///private/
~/../ -- file:/// => file:///
/ => /
~/ => ~/
//example.com => //example.com
https://example.com => https://example.com
https://example.com/ => https://example.com/
https://example.com:443/ => https://example.com:443/
https://example.com/resource?query=true#fragment => https://example.com/resource?query=true#fragment
.absoluteURL.standardized:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => file:///~/
~/Desktop -- file:/// => file:///~/Desktop
~/.bash_profile -- file:/// => file:///~/.bash_profile
./ -- file:/// => file:///
../ -- file:/// => file://
file:///etc/../ => file:///
~/../ -- file:/// => file:///
/ => /
~/ => ~/
//example.com => example.com
https://example.com => https://example.com
https://example.com/ => https://example.com/
https://example.com:443/ => https://example.com:443/
https://example.com/resource?query=true#fragment => https://example.com/resource?query=true#fragment
.absoluteURL.standardizedFileURL:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => file:///~/
~/Desktop -- file:/// => file:///~/Desktop
~/.bash_profile -- file:/// => file:///~/.bash_profile
./ -- file:/// => file:///
../ -- file:/// => file:///
file:///etc/../ => file:///private/
~/../ -- file:/// => file:///
/ => /
~/ => ~/
//example.com => //example.com
https://example.com => https://example.com
https://example.com/ => https://example.com/
https://example.com:443/ => https://example.com:443/
https://example.com/resource?query=true#fragment => https://example.com/resource?query=true#fragment
import Cocoa
func largest<T: Comparable>(a: T, b: T) -> T { // necessary because of Sequence.max
return a < b ? b : a
}
extension String {
subscript (r: Range<Int>) -> String {
let start = index(startIndex, offsetBy: r.lowerBound)
let end = index(startIndex, offsetBy: r.upperBound)
return self[start..<end]
}
}
extension Sequence where Iterator.Element == URL? {
func debugMap<Output: CustomStringConvertible>(prefix: String = "Tested URLs", minPadding: Int = 50, _ mapper: @escaping (Iterator.Element) -> Output?) -> [Output?] {
let paddingString = (0..<minPadding).reduce("") { $0.0 + " " } + " \t"
let (debugString, newArray) =
self.reduce((debugString: "\(prefix):", newArray: [Output?]()))
{ (accumulator: (debugString: String, array: [Output?]), current) in
let newValue = mapper(current)
let currentDescription = current?.description ?? "(null)"
let estimatedPadding = minPadding - currentDescription.characters.count
let necessaryPadding: Int = largest(a: 0, b: estimatedPadding)
let paddingString = necessaryPadding <= 0 ? "" : paddingString[0..<necessaryPadding]
let newDebugString = accumulator.debugString + "\n\t\(currentDescription) \(paddingString) => \t \t \(newValue?.description ?? "(null)")"
let newArray = accumulator.array + [newValue]
return (newDebugString, newArray)
}
print("\(debugString)\n\n")
return newArray
}
}
let urls = [
URL(fileURLWithPath: "/"),
URL(fileURLWithPath: "/etc/"),
URL(fileURLWithPath: "~/"),
URL(fileURLWithPath: "~/Desktop"),
URL(fileURLWithPath: "~/.bash_profile"),
URL(fileURLWithPath: "./"),
URL(fileURLWithPath: "../"),
URL(fileURLWithPath: "/etc/../"),
URL(fileURLWithPath: "~/../"),
URL(string: "/"),
URL(string: "~/"),
URL(string: "//example.com"),
URL(string: "https://example.com"),
URL(string: "https://example.com/"),
URL(string: "https://example.com:443/"),
URL(string: "https://example.com/resource?query=true#fragment")
]
urls.debugMap(prefix: "Control") { $0 }
urls.debugMap(prefix: ".standardized") { $0?.standardized }
urls.debugMap(prefix: ".absoluteURL") { $0?.absoluteURL }
urls.debugMap(prefix: ".standardizedFileURL") { $0?.standardizedFileURL }
urls.debugMap(prefix: ".absoluteURL.standardized") { $0?.absoluteURL.standardized }
urls.debugMap(prefix: ".absoluteURL.standardizedFileURL") { $0?.absoluteURL.standardizedFileURL }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment