Skip to content

Instantly share code, notes, and snippets.

View alexbosworth's full-sized avatar

Alex Bosworth alexbosworth

View GitHub Profile
@alexbosworth
alexbosworth / node-jquery.js
Created September 27, 2010 21:05
A port of jQuery to Node.js (required for all my other scripts)
// node-jquery - port of jquery 1.4.2 (http://jquery.com/)to node.js by alex bosworth (http://alexbosworth.net/)
function now() {
return (new Date).getTime();
}
var window = {},
jsc = now(),
rscript = /<script(.|\s)*?\/script>/gi,
rselectTextarea = /select|textarea/i,
@alexbosworth
alexbosworth / amazon-s3.js
Created September 27, 2010 21:21
A straightforward S3 library
/*
* 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)
*
@alexbosworth
alexbosworth / end_script_timer.js
Created October 7, 2010 06:21
a runtime calculator for scripts
// for node-jquery.js (http://gist.github.com/599831)
// add this to a script to show how long it took to complete
process.on('exit', $.proxy(function () {
var sec = Math.round((new Date().getTime() - this.start.getTime()) / 1000);
console.log('completed', 'in ' + sec + ' seconds');
}, {start:new Date()}));
@alexbosworth
alexbosworth / amazon-sdb.js
Created October 13, 2010 10:14
An AWS SimpleDb library
/*
* Alex Bosworth
*
* A straightforward S3 library
*
* USE: var s3 = new S3(AWS_KEY, AWS_SECRET, {defaultBucket : MY_BUCKET});
* s3.put(KEY, {data:{},headers:{}}, [bucket]);
* s3.get(KEY, [bucket]).on('success', function(data) { console.log(data); });
* (more operations: buckets, info, list)
*
@guybrush
guybrush / gist:721762
Created November 30, 2010 14:38
nodejs file-sha1
#!/usr/bin/env node
require('fs').readFile('image.png',function(err, data){
console.log(require('crypto').createHash('sha1').update(data).digest('hex'))
})
@mattheworiordan
mattheworiordan / rate_limit.js
Created July 15, 2011 14:49
Rate limiting function calls with JavaScript and Underscore.js
/* Extend the Underscore object with the following methods */
// Rate limit ensures a function is never called more than every [rate]ms
// Unlike underscore's _.throttle function, function calls are queued so that
// requests are never lost and simply deferred until some other time
//
// Parameters
// * func - function to rate limit
// * rate - minimum time to wait between function calls
// * async - if async is true, we won't wait (rate) for the function to complete before queueing the next request
@alexbosworth
alexbosworth / deferred.js
Created August 30, 2011 02:09
Deferred for Node.js
// Deferreds are useful for chaining: doSomething().success(doSomethingElse).failure(stopStuff);
function Deferred() {
this._successCbk = function() {},
this._failureCbk = function() {};
return this;
}
Deferred.prototype.success = function(cbk) {
this._successCbk = cbk;
@fdstevex
fdstevex / gist:6741638
Created September 28, 2013 12:34
Method to retrieve a list of downloadable fonts on iOS 7. This method may block for a while (it can involve a network call) so don't call on main thread. The return is a dictionary that maps font families to arrays of font names in that family.
+ (NSDictionary *)downloadableFonts
{
CTFontDescriptorRef desc = CTFontDescriptorCreateWithAttributes((CFDictionaryRef)@{(id)kCTFontDownloadableAttribute : (id)kCFBooleanTrue});
CFArrayRef matchedDescs = CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL);
NSArray *array = (__bridge NSArray *)matchedDescs;
NSMutableDictionary *families = [NSMutableDictionary dictionary];
for (UIFontDescriptor *fontDescriptor in array) {
NSString *familyName = fontDescriptor.fontAttributes[UIFontDescriptorFamilyAttribute];
@jj1bdx
jj1bdx / sleep5.sh
Created June 2, 2014 04:02
A shell one-liner for an infinite loop (sh/zsh/bash compatible)
while true; do date; sleep 5; done
@mrecachinas
mrecachinas / toposort.swift
Last active October 9, 2021 02:01
First topological sort written in Apple's new language Swift.
// First Topological Sort in Apple's new language Swift
// Updated on 10/30/2016 to account for the newest version of Swift (3.0)
// Michael Recachinas
enum TopologicalSortError : Error {
case CycleError(String)
}
/// Simple helper method to check if a graph is empty
/// - parameters:
/// - dependency_list: a `Dictionary<String, [String]>` containing the graph structure