Skip to content

Instantly share code, notes, and snippets.

View frzi's full-sized avatar
🍕
time

Freek frzi

🍕
time
View GitHub Profile
@frzi
frzi / uuid.ts
Created August 23, 2025 12:09
Primitive UUID in TypeScript
export function uuid(value?: string): bigint {
const uuid = value || crypto.randomUUID()
return BigInt('0x' + uuid.replace(/-/g, ''))
}
export function uuidString(uuid?: bigint): string {
if (!uuid) {
return crypto.randomUUID()
}
const str = uuid.toString(16).padStart(32, '0')
@frzi
frzi / ImagePlaceholder.swift
Last active August 19, 2025 10:45
LQIP view for SwiftUI
/**
* MIT License
* Copyright (c) 2025 frzi (github.com/frzi)
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@frzi
frzi / TypedEventTarget.ts
Last active August 23, 2025 11:59
Typed EventTarget for TypeScript
export default class TypedEventTarget<Events extends object> extends EventTarget {
override addEventListener<K extends keyof Events, U = Events[K] extends Event ? Events[K] : CustomEvent<Events[K]>>(
type: K,
listener: (this: this, event: U) => void,
options?: AddEventListenerOptions | boolean
) {
super.addEventListener(type as string, listener as any, options)
}
override dispatchEvent<K extends keyof Events>(type: K, detail: Events[K]): boolean
@frzi
frzi / WebView.swift
Created November 19, 2022 18:15
Simple WebView in SwiftUI
import Combine
import SwiftUI
import WebKit
struct WebView: NSViewRepresentable {
let url: URL
@Binding var title: String
private(set) var onCommitHandlers: [(URL) -> Void] = []
@frzi
frzi / BlankApp.swift
Last active April 21, 2022 18:17
Creating a blank window on macOS
import Cocoa
final class App {
private static var shared: App?
static func main() {
shared = App()
}
// MARK: -
@frzi
frzi / Blur.swift
Created November 22, 2021 17:22
Blur view for macOS
import SwiftUI
#if os(macOS)
import AppKit
struct Blur: NSViewRepresentable {
fileprivate var blending: NSVisualEffectView.BlendingMode
fileprivate var style: NSVisualEffectView.Material
/**
* Keychain.swift
* Created by frzi (github.com/frzi)
*/
import Foundation
import Security
class Keychain {
fileprivate class func getKeychainQuery(_ key: String) -> NSMutableDictionary {
@frzi
frzi / UserDefault.swift
Created March 11, 2021 11:51
UserDefault property wrapper
/// Example of usage:
/// ```swift
/// @UserDefault("username") var username: String? = nil
/// @UserDefault("maxItems") var maxItems = 100
/// ```
@propertyWrapper
struct UserDefault<T> {
let defaultValue: T
let key: String
private let environment: UserDefaults
@frzi
frzi / EventEmitter.ts
Last active February 9, 2021 21:06
Strongly typed Event Emitter
/**
* Custom written EventEmitter.
* Shares most of the same interface as Node.js' Event Emitter.
* Except everything is strongly typed and stored in `#listeners`.
*/
type Arguments<T> = [T] extends [(...args: infer U) => any] ? U : [T] extends [void] ? [] : [T]
type Listener<T> = (...argv: Arguments<T>) => void
interface DefaultEvents {
@frzi
frzi / discord-minesweeper.js
Last active August 23, 2023 14:07
Generate Minesweeper for Discord
// https://gist.github.com/frzi/5c397d396db3b94c281d57e803227db8
const MINE = -1
function generateSpaces(width = 9, height = 9, mines = 15) {
const totalSpaces = width * height
mines = Math.min(totalSpaces - 1, Math.abs(mines))
let spaces = new Array(totalSpaces).fill(0)
let openSpaces = spaces.map((_, index) => index)