Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
@AutoSponge
AutoSponge / observable.js
Last active October 13, 2015 23:58
Observable AOP
function observable(fn, topic) {
function observed() {
var result;
PubSub.publish(topic + "/before", {obj: this, args: arguments});
result = fn.apply(this, arguments);
PubSub.publish(topic + "/after", {obj: this, args: arguments, result: result});
return result;
};
observed.before = function (fn) {
PubSub.subscribe(topic + "/before", fn);
//http://rmurphey.com/blog/2010/09/15/in-search-of-javascript-developers-a-gist/
// 1: how could you rewrite the following to make it shorter?
if (foo) {
bar.doSomething(el);
} else {
bar.doSomethingElse(el);
}
/**
* @method curry
* @memberOf Function.prototype
* @param arg {Any}
* @param context {Object} used as /this/ when invoking the original function
* @returns {Function}
* @example var add1 = (function add(a, b) { return a + b; }).curry(1);
* add1(2);//3
* @see http://en.wikipedia.org/wiki/Currying
*/
(function (global) {
var Class = {};
var app = {};
function fluent(obj, fn) {
return function () {
fn.apply(obj, arguments);
return obj;
}
}
//http://jsperf.com/partial-speed-test
var strategy = {};
strategy[1] = function() {
var args = Array.prototype.slice.call(arguments);
var f = this;
return function() {
var inner_args = Array.prototype.slice.call(arguments);
return f.apply(this, args.concat(inner_args))
};
};
(function (global, undef) {
/**
* memoizes functions with a single, primitive argument
* @param f {Function}
* @returns {Function}
*/
function memoize(f) {
var cache = (f.memoize = f.memoize || {});
return function (arg) {
return (arg in cache) ? cache[arg] : cache[arg] = f.call(this, arg);
(function (global) {
var cache = {};
function c(n) {
if (n < 1) {
return new Function;
}
var i;
var params = "";
var vars = " return function () {" + "\n";
@AutoSponge
AutoSponge / QueueList.js
Last active December 13, 2015 22:59
Take the idea of a LinkedList and apply it to a queue to run a series of operations on a given input.
(function(global, undef) {
function QueueList(fn, next) {
if (!(this instanceof QueueList)) {
return new QueueList(fn, next);
}
this.fn = fn;
this.next = next || undef;
}
QueueList.prototype.after = function(fn) {
return new QueueList(fn, this);
(function (global, pubsub, cache) {
function push(prop) {
return function (val) {
this[prop].push(val);
return this;
};
}
function fire(data) {
var proceed = true;
this.start.every(function (fn) {
(function (global, known) {
global.dev = global.dev || {};
function register(str) {
known[str] = str;
}
function raiseError(leaks) {
if (leaks && leaks.length) {
throw new Error("global leak" +