Skip to content

Instantly share code, notes, and snippets.

View unscriptable's full-sized avatar
🏔️

John Hann unscriptable

🏔️
View GitHub Profile
module.exports = curry;
function curry (f) {
var arity = f.length;
var params = [];
var end = createEnd(f, arity);
return createCurried(params, arity, end);
}
function createEnd (f, arity) {
@unscriptable
unscriptable / curry.js
Last active August 29, 2015 14:04
One way to create a curry function. Currying: http://en.wikipedia.org/wiki/Currying
module.exports = curry;
function curry (f) {
var arity = f.length;
var params = [];
var end = createEnd(f, arity);
return createCurried(params, arity, end);
}
function createEnd (f, arity) {
@briancavalier
briancavalier / ambiguous-race.js
Last active August 29, 2015 14:07
Promise.race answers differently with same inputs, depending on when it is called.
// Use native or try any of these promise impls.
// They all behave the same.
//var Promise = require('when').Promise;
//var Promise = require('rsvp').Promise;
//var Promise = require('bluebird');
// Here are 2 promises, p1 and p2. p2 always *actually* finishes
// first, since p1 finishes in 1 millisecond, and p2 is already
// finished when created
@millermedeiros
millermedeiros / image.js
Created February 10, 2011 22:19
RequireJS Image Plugin
/*
* Example #1 : usage example
* - Image paths are relative to the HTML file.
* - Support absolute paths as well.
* - Appending `!bust` to the end of the file name will avoid caching the image.
*/
require(['image!lol_cat.gif', 'image!http://tinyurl.com/4ge98jz', 'image!dynamic_image.jpg!bust'], function(cat, awesome, dynamic){
//add loaded images to the document!
document.body.appendChild(awesome);
document.body.appendChild(cat);
@briancavalier
briancavalier / then-promise.js
Created March 30, 2011 13:19
Promise chaining implementation of then()
Promise.prototype.then = function (onResolve, onReject, onProgress) {
if(isPromise(onResolve)) {
// Chain promise
this.then(
function(val) { onResolve.resolve(onReject ? onReject : val); },
function(err) { onResolve.reject(err); },
function(update) { onResolve.progress(update); }
);
} else {
// capture calls to then()
@bgerrissen
bgerrissen / js-loader.js
Created May 24, 2011 09:05
Robust cross browser JS Loader for dependency management.
/**
* Author: Ben Gerrissen
* Date: 9-3-11
* License: MIT
*/
(function( global ){
var undefined
, preloads = false
(function(){
function Klass(id){ this._id = id; }
Klass.prototype.id = function(){ console.log(this._id); }
var freeExports = typeof exports == 'object' && exports;
if (freeExports) {
for (var prop in Klass) freeExports[prop] = Klass[prop];
} else if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
define(function() { return Klass; });
@unscriptable
unscriptable / ZZZ_MyView.js
Created November 23, 2011 11:05
cujo-ish view/widget
// MyView.js when concatenated together using cram.js
// cram.js needs two new features to make this work:
// 1. an option to NOT normalize module ids
// 2. an option to more easily exclude modules from the build (wire plugins, for instance)
define('./myView/controller', {
_onClick: function (e) {
this.rootNode.classList.toggle(this.states.selected);
}
});
@briancavalier
briancavalier / soon.js
Created January 6, 2012 19:31
Enqueue a task for nextTick in many environments
soon = (function() {
var enqueueTask;
if(typeof process != 'undefined' && typeof process.nextTick == 'function') {
// Node nextTick
enqueueTask = process.nextTick;
} else if (typeof setImmediate == 'function') {
// setImmediate for new IE
@unscriptable
unscriptable / _spec.js
Created April 5, 2012 02:43
wire plugin to create functions without violating JSON
define({
myFunc: {
expr: {
body: 'alert(e.target.textContent);',
params: ['e']
}
},
body: {