Skip to content

Instantly share code, notes, and snippets.

View brentsimmons's full-sized avatar

Brent Simmons brentsimmons

View GitHub Profile
@brentsimmons
brentsimmons / JavaScript Indent.js
Last active April 13, 2024 11:27
This is a BBEdit text filter for indenting (and beautifying) JavaScript.
#!/usr/local/bin/node
// PBS 12/5/13
// This is a BBEdit text filter for indenting (and beautifying) JavaScript.
// It goes in ~/Library/Application Support/BBEdit/Text Filters/
//
// On my machine I assigned it a keyboard shortcut: cmd-'
//
// It requires the js-beautify Node module: https://github.com/einars/js-beautify
//
@brentsimmons
brentsimmons / EmptyChecks.m
Created July 28, 2012 19:06
Checking if an object is empty
BOOL RSIsEmpty(id obj) {
return obj == nil || obj == [NSNull null] || ([obj respondsToSelector:@selector(length)] && [(NSData *)obj length] == 0) || ([obj respondsToSelector:@selector(count)] && [obj count] == 0);
}
BOOL RSStringIsEmpty(NSString *s) {
/*22 Feb. 2011: added NSNull check. JSON parser can put a null where we expect a string, and NSNull throws an exception when checking length. Since [NSNull null] is, arguably, emptiness, it makes sense to include it.*/
return s == nil || (id)s == (id)[NSNull null] || [s length] == 0;
}
// This is the traditional version of the RxSwift-based gist posted here:
// https://gist.github.com/cliss/51cb740b14f3cd56ba1d11f2a9a6ba02
// This won’t compile and it surely has errors.
// (The same may be true as the original.)
// Some things are obviously omitted.
// It's meant as illustrative rather than as actual running code.
//
// The problem being solved:
// There is a text field. When you type in it, all changes are coalesced
// for 0.3 seconds, and then an HTTP call is made, and a table is updated
@brentsimmons
brentsimmons / gist:5810992
Last active January 3, 2021 02:22
Detect a tap on a URL inside a UITextView. Note: the rs_links method isn't included -- you'll need something that takes text and returns an array of detected links. This gist just demonstrates walking through the UITextView characters.
@implementation UITextView (RSExtras)
static BOOL stringCharacterIsAllowedAsPartOfLink(NSString *s) {
/*[s length] is assumed to be 0 or 1. s may be nil.
Totally not a strict check.*/
if (s == nil || [s length] < 1)
return NO;
@brentsimmons
brentsimmons / SecretSanta.swift
Created September 25, 2015 17:30
My solution for the Omni Group’s Swift Bike Shedding Club week two question: http://rubyquiz.com/quiz2.html
//: Playground - noun: a place where people can play
import Cocoa
class Person: Hashable {
let firstName: String
let lastName: String
let email: String
#!/usr/bin/env ruby -wKU
# Comments added 6 March 2014.
# Implementation of the MetaWeblog API for my personal Ruby static blog generator.
# This is not even nearly idiomatic Ruby. There are all kinds of issues.
# (What's with the method interiors all being mushed-up together?)
# But -- it's also worked flawlessly for five years without my having to edit it.
# It won't work for anyone else as-is — but if it helps anyone
# to do a MetaWeblog API implementation, then cool.
@brentsimmons
brentsimmons / declarative_code.markdown
Last active April 30, 2019 14:16
A draft of a blog post that will never be published.

THIS IS A BLOG POST DRAFT THAT I WILL NEVER PUBLISH

INSTEAD I PLAN TO DO A SERIES OF SMALLER BLOG POSTS ON THE SUBJECT

The Objective-C Runtime and Declarative Programming

I write new code in Swift. It’s fun and I like it.

//: Playground - noun: a place where people can play
import Cocoa
protocol Account: Equatable {
var accountID: String {get}
}
// https://twitter.com/optshiftk/status/628985834801336320
@brentsimmons
brentsimmons / headerview.m
Created April 26, 2014 22:09
UITableView header issues
#pragma mark - Class Methods
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
#pragma mark - Init
- (id)initWithFrame:(CGRect)frame {
@brentsimmons
brentsimmons / googleSearchString.py
Created August 1, 2012 04:32
A script that generates a Google search URL and pastes it to the Mac OS X clipboard
#!/usr/bin/python
import sys
import os
from urllib import urlencode
import subprocess
# setClipboardData is from <http://www.macdrifter.com/2011/12/python-and-the-mac-clipboard/>*/
def setClipboardData(data):