Skip to content

Instantly share code, notes, and snippets.

View AutoSponge's full-sized avatar

Paul Grenier AutoSponge

View GitHub Profile
function empty_list(selector) {
return selector(undefined, undefined, true);
};
function prepend(el, list) {
return function(selector) {
return selector(el, list, false);
};
};
var strategy = {};
strategy[1] = function (el, arr) {
return [el].concat(arr);
};
strategy[2] = function (el, arr) {
var copy = arr.slice(0);
return (copy.unshift(el), copy);
};
strategy[3] = function (el, arr) {
arr.unshift(el);
function shift(arr, i) {
var cut = arr.length - i;
return arr.slice(cut).concat(arr.slice(0, cut));
}
var arr = [1,2,3,4,5];
shift(arr, 1); //[5, 1, 2, 3, 4]
shift(arr, 3); //[3, 4, 5, 1, 2]
//not a stack implementation of pubsub
(function (namespace) {
var topics = {};
PubSub = {
subscribe: function (topic, fn, reciever) {
var arr = topics[topic] = topics[topic] || [];
arr.push({
fn: fn,
reciever: reciever
});
function fn(a, b) {
console.log(this, a, b);
this[a] = b;
console.log(obj[a]);
}
var obj = {"test": null};
fn.apply(obj, ["test", 1]);
fn.call(obj, "test", 1);
fn.bind(obj, "test", 1)();
(function createMediator(namespace, topics) {
function applyBind(handler, reciever) {
return Function.apply.bind(handler, reciever);
}
function getSubscriptions(topic) {
return topics[topic] = topics[topic] || [];
}
function addSubscription(topics, subscription) {
return topics[topics.length] = subscription;
}
(function createUtil(global) {
return global.util = {
apply: function (reciever, args) {
return function (fn) {
return fn.apply(reciever, args);
};
}
};
}(this));
//this version handles objects as well
//beware using functions as they can appear the same but hold different scoped values
//beware using native functions as they will appear the same in string form
//beware using objects with functions as the functions will not be serialized by JSON
(function (GLOBAL) {
"use strict";
var reduce = Array.prototype.reduce;
var toString = Object.prototype.toString;
function hash(key, arg, i) {
var d = i ? "↔" : "";
var strategy = {};
strategy["slice"] = function () {
return Array.prototype.slice.call(arguments, 0);
};
strategy["cache slice"] = (function () {
var slice = Array.prototype.slice;
return function () {
return slice.call(arguments, 0);
};
}());