Created
May 23, 2017 07:41
-
-
Save frzi/c293577469cfeb9614248fed38c643e0 to your computer and use it in GitHub Desktop.
Javascript's Console functions in Swift.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fuck it. | |
private func makeString(prefix: String? = nil, values: [Any]) -> String { | |
var str = prefix ?? "" | |
for val in values { | |
str += "\(val) " | |
} | |
return str | |
} | |
public final class Console { | |
private var timesTable: [String : CFTimeInterval] = [:] | |
private var labels: [String : Int] = [:] | |
@discardableResult | |
func log(_ vals: Any...) -> Self { | |
print(makeString(values: vals)) | |
return self | |
} | |
@discardableResult | |
func count(_ label: String) -> Self { | |
if let count = labels[label] { | |
labels[label] = count + 1 | |
print("\(label): \(count + 1)") | |
} | |
else { | |
labels[label] = 1 | |
print("\(label): 1") | |
} | |
return self | |
} | |
@discardableResult | |
func info(_ vals: Any...) -> Self { | |
print(makeString(prefix: "ℹ️", values: vals)) | |
return self | |
} | |
@discardableResult | |
func warn(_ vals: Any...) -> Self { | |
print(makeString(prefix: "⚠️", values: vals)) | |
return self | |
} | |
@discardableResult | |
func error(_ vals: Any...) -> Self { | |
print(makeString(prefix: "🛑", values: vals)) | |
return self | |
} | |
@discardableResult | |
func time(_ label: String) -> Self { | |
timesTable[label] = CACurrentMediaTime() | |
return self | |
} | |
@discardableResult | |
func timeEnd(_ label: String) -> Self { | |
if let time = timesTable[label] { | |
let now = CACurrentMediaTime() | |
print("🕐 \(now - time)") | |
} | |
return self | |
} | |
} | |
public let console = Console() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment