Skip to content

Instantly share code, notes, and snippets.

View marcuswestin's full-sized avatar

Marcus Westin marcuswestin

  • New York
View GitHub Profile
@marcuswestin
marcuswestin / isTouch.js
Created May 30, 2012 00:15
Detect if the current device is a touch device
var isTouch = (function() {
try { return document.createEvent("TouchEvent") && ('ontouchstart' in window) }
catch(e) { return false }
})();
@marcuswestin
marcuswestin / git-count-commits.sh
Created June 15, 2012 01:22
How many git commits did I make today?
git log --pretty=oneline --author=Marcus --since=yesterday | wc
@marcuswestin
marcuswestin / monkeypatch-jquery-append.js
Created June 18, 2012 23:57
Monkey-patch jquery $('.foo').append(...) to accept variable number arguments and arrays of elements
var originalAppend = $.fn.append
$.fn.append = function() {
if (arguments.length == 1) {
var arg = arguments[0]
if ($.isArray(arg)) {
for (var i=0; i<arg.length; i++) { this.append(arg[i]) }
} else {
originalAppend.call(this, arg)
}
} else {
@marcuswestin
marcuswestin / upload-images.js
Created June 27, 2012 01:17
Upload all the images of a page to s3
/*
# First, install node and:
sudo npm install jsdom
sudo npm install aws2js
sudo npm install request
*/
@marcuswestin
marcuswestin / scrape-crunchbase-funding-rounds.js
Created June 27, 2012 21:28
Stay up to date on funding rounds
// sudo npm install request && sudo npm install jsdom
var request = require('request')
var jsdom = require('jsdom')
var pages = process.argv.slice(2)
if (!pages.length) { pages = [0] }
console.error('Scrape Crunchbase funding rounds. Pages:', pages)
@marcuswestin
marcuswestin / gitpush.sh
Last active October 8, 2015 18:58
These scripts are now maintained at https://github.com/marcuswestin/git-star
# See https://github.com/marcuswestin/git-star for new version of this script
#!/bin/bash
set -e # die on error
cd $(git rev-parse --show-toplevel)
REMOTE="origin"
BRANCH=`git branch --no-color | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
@marcuswestin
marcuswestin / colorize-node.js
Last active October 9, 2015 02:58
Add color to your node.js scripts
'use strict'
;(function colorizeNode(){
var colors = {
white:1, black:30, blue:34, pink:35, cyan:36,
red:31, redLight:91,
green:32, greenLight:92,
yellow:33, yellowLight:93,
gray:90, grayLight:37,
@marcuswestin
marcuswestin / stats.js
Created September 7, 2012 20:18
Statistics functions
// std
//////
function isArray(obj) { return Object.prototype.toString.call(obj) == '[object Array]' }
function isArguments(obj) { return Object.prototype.toString.call(obj) == '[object Arguments]' }
function slice(arr, i) { return Array.prototype.slice.call(arr, i || 0) }
function curry(fn /*, args ...*/) {
var args = slice(arguments, 1)
return function() { return fn.apply(this, args.concat(slice(arguments))) }
}
function each(obj, fn) {
@marcuswestin
marcuswestin / gist:3813026
Created October 1, 2012 17:02
Code to match parts of a url using NSPredicates
NSURL* url = [NSURL URLWithString:@"http://www.google.com/path/to/foo?aParam=value"];
url.absoluteString;
url.host;
url.scheme;
url.path;
NSArray* arr = [NSArray arrayWithObjects:url, [NSURL URLWithString:@"https://marcuswest.in/teachers"], nil];
NSLog(@"scheme %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"scheme MATCHES 'http'"]]);
NSLog(@"host %@", [arr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"host MATCHES 'www.google.com'"]]);
@marcuswestin
marcuswestin / gist:3831043
Created October 4, 2012 01:57
NSURLConnection example
- (void)startLoading {
self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self];
if (!self.connection) {
[self.client URLProtocol:self didFailWithError:nil];
}
}
- (void)stopLoading {
[self.connection cancel];
self.connection = nil;