Skip to content

Instantly share code, notes, and snippets.

View omidgolparvar's full-sized avatar

Omid Golparvar omidgolparvar

View GitHub Profile
extension Publisher {
var asAsyncThrowingStream: AsyncThrowingStream<Output, any Error> {
AsyncThrowingStream { continuation in
// Produce item using yield
let cancellable = self.sink { completion in
switch completion {
case .failure(let error):
continuation.finish(throwing: error)
/// NSHashTable
/// https://developer.apple.com/documentation/foundation/nshashtable
///
/// NSMapTable
/// https://developer.apple.com/documentation/foundation/nsmaptable
///
/// NSHash​Table & NSMap​Table
/// https://nshipster.com/nshashtable-and-nsmaptable/
///
extension PlaygroundPage {
var dataDirectoryURL: URL? {
let directory = playgroundSharedDataDirectory
.deletingLastPathComponent()
.appendingPathComponent("Shared Playground Data", isDirectory: true)
guard !FileManager.default.fileExists(atPath: directory.path(), isDirectory: nil)
else { return directory }
do {
struct KeyValue<Key, Value> {
let key: Key
let value: Value
}
extension KeyValue: Equatable where Key: Equatable, Value: Equatable {}
extension KeyValue: Hashable where Key: Hashable, Value: Hashable {}
extension KeyValue: Encodable where Key: Encodable, Value: Encodable {}
import Combine
protocol ValueDispatching<Output, Failure>: AnyObject {
associatedtype Output
associatedtype Failure: Error
func send(_ value: Output)
func send(error: Failure)
func completed()
}
public enum PasswordRule {
public enum CharacterClass {
case upper
case lower
case digits
case special
case asciiPrintable
case unicode
case custom(Set<Character>)
@omidgolparvar
omidgolparvar / EnablingTouchIDForAccessOnTerminal.bash
Created December 22, 2023 18:16
Enabling touch ID for access on Terminal
# https://labbots.com/enabling-touch-id-for-access-on-terminal/
# Edit this file /etc/pam.d/sudo with your favourite editor.
sudo vim /etc/pam.d/sudo
# Add the following line to the top of the file.
auth sufficient pam_tid.so
# To enable Touch ID access on Iterm2. You need to do the following.
# Go to Prefs -> Advanced -> Allow sessions to survive logging out and back in and set value to no.
@omidgolparvar
omidgolparvar / SwiftCombineMulticast.swift
Created December 22, 2023 17:16
Swift Combine Multicast Operator
import Combine
var storage = Set<AnyCancellable>()
let pub1 = Timer
.publish(every: 1, on: .main, in: .default)
.autoconnect()
let sub = CurrentValueSubject<Int, Never>(0)
@omidgolparvar
omidgolparvar / URLRequest+Extensions.swift
Created December 18, 2023 16:42
Swift URLRequest Extensions
extension URLRequest {
func prettyDescription() -> String? {
guard
let urlString = url?.absoluteString,
let methodString = method?.rawValue
else { return nil }
let headersString = headers.map(\.description).joined(separator: "\n")
let bodyString = if let httpBody {
String(data: httpBody, encoding: .utf8) ?? "-unknown-body-"