Skip to content

Instantly share code, notes, and snippets.

View furf's full-sized avatar
🎯
Focusing

Dave Furfero furf

🎯
Focusing
View GitHub Profile
var ensureArray = function(obj) {
return (obj instanceof Array) ? obj : (typeof obj !== 'undefined') ? [obj] : [];
};
// jQuery plugin Textmate snippet
(function (\$) {
/**
* ${10:plugin}
*/
\$.fn.${10:plugin} = function(cfg) {
if (this.length === 0) {
function stress (fn /*, n */) {
var n = arguments[1] || 1000,
i, t,
s = new Date();
for (i = 0; i < n; i++) {
fn();
};
t = ((new Date()) - s);
return {
handler: fn,
// Empty strings are truthy-falsy...
var EMPTY = "";
EMPTY == true; // false
EMPTY == false; // true
// ...but non-empty strings are NOT.
var NON_EMPTY = "hello, world";
NON_EMPTY == true; // false
jQuery.useJsonpCallback = (jQuery.support.jsonpCallback = parseFloat(jQuery.fn.jquery) >= 1.4) ? jQuery.noop : function () {
var cfg = this, jsonp = cfg.dataType === 'script' && cfg.jsonpCallback;
if (jsonp && !window[jsonp]) {
window[jsonp] = function (data) {
cfg.success.call(cfg, data, 'success');
try {
delete window[jsonp];
} catch (ie6) {
// cannot delete window properties in IE6
window[jsonp] = undefined;
// punch
jQuery.punch = function (obj, method, fn) {
var hasOwnMethod = obj.hasOwnProperty(method),
original = obj[method];
obj[method] = function () {
Array.prototype.unshift.call(arguments, $.proxy(original, this));
return fn.apply(this, arguments);
};
// everyone's new favorite closure pattern:
(function(window,document,undefined){ ... })(this,this.document);
// minified:
(function(b,a,c){ ... })(this,this.document);
// which means all uses of window/document/undefined inside the closure
// will be single-lettered, so big gains in minification.
// it also will speed up scope chain traversal a tiny tiny little bit.
$.fn.expando = (function (re) {
return function () {
var prop, elem = this.get(0);
for (prop in elem) {
if (re.test(prop)) {
return elem[prop];
}
}
};
})(/^jQuery\d{13}$/);
(function($) {
/**
* Add live and die to the jQuery root
*/
$.extend({
live: function( selector, type, data, fn, thisObject ) {
if ( jQuery.isFunction( data ) ) {
$.fn.hoverClass = function(className) {
return this.each(function() {
var $this = $(this);
$this.hover(function() {
$this.addClass(className);
}, function() {
$this.removeClass(className);
});
});
};