Skip to content

Instantly share code, notes, and snippets.

View duncanbeevers's full-sized avatar
🔊


Duncan Beevers duncanbeevers

🔊

View GitHub Profile
@duncanbeevers
duncanbeevers / ArrayShuffle.js
Created July 20, 2010 00:10
Fisher-Yates shuffle
Array.prototype.shuffle = function() {
var i = this.length, k, e;
while (--i) {
k = Math.floor(Math.random() * i);
if (k != i) {
e = this[i];
this[i] = this[k];
this[k] = e;
}
}
<!doctype html>
<html>
<head>
<title>xhr status code test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js"></script>
<script type="text/javascript">
//<![CDATA[
document.observe('dom:loaded', function() {
var body = $$('body')[0],
template = new Template("<span class=\"script_example\"><pre class=\"body\">#{body}</pre><button class=\"trigger_script\">Execute</button></span>"),
class Memcached::CompressedRailsCache
def initialize(rails_cache)
@rails_cache = rails_cache
end
def get(key, raw = true) # raw is discarded
v = @rails_cache.get(key)
Marshal.load(Zlib::Inflate.inflate(v)) if v
end
class Memcached::MultiPartRailsCache
MAX_KEY_LENGTH = 255 # Actually overshooting what the client will let you send through
# but this means our chunk sizes will just a few bytes of headroom
# as we split
PART_SIZE = 1.megabyte - MAX_KEY_LENGTH
def initialize(rails_cache)
@rails_cache = rails_cache
end
@duncanbeevers
duncanbeevers / number_with_delimiter.js
Created March 4, 2010 17:34
Format numbers as comma-delimited strings
//= require <prototype>
// (2001000).withDelimiters(); // "2,001,000"
// (9821).withDelimiters("_"); // "9_821"
// (1221).withDelimiters(",", 100); // "12,21"
Number.prototype.withDelimiters = function(s, k) {
if (!s) s = ",";
if (!k) k = 1000;
var o = [],
i = Math.floor(this);
@duncanbeevers
duncanbeevers / db_uptodate.rake
Created February 22, 2010 23:49
Check whether migrations need to be run without requiring the whole app
namespace :db do
task :uptodate do
if pending_migrations.blank?
puts "Migrations are up-to-date"
else
puts "The following migrations are pending:"
puts pending_migrations.map { |mp| "%s\t\t%s" % [ mp.version, mp.name ] }.join("\n")
end
end
@duncanbeevers
duncanbeevers / mount_secure_volume.bash
Created February 19, 2010 00:07
Mount a secure DMG, execute the deploy_credentials.sh script within it to set up deploy environment, unmount dmg
# Read Secure Volume credentials for deploy
export VAULT_DMG="~/Sensitive.dmg"
alias mount_sp="hdid $VAULT_DMG > /dev/null"
alias sp="SECURE_MOUNT_INFO=\`hdid $VAULT_DMG\`; SECURE_MOUNT_DEVICE=\`echo -e \$SECURE_MOUNT_INFO | cut -d ' ' -f1\`; SECURE_MOUNT_PATH=\`echo -e \$SECURE_MOUNT_INFO | cut -d ' ' -f2\`; . \$SECURE_MOUNT_PATH/deploy_credentials.sh; hdiutil eject \$SECURE_MOUNT_DEVICE > /dev/null; unset SECURE_MOUNT_INFO SECURE_MOUNT_PATH SECURE_MOUNT_DEVICE"
class Hash
def recursively_stringify_keys
recursively_xify_keys(:to_s, :recursively_stringify_keys)
end
def recursively_symbolize_keys
recursively_xify_keys(:to_sym, :recursively_symbolize_keys)
end
private
Compare = function(fn1, fn2, i) {
var time = function(fn) {
var now = new Date();
fn();
return new Date() - now;
},
spinOn = function(fn, i) {
return function() { for (var ii = i;ii;ii--) fn(); }
},
fn1_time = time(spinOn(fn1, i)),
@duncanbeevers
duncanbeevers / hex2rgb.js
Created January 15, 2010 19:10
Convert a hex color string to an object representing the red, green, and blue parts
function hex2rgb(hex) {
var c, o = [], k = 0, m = hex.match(
/^#(([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})|([0-9a-f])([0-9a-f])([0-9a-f]))$/i);
if (!m) return {r:0,g:0,b:0};
for (var i = 2, s = m.length; i < s; i++) {
if (undefined === m[i]) continue;
c = parseInt(m[i], 16);
o[k++] = c + (i > 4 ? c * 16 : 0);
}