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
| const net = require('net'); | |
| function findUnusedPort() { | |
| return new Promise(resolve => { | |
| const server = net.createServer(); | |
| // When the port is omitted, the OS provides an unused one. | |
| // https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback | |
| server.listen(() => { | |
| const { port } = server.address(); |
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
| typealias Reducer<StateType, ActionType> = (_ state: StateType, _ action: ActionType) -> StateType |
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
| import AppKit | |
| extension NSImage { | |
| /// Copies this image to a new one with a circular mask. | |
| func oval() -> NSImage { | |
| let image = NSImage(size: size) | |
| image.lockFocus() | |
| NSGraphicsContext.current?.imageInterpolation = .high |
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
| import Foundation | |
| class MyService: NSObject, MyServiceProtocol { | |
| func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) { | |
| let response = string.uppercased() | |
| reply(response) | |
| } | |
| } |
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
| import AppKit | |
| extension NSApplication { | |
| /// Restarts the application. | |
| func restart() { | |
| let process = Process() | |
| process.launchPath = "/bin/sh" | |
| process.arguments = ["-c", "sleep 1; open '\(Bundle.main.bundlePath)'"] | |
| process.launch() |
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
| import Cocoa | |
| extension NSMutableAttributedString { | |
| /// Makes the specified text a clickable hyperlink. | |
| func hyperlink(text: String, with url: URL, additionalAttributes: [NSAttributedStringKey: Any] = [:]) { | |
| var attributes = additionalAttributes | |
| attributes[.link] = url.absoluteString | |
| let range = (string as NSString).range(of: text) | |
| addAttributes(attributes, range: range) |
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
| // You don't necessarily need this subclass if your NSTextView is selectable. | |
| // If it isn't though, this allows you to have an uneditable, unselectable label where links work as expected. | |
| import AppKit | |
| class HyperlinkTextView: NSTextView { | |
| override func mouseDown(with event: NSEvent) { | |
| super.mouseDown(with: event) | |
| openClickedHyperlink(with: event) |
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
| extension Collection { | |
| /// Returns the element at the specified index if it is within bounds, or nil if it's outside. | |
| subscript(safe index: Index) -> Iterator.Element? { | |
| return indices.contains(index) ? self[index] : nil | |
| } | |
| } |
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
| import Foundation | |
| extension String { | |
| /// Finds and returns the ranges of occurrences of a given string within a given range of the `String`. | |
| func ranges(of searchString: String, options: CompareOptions = [], range searchRange: Range<Index>? = nil, locale: Locale? = nil) -> [Range<Index>] { | |
| let searchRange = searchRange ?? startIndex..<endIndex | |
| if let foundRange = range(of: searchString, options: options, range: searchRange, locale: locale) { | |
| let nextRange = foundRange.upperBound..<searchRange.upperBound |