|
| 1 | +// |
| 2 | +// JSConsole.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 6/3/20. |
| 6 | +// |
| 7 | + |
| 8 | +public final class JSConsole: JSType { |
| 9 | + |
| 10 | + // MARK: - Properties |
| 11 | + |
| 12 | + public let jsObject: JSObjectRef |
| 13 | + |
| 14 | + // MARK: - Initialization |
| 15 | + |
| 16 | + public init(_ jsObject: JSObjectRef) { |
| 17 | + self.jsObject = jsObject |
| 18 | + } |
| 19 | + |
| 20 | + // MARK: - Methods |
| 21 | + |
| 22 | + /** |
| 23 | + The Console method `log()` outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects. |
| 24 | + */ |
| 25 | + public static func log(_ message: String) { |
| 26 | + logFunction(message) |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + The `console.info()` method outputs an informational message to the Web Console. In Firefox, a small "i" icon is displayed next to these items in the Web Console's log. |
| 31 | + */ |
| 32 | + public static func info(_ message: String) { |
| 33 | + infoFunction(message) |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + The console method `debug()` outputs a message to the web console at the "debug" log level. The message is only displayed to the user if the console is configured to display debug output. |
| 38 | + */ |
| 39 | + public static func debug(_ message: String) { |
| 40 | + debugFunction(message) |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + Outputs an error message to the Web Console. |
| 45 | + */ |
| 46 | + public static func error(_ message: String) { |
| 47 | + errorFunction(message) |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + The `console.assert()` method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens. |
| 52 | + */ |
| 53 | + public static func assert(_ condition: @autoclosure () -> (Bool), _ message: String) { |
| 54 | + assertFunction(condition(), message) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +internal extension JSConsole { |
| 59 | + |
| 60 | + static let classObject = JSObjectRef.global.console.object! |
| 61 | + |
| 62 | + static let logFunction = classObject.log.function! |
| 63 | + |
| 64 | + static let infoFunction = classObject.info.function! |
| 65 | + |
| 66 | + static let debugFunction = classObject.debug.function! |
| 67 | + |
| 68 | + static let errorFunction = classObject.error.function! |
| 69 | + |
| 70 | + static let assertFunction = classObject.assert.function! |
| 71 | +} |
0 commit comments