Skip to content

Instantly share code, notes, and snippets.

Shai Mishali freak4pc

  • Gett
  • Tel Aviv, Israel
View GitHub Profile
@freak4pc
freak4pc / RxArray.swift
Created Oct 5, 2018
RxArray - A basic Reactive RxSwift array
View RxArray.swift
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 Sep 7, 2018
Simple (Naive) Rx Cart
View Cart.swift
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 Aug 26, 2018
Detect text messages from QR code
View UIImage+QR.swift
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 Jul 16, 2017
Optional Unwrapping with Closures
View OptionalType+Exts.swift
public protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
public var value: Wrapped? {
return self
}
@freak4pc
freak4pc / MKMapView+FitCoords.swift
Created Feb 12, 2017
Display list of coordinates / annotation correctly inside a map
View MKMapView+FitCoords.swift
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 Apr 5, 2019
Get a list of coordinates from a MKPolyline / MKRoute
View MKMultiPoint+Ext.swift
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 Nov 2, 2018
Israeli ID Validator (Javascript)
View IsraeliID.Validator.js
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) => {
const step = digit * ((i % 2) + 1);
@freak4pc
freak4pc / .zshrc
Created May 20, 2015
History that accepts a grep filter inline as an argument
View .zshrc
function __history {
[ -n "$1" ] && history 1 | grep -i $1 || history 1
}
alias history='__history'
You can’t perform that action at this time.