Skip to content

Instantly share code, notes, and snippets.

View dshaw's full-sized avatar
🦄
Always bet on Node.js ✨

Dan Shaw dshaw

🦄
Always bet on Node.js ✨
View GitHub Profile
@dshaw
dshaw / gist:468335
Created July 8, 2010 17:30
Quick and dirty psuedo-ish code for a Node.js Web Socket connection throttler
var maxThrottles = 10;
connectionManager[connection.id].lastMessage = +new Date();
connectionManager[connection.id].timeOutUntil = connectionManager[connection.id].lastMessage + 1000; // one second, too long?
connectionManager[connection.id].throttledCount = 0;
connection.addListener("message", function( message ) {
var messageTime = +new Date();
if (messageTime < connectionManager[connection.id].timeOutUntil) {
(throttleCount < maxThrottles) ? connectionManager[connection.id].throttledCount++ : kickMe(connection.id);
}
tests['backgroundsize'] = function() {
var bool = test_props_all( 'backgroundSize' );
if (bool){
bool = new Boolean(bool);
bool.contain = !!(function() {
set_css('background-size: contain');
return contains(m_style['backgroundSize'], 'contain');
});
bool.cover = !!(function() {
@dshaw
dshaw / factorial.js
Created September 5, 2010 04:06
recursive factorial function
function factorial( n ) {
if (n > 1) {
return factorial(n-1) * n;
} else {
return 1;
}
}
console.log( factorial( 1 ) 1 );
console.log( factorial( 3 ), 6 );
@dshaw
dshaw / screening.js
Created September 14, 2010 15:07 — forked from rmurphey/screening.js
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
// From jQuery source
// Calculate pageX/Y if missing and clientX/Y available
if ( event.pageX == null && event.clientX != null ) {
var doc = document.documentElement, body = document.body;
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
}
@dshaw
dshaw / cl-snips
Created September 22, 2010 00:53
Command Line Snippets
# set temporary environment variable
export PORT=8888
# Expresso test with code coverage
expresso -I lib-cov test/*
function doHash(str, seed) {
var m = 0x5bd1e995;
var r = 24;
var h = seed ^ str.length;
var length = str.length;
var currentIndex = 0;
while (length >= 4) {
var k = UInt32(str, currentIndex);
@dshaw
dshaw / dreadnode-gamecast.js
Created October 6, 2010 19:41
dreadnode game.gamecast()
// Optimize gamecast with ( http://github.com/LearnBoost/Socket.IO-node/blob/master/lib/socket.io/listener.js#L83-90 ):
Listener.prototype.broadcast = function(message, except){
for (var i = 0, k = Object.keys(this.clients), l = k.length; i < l; i++){
if (this.clients[k[i]] && (!except || [].concat(except).indexOf(this.clients[k[i]].sessionId) == -1)){
this.clients[k[i]].send(message);
}
}
return this;
};
@dshaw
dshaw / min_jquery_version.js
Created October 29, 2010 03:42
Determine if a page has the minimum jQuery version you need to do what you're doing.
function minVersion(version) {
var $vrs = window.jQuery.fn.jquery.split('.'),
min = version.split('.');
for (var i=0, len=$vrs.length; i<len; i++) {
console.log($vrs[i], min[i]);
if (min[i] && $vrs[i] < min[i]) {
return false;
}
}
return true;
@dshaw
dshaw / tweet-media_dailybooth.html
Created November 4, 2010 20:06
#newtwitter tweet-media embeds.
<div class="component">
<div class="tweet-media">
<div class="dailybooth">
<a class="inline-media-image" data-inline-type="DailyBooth" href="http://dailybooth.com/pamelafox/9884843" target="_blank"><img src="http://cdn2.dailybooth.com/5/pictures/large/cd75a8852f0d648d2de6a28e9317a2e3_9884843.jpg"></a>
</div>
<div class="media-attribution">
<span>via</span><img width="16px" height="16px" src="http://dailybooth.com/favicon.ico"><a target="_blank" data-media-type="DailyBooth" class="media-attribution-link" href="http://dailybooth.com">DailyBooth</a>
</div>
</div>
<hr class="component-spacer">