Skip to content

Instantly share code, notes, and snippets.

View appden's full-sized avatar

Scott Kyle appden

View GitHub Profile
// Static mutator to accept static methods to a Class
Class.Mutators.Static = function(methods){
this.extend(methods);
};
// 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;
}
<!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>
// 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);
};
};
// 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;
}
Element.implement({
// truncates element text to fit its width
truncate: function(trail){
var text = this.get('text').trim();
if (!text) return;
trail = trail || '...';
var style = this.style;
var styles = { overflow: style.overflow, 'white-space': style.whiteSpace, padding: style.padding, margin: style.margin };
String.implement({
truncate: function(max, trail, separator){
var s = this.trim();
if (s.length <= max) return s;
separator = $pick(separator, ' ');
if (separator)
s = s.substr(0, max + 1).substr(0, s.lastIndexOf(separator));
else
s = s.substr(0, max);
// inspired by jQuery watch: http://plugins.jquery.com/files/jquery-watch.js.txt
(function(){
var watch = Object.prototype.watch,
unwatch = Object.prototype.unwatch,
watched = [],
timer;
Object.watch = function(obj, prop, fn){
// FUNCTION BINDING
Function.prototype.bind = function(bind, args){
var self = this.unbind();
if (args != null) args = $splat(args);
var fn = function(){
return self.apply(bind, args || arguments);
};
// Auto-updating Date stuff
Date.pretty = new Class({
Implements: Options,
options: {
interval: 60000,
threshold: '24 hours'
},