Skip to content

Instantly share code, notes, and snippets.


Alvin Liang aliang

  • Burlingame, CA
View GitHub Profile
aliang / country_codes.js
Created Apr 17, 2014
Really stupid iso-3166 country code converter. As stupid as possible!
View country_codes.js
var CountryCodes = (function() {
// Codes taken from Wikipedia as of Wed Apr 16 2014
var codes = {"AFG": "AF", "ALA": "AX", "ALB": "AL", "DZA": "DZ", "ASM": "AS", "AND": "AD", "AGO": "AO", "AIA": "AI", "ATA": "AQ", "ATG": "AG", "ARG": "AR", "ARM": "AM", "ABW": "AW", "AUS": "AU", "AUT": "AT", "AZE": "AZ", "BHS": "BS", "BHR": "BH", "BGD": "BD", "BRB": "BB", "BLR": "BY", "BEL": "BE", "BLZ": "BZ", "BEN": "BJ", "BMU": "BM", "BTN": "BT", "BOL": "BO", "BES": "BQ", "BIH": "BA", "BWA": "BW", "BVT": "BV", "BRA": "BR", "IOT": "IO", "BRN": "BN", "BGR": "BG", "BFA": "BF", "BDI": "BI", "KHM": "KH", "CMR": "CM", "CAN": "CA", "CPV": "CV", "CYM": "KY", "CAF": "CF", "TCD": "TD", "CHL": "CL", "CHN": "CN", "CXR": "CX", "CCK": "CC", "COL": "CO", "COM": "KM", "COG": "CG", "COD": "CD", "COK": "CK", "CRI": "CR", "CIV": "CI", "HRV": "HR", "CUB": "CU", "CUW": "CW", "CYP": "CY", "CZE": "CZ", "DNK": "DK", "DJI": "DJ", "DMA": "DM", "DOM": "DO", "ECU": "EC", "EGY": "EG", "SLV": "SV", "GNQ": "GQ", "ERI": "ER", "EST": "EE", "ETH": "ET",
aliang / io_ext.rb
Created May 7, 2014
Select random line from IO object
View io_ext.rb
# See
class IO
# Selects a random "line" from a file (might not be a line if different separator is passed).
# @param *args Same arguments as IO.foreach
# @return A random line from the IO object
def self.random_line(*args)
chosen_line = nil
self.foreach(*args).each_with_index do |line, number|
chosen_line = line if rand < 1.0/(number+1)
aliang / disable-leaflet-map-events.js
Created Jun 4, 2014
Disable all (or almost all!) leaflet map events
View disable-leaflet-map-events.js
// Assumes your L.Map is called "map". Untested!
if (map.tap) {
aliang / vanity_phone_number_converter.rb
Created Sep 2, 2014
Vanity phone number converter
View vanity_phone_number_converter.rb
# Converts a phone number with letters to a phone number with only numbers.
# Leaves any other characters untouched.
class VanityPhoneNumberConverter
'a' => '2', 'b' => '2', 'c' => '2',
'd' => '3', 'e' => '3', 'f' => '3',
'g' => '4', 'h' => '4', 'i' => '4',
'j' => '5', 'k' => '5', 'l' => '5',
'm' => '6', 'n' => '6', 'o' => '6',
'p' => '7', 'q' => '7', 'r' => '7', 's' => '7',
aliang / gist:263863
Created Dec 26, 2009 — forked from retr0h/gist:98308
content type handling for sinatra
View gist:263863
CONTENT_TYPES = {:html => 'text/html', :css => 'text/css', :js => 'application/javascript'}
before do
# instead of using case here, metaprogram it
request_uri = case request.env['REQUEST_URI']
when /\.css$/ : :css
when /\.js$/ : :js
else :html
content_type CONTENT_TYPES[request_uri], :charset => 'utf-8'
View gist:308372
function delegateHandler(e){
var element = e.element(), elements = element.ancestors ? element.ancestors().concat([element]) : [element];
((Element.retrieve(this, 'prototype_delegates') || $H()).get(e.eventName || e.type) || []).each(function(pair){
if (element = Selector.matchElements(elements, pair.key)[0])
pair.value.invoke('call', element, e);
function delegate(element, selector, event, handler){
View heroku_deploy.rake
# List of environments and their heroku git remotes
:staging => 'myapp-staging',
:production => 'myapp-production'
namespace :deploy do
ENVIRONMENTS.keys.each do |env|
desc "Deploy to #{env}"
task env do
aliang / prototype-within-viewport.js
Created Aug 1, 2010
methods for seeing if something is in the viewport. see also
View prototype-within-viewport.js
Element.Methods.centerIsWithinViewport = function(element) {
var dim = document.viewport.getDimensions();
var so = document.viewport.getScrollOffsets();
var co = element.cumulativeOffset();
var edim = element.getDimensions();
// x and y measured from upper left
var center = {
x: co.left + (edim.width / 2),
y: + (edim.height / 2)
View partials.rb
# stolen from
# and made a lot more robust by me
# this implementation uses erb by default. if you want to use any other template mechanism
# then replace `erb` on line 13 and line 17 with `haml` or whatever
module Sinatra::Partials
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
View share random song from your itunes library in dropbox
require 'fileutils'
dirs = Dir['path_to_your_root_music_directory/**/*.*']
file = dirs[rand(dirs.size)]
end until File.file?(file)
# could check to make sure your dropbox folder is mounted/exists here
# remove all files from this dropbox folder
You can’t perform that action at this time.