Skip to content

Instantly share code, notes, and snippets.

(function(array_plus) {
function x_flatten(array, sum) {
sum = sum || [];
var y;
for (var x = array.length-1; x >= 0; x--) {
y = array[x];
if ((typeof y) == "object" && y.length) {
x_flatten(y, sum);
} else {
sum.unshift(y);
# see: http://www.codenothing.com/archives/2010/8-jquery-micro-optimization-tips/
# usage: cat application.js | ruby jQuery.live2delegate.rb
puts $_.to_s.gsub(/\((.+?)\)\.live\(/, '.root.delegate(\1,') while $stdin.readline rescue exit(0)
// modified from http://javascript.crockford.com/remedial.html
String.prototype.safe_supplant = function (o) {
return this.replace(/{([^{}]*)}/g, function (a, b) {
var r = o[b];
if (typeof r === 'string') {
return r.replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;');
} else {
return (typeof r === 'number') ? r : a;
}
});
@choonkeat
choonkeat / supplant_template.rb
Created May 5, 2010 08:26
Sometimes you just need objects that spew placeholder strings
# Sometimes you just need objects that spew placeholder strings
#
# Regular supplant style http://javascript.crockford.com/remedial.html
# >> x = SupplantTemplate.new
# => ""
# >> x.client.company_name
# => "{client_company_name}"
# >> x.client
# => "{client}"
//
// UIWindow+MyWindow.h
//
#import <Foundation/Foundation.h>
@interface UIWindow (MyWindow)
- (void)addEventMonitor:(void *)monitor;
- (void)removeEventActivityMonitor:(void *)monitor;
- (void)pendingMouseUpCount;
@choonkeat
choonkeat / zoom.js
Created June 18, 2010 00:15 — forked from mislav/zoom.js
/* tested on the iPad and iPhone */
function getZoomFactor() {
var deviceWidth, landscape = Math.abs(window.orientation) == 90
// workaround for strange screen.height on the iPhone (v3.1.3)
if (window.screen.width == 320) deviceWidth = landscape ? 480 : 320
else deviceWidth = window.screen[landscape ? "height" : "width"]
return deviceWidth / window.innerWidth
}
Index: trunk/FBConnect/FBConnectHooks.php
===================================================================
--- trunk/FBConnect/FBConnectHooks.php (revision 206)
+++ trunk/FBConnect/FBConnectHooks.php (working copy)
@@ -%ld,%ld +%ld,%ld @@
// Don't include jQuery if it's already in use on the site
#$out->includeJQuery();
// Temporary workaround until until MW is bundled with jQuery 1.4.2:
- $out->addScriptFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
+ $out->addInlineScript('!window.jQuery && document.write(unescape("%3Cscript src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\"%3E%3C/script%3E"))');
@choonkeat
choonkeat / get_params.js
Created July 21, 2010 04:30
get_params.js
(function(url_parts, params) {
var query_string = (url_parts && url_parts[1]);
if (! query_string) return;
var pairs = query_string.split('&');
for (var x = 0; x < pairs.length; x++) {
var kv = pairs[x].split('=');
params[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1]);
}
// do useful with params
})(window.location.href.split('?'), {});
@choonkeat
choonkeat / gruber.rb
Created July 28, 2010 04:33
gruber.rb
module Gruber
# http://daringfireball.net/2010/07/improved_regex_for_matching_urls
URL_REGEXP = %r{(?xi)
\b
( # Capture 1: entire matched URL
(?:
[a-z][\w-]+: # URL protocol and colon
(?:
/{1,3} # 1-3 slashes
| # or
@choonkeat
choonkeat / track_events.js
Created August 19, 2010 05:37
Paste at the bottom of a webpage
// See: http://gist.github.com/537133
// standard script to track clicks on document elements, and send google analytic events
// identifying which element was clicked (classname, id, href, src, csspath)
// if "cssprefix_optional" is null, identify element as either using classname, id, href, src, csspath (order of preference)
// if "pagename_optional" is null, identify "event category" as page filename
(function(jQuery, pageTracker, cssselector, cssprefix_optional, pagename_optional) {
if (! jQuery) return;
try {
var baseurl = window.location.href.split(/[?#;]/)[0];
var pathsegments = baseurl.split('/');