Skip to content

Instantly share code, notes, and snippets.

View freak4pc's full-sized avatar
🤓

Shai Mishali freak4pc

🤓
View GitHub Profile
@freak4pc
freak4pc / Combine+WithLatestFrom.swift
Last active February 19, 2024 15:35
withLatestFrom for Apple's Combine
//
// Combine+WithLatestFrom.swift
//
// Created by Shai Mishali on 29/08/2019.
// Copyright © 2019 Shai Mishali. All rights reserved.
//
import Combine
// MARK: - Operator methods
@freak4pc
freak4pc / RxArray.swift
Created October 5, 2018 11:05
RxArray - A basic Reactive RxSwift array
import Foundation
import RxSwift
struct RxArray<T: Hashable> {
private let storage: BehaviorRelay<[T]>
private let lock = NSRecursiveLock()
var items: Observable<[T]> {
return storage.asObservable()
}
@freak4pc
freak4pc / Cart.swift
Last active September 7, 2018 13:39
Simple (Naive) Rx Cart
struct Cart {
private let action = PublishRelay<Action>()
public let items: Observable<[Item]>
init() {
items = action
.scan([Item]()) { items, action in
switch action {
case .add(let item):
return items + [item]
@freak4pc
freak4pc / UIImage+QR.swift
Created August 26, 2018 08:37
Detect text messages from QR code
extension UIImage {
func parseQR() -> [String] {
guard let image = CIImage(image: self) else {
return []
}
let detector = CIDetector(ofType: CIDetectorTypeQRCode,
context: nil,
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
@freak4pc
freak4pc / OptionalType+Exts.swift
Last active February 4, 2020 11:11
Optional Unwrapping with Closures
public protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
public var value: Wrapped? {
return self
}
@freak4pc
freak4pc / MKMapView+FitCoords.swift
Created February 12, 2017 12:55
Display list of coordinates / annotation correctly inside a map
public extension MKMapView {
public static func visibleRect(for coords: [CLLocationCoordinate2D]) -> MKMapRect {
return coords.reduce(MKMapRectNull) { outRect, coord in
let point = MKMapPointForCoordinate(coord)
let rect = MKMapRectMake(point.x, point.y, 0.1, 0.1)
let union = MKMapRectUnion(rect, outRect)
return union
}
}
@freak4pc
freak4pc / MKMultiPoint+Ext.swift
Last active September 5, 2024 06:36
Get a list of coordinates from a MKPolyline / MKRoute
public extension MKMultiPoint {
var coordinates: [CLLocationCoordinate2D] {
var coords = [CLLocationCoordinate2D](repeating: kCLLocationCoordinate2DInvalid,
count: pointCount)
getCoordinates(&coords, range: NSRange(location: 0, length: pointCount))
return coords
}
}
@freak4pc
freak4pc / IsraeliID.Validator.js
Last active June 17, 2024 16:19
Israeli ID Validator (Javascript)
function isValidIsraeliID(id) {
var id = String(id).trim();
if (id.length > 9 || id.length < 5 || isNaN(id)) return false;
// Pad string with zeros up to 9 digits
id = id.length < 9 ? ("00000000" + id).slice(-9) : id;
return Array
.from(id, Number)
.reduce((counter, digit, i) => {
@freak4pc
freak4pc / .zshrc
Created May 20, 2015 09:15
History that accepts a grep filter inline as an argument
function __history {
[ -n "$1" ] && history 1 | grep -i $1 || history 1
}
alias history='__history'