Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
@cowboy
cowboy / HEY-YOU.md
Last active April 9, 2024 15:54
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@bentruyman
bentruyman / jquery-pubsub.js
Created February 14, 2011 23:16
Simple Pub/Sub Implementation for jQuery
/*
* Simple Pub/Sub Implementation for jQuery
*
* Inspired by work from Peter Higgins (https://github.com/phiggins42/bloody-jquery-plugins/blob/master/pubsub.js)
*
* This is about the simplest way to write a pubsub JavaScript implementation for use with jQuery.
*/
(function( $ ) {
// Cache of all topics
@jpantuso
jpantuso / osx_lion_rail_setup.md
Created July 27, 2011 19:51
Setup OS X 10.7 w/ homebrew, oh-my-zsh, rvm, rails, and MySQL
@rauschma
rauschma / module_boilerplate.js
Created January 23, 2012 15:36
Triple module boilerplate: Node.js, AMD, plain browser
// No imports, those are more work, especially for plain browser.
// However, as soon as you have imports, you should switch to AMD on browsers.
// Related: http://www.2ality.com/2011/11/module-gap.html
({ define:
typeof define === "function" ?
define
: typeof module !== "undefined" ?
function(F) { module.exports = F() }
: function(F) { this.defClass = F() }.bind(this)
@Gozala
Gozala / example.js
Created January 29, 2012 03:46
Workaround for lack of "tail call optimization" in JS
// Lack of tail call optimization in JS
var sum = function(x, y) {
return y > 0 ? sum(x + 1, y - 1) :
y < 0 ? sum(x - 1, y + 1) :
x
}
sum(20, 100000) // => RangeError: Maximum call stack size exceeded
// Using workaround
@dherman
dherman / 1-recursive.js
Created February 7, 2012 20:10
turning recursion into iteration in JS
// Version 1. Simple recursive function. Blows the stack for large nodes.
function replace(node, from, to) {
switch (node.type) {
case IF:
return {
type: IF,
test: replace(node.test, from, to),
then: replace(node.then, from, to),
else: replace(node.else, from, to)
};
@pamelafox
pamelafox / senderror.js
Created February 21, 2012 19:20
Sending JS errors to server
function sendError(message, url, lineNum) {
var i;
// First check the URL and line number of the error
url = url || window.location.href;
lineNum = lineNum || 'None';
// If the error is from these 3rd party script URLs, we ignore
// We could also just ignore errors from all scripts that aren't our own
var scriptURLs = [
@dherman
dherman / closure.js
Created July 27, 2012 21:39
Analog to Function constructor for building closures
(function() {
var hasOwnProperty = Object.prototype.hasOwnProperty;
var Function = hasOwnProperty.constructor;
function isIdentifier(s) {
return /[a-zA-Z_$][a-zA-Z_$0-9]*/.test(s);
}
@dherman
dherman / functions.js
Created July 29, 2012 01:44
Function utilities
(function(Fp, Ap) {
var applyMethod = Fp.apply,
bindMethod = Fp.bind,
callMethod = Fp.call;
var sliceMethod = Ap.slice,
concatMethod = Ap.concat;
var apply = callMethod.bind(applyMethod),
bind = callMethod.bind(bindMethod),