Skip to content

Instantly share code, notes, and snippets.

View appden's full-sized avatar

Scott Kyle appden

View GitHub Profile
// Array shuffle for MooTools
Array.implement({
// don't remember where I saw this one-liner first...
shuffle: function(){
for (var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
return this;
}
// Function overloading based on the type of the first argument
Function.prototype.overload = function(types, pick){
var self = this;
return function(){
var type = (pick || typeOf).apply(this, arguments);
return (type && types[type] || self).apply(this, arguments);
};
};
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<!-- Online here: http://ejohn.org/files/bugs/isObjectLiteral/ -->
<title>isObjectLiteral</title>
<style>
li { background: green; } li.FAIL { background: red; }
iframe { display: none; }
</style>
// add chrome to jQuery browser test
if ((jQuery.browser.chrome = /chrome/i.test(navigator.userAgent))
jQuery.browser.version = navigator.userAgent.match(/chrome\/(\d+)/i)[1];
// alternative I just thought of...
if (/chrome\/(\d+)/i.exec(navigator.userAgent)){
jQuery.browser.chrome = true;
jQuery.browser.version = RegExp.$1;
}
// Static mutator to accept static methods to a Class
Class.Mutators.Static = function(methods){
this.extend(methods);
};
// adds in support for having just msxml.dll, see issue #739 on -core
Browser.Request = function(){
return $try(function(){
return new XMLHttpRequest();
}, function(){
return new ActiveXObject('MSXML2.XMLHTTP');
}, function(){
return new ActiveXObject('Microsoft.XMLHTTP');
});
// set the class of a jQuery collection
jQuery.fn.setClass = function(className){
return this.each(function(){
this.className = className || '';
});
};
Request.Twitter = new Class({
Extends: Request.JSONP,
options: {
linkify: true,
url: 'http://twitter.com/statuses/user_timeline/{term}.json',
data: {
count: 5
}
Function.stab(function(){
Array.from(document.html.childNodes);
}, function(){
var old = Array.from;
Array.from = function(item, slice){
if (typeOf(item) == 'collection'){
var l = item.length, array = new Array(l - slice), i = slice;
while (i++ < l) array[i - slice] = item[i];
return array;
}
// Puncture a closure, absolute madness
Function.prototype.puncture = function(){
var wormhole = function(__cmd__){
return eval(__cmd__);
};
var source = ('' + this).match(/function.+?\{([\s\S]*)\}/)[1];
var fn = new Function('this.wormhole=' + wormhole + '\n' + source);