Skip to content

Instantly share code, notes, and snippets.


Sindre Sorhus sindresorhus

View GitHub Profile
sindresorhus / IdentifiableByHashable.swift
Created Mar 10, 2021
Make a type `Identifiable` based on its `Hashable` hash value.
View IdentifiableByHashable.swift
Make a type `Identifiable` based on its `Hashable` hash value.
This can be useful when all the properties are required to represent its identifier.
struct Item: Hashable, IdentifiableByHashable {
let title: String
var message: String?
View Image++.swift
extension Image {
Image(nsImageNamed: NSImage.addTemplateName)
- Note: The `size` parameter is there as resizing in SwiftUI creates a blurry image. Seems like a bug:
Image(nsImageNamed: NSImage.addTemplateName)

Pure ESM package

The package linked to from here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
sindresorhus / MenuBarApp.swift
Last active Jul 18, 2020
Dream API for macOS menu bar apps
View MenuBarApp.swift
struct MyApp: App {
var body: some Scene {
StatusBar {
StatusItem(id: "foo", systemImage: "gear") {
Menu {
Button("Toggle") {}
Button("Quit") {}
View swiftui_helper_property_wrapper.swift
import Combine
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Model<Value>: DynamicProperty {
private final class _Box: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var value: Value {
sindresorhus / EmptyInitializable.swift
Created Jan 7, 2020
A type that can be initialized without any parameters.
View EmptyInitializable.swift
/// Types that can be initialized without any parameters.
/// Useful if you need to accept an array of metatypes and then initialize them.
protocol EmptyInitializable {
extension Int: EmptyInitializable {}
extension Int8: EmptyInitializable {}
extension Int16: EmptyInitializable {}
extension Int32: EmptyInitializable {}
sindresorhus / NativeButton.swift
Created Oct 11, 2019
Example of using NSButton in SwiftUI to access missing features like `keyEquivalent` (for example, to make the button the default and highlighted). Stack Overflow answer:
View NativeButton.swift
struct ContentView: View {
var body: some View {
NativeButton("Submit", keyEquivalent: .return) {
// Some action
sindresorhus / ShowForAppRuns.swift
Created Oct 10, 2019
SwiftUI modifier to show a view only a given amount of app runs.
View ShowForAppRuns.swift
// Depends on
@available(macOS 10.15, *)
private struct ShowForAppRuns: ViewModifier {
private static var runCounts = [String: Int]()
private let count: Int
private let startShowingFromAppRun: Int
private let runCount: Int
init(count: Int, id: String, startShowingFromAppRun: Int = 1) {
View puppeteer-with-adblock.js
'use strict';
const puppeteer = require('puppeteer');
(async () => {
0. download ublock, I used
1. run $PATH_TO_CHROME --user-data-dir=/some/empty/directory --load-extension=/location/of/ublock
2. enable block lists you want to use