Skip to content

Instantly share code, notes, and snippets.

@masters3d
masters3d / failedJSEncoding.js
Created June 7, 2017 02:02
This does not work. Simple encode & decoder
// This would be hosted on the server but node does not support atob btoa.
function decode(toDecode){ // eslint-disable-line no-unused-vars
let salt = 'MySimpleSalt';
let decoded = atob(toDecode);
if (toDecode === btoa(decoded)){
return decoded.replace(salt,'');
}
throw new Error('Could not decode data');
}
@masters3d
masters3d / compareTwoMaps.ts
Created March 6, 2017 18:42
Compares two TypeScript / ES6 maps
function areMapsEqual(left: Map<number, string []>, right: Map<number, string []>): boolean {
if (left.size !== right.size) { return false }
const leftKeys = Array.from(left.keys()).sort()
const rightKeys = Array.from(right.keys()).sort()
if ( leftKeys !== rightKeys ) { return false }
for ( const key of leftKeys ) {
const leftValues = left.get(key) || []
const rightValues = right.get(key) || []
if ( leftValues.sort() !== rightValues.sort()) { return false }
@masters3d
masters3d / isAlertShowing.swift
Created September 29, 2016 22:05
IsAlertShowing
extension UIViewController {
var appDelegate:AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
var isAlertShowingComputed:Bool {
guard let window = self.appDelegate.window else { return false }
return !window.subviews.filter{
$0.isKind(of: UIAlertController.self)}.isEmpty
}
@masters3d
masters3d / pggrep.swift
Created April 20, 2016 17:33 — forked from erica/pggrep.swift
Find files in playgrounds, which are otherwise not indexed by spotlight
//
// main.swift
// pggrep - updated 12/4/15
// Created by Erica Sadun on 6/17/15.
// Copyright © 2015 Erica Sadun. All rights reserved.
//
import Foundation
extension String {
@masters3d
masters3d / defaultInitProtocol.swift
Created April 15, 2016 17:18
Default initializer from protocol/mix in swift
protocol Trying{
var uno:Int {get set}
var name:String{get set}
}
extension Trying{
init(unit:Int){
self.init()
self.name = ""
let hey = [1,2,23,3,4,5,5,6,6,7,7,8]
let slice = hey[4...10]
for each in slice.indices{
print(slice[each])
}
//Filter.swift
public mutating func next() -> Base.Element? {
var n: Base.Element?
for/*ever*/;; {
n = _base.next()
if n != nil ? _predicate(n!) : true {
return n
}
import fileinput
file = 'Directory'
filein = file + '.swift'
fileout = file + 'Edit' + '.swift'
def to_camel_case(snake_str):
components = snake_str.split('_')
return components[0] + "".join(x.capitalize() for x in components[1:])
enum Allergies:UInt {
static func hasAllergy(value:UInt, testAgainst: Allergies) -> Bool {
return value & testAgainst.rawValue > 0
}
case None = 0
case eggs = 1
case peanuts = 2
case shellfish = 4
case strawberries = 8
// http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift
extension CollectionType {
/// Return a copy of `self` with its elements shuffled
func shuffle() -> [Generator.Element] {
var list = Array(self)
list.shuffleInPlace()
return list
}
}