Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Bosworth alexbosworth

View GitHub Profile
View urldecode_query_parameters.swift
var query = [String: String]()
if let queryString = url.query {
for q in queryString.componentsSeparatedByString("&") {
let pair = q.componentsSeparatedByString("=")
let value = pair[1]
.stringByReplacingOccurrencesOfString("+", withString: " ")
.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
View delay.swift
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
View gist:06c7861dc1a2196fdac8
/** Determine if value is present in array
*/
func contains<T: Equatable>(array: [T], element: T) -> Bool {
for elem in array { if elem == element { return true } }
return false
}
View links_header_parser.swift
View clean_phone_number.swift
let phoneNumber = "(408) 555-5270"
let cleanedPhoneNumber = phoneNumber.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).reduce("", +)
// 4085555270
@alexbosworth
alexbosworth / gist:2410117298c22dc971e3
Created Feb 13, 2015
UIAutomation, delay until network activity indicator is missing
View gist:2410117298c22dc971e3
/** Delay execution until status bar Network Activity is completed
{}
*/
automation.afterNetworkActivityEnds = function(args, cbk) {
var statusBar = target.frontMostApp().statusBar();
var predicate = "name beginswith 'Network'";
while (statusBar.elements().firstWithPredicate(predicate).isValid()) {
UIALogger.logMessage("Waiting for network");
@alexbosworth
alexbosworth / hour_floor.swift
Created Mar 20, 2015
Strip the minutes and seconds from a date, keeping yymmddhh
View hour_floor.swift
/** Strip minutes and seconds from a date
*/
func hourFloorFromDate(date: NSDate) -> NSDate? {
let calendar: NSCalendar
if let c = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) { calendar = c } else { return nil }
let units = NSCalendarUnit.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay | .CalendarUnitHour
return calendar.dateFromComponents(calendar.components(units, fromDate: date))
View tableModifications.swift
//
// TableModifications.swift
// mn_ios
//
// Created by Alex Bosworth on 4/8/15.
// Copyright (c) 2015 adylitica. All rights reserved.
//
import UIKit
View find_in_collection.swift
// Find the index of the first element found in a collection
func findInCollection<T: CollectionType where T.Generator.Element: Equatable>(collection: T, val: T.Generator.Element...) -> T.Index? {
for v in val { if let index = find(collection, v) { return index } }
return nil
}
@alexbosworth
alexbosworth / amazon-s3.js
Created Sep 27, 2010
A straightforward S3 library
View amazon-s3.js
/*
* Alex Bosworth
*
* A straightforward S3 library
*
* USE: var s3 = new S3(AWS_KEY, AWS_SECRET, {defaultBucket : MY_BUCKET});
* s3.put(KEY, DATA);
* s3.get(KEY).on('success', function(data) { console.log(data); });
* (more operations: buckets, info, list)
*