Skip to content

Instantly share code, notes, and snippets.

@bttmly
bttmly / keys-bench.js
Last active August 29, 2015 14:07
own keys benchmarking
var Benchmark = require( 'benchmark' );
var len = process.argv[2] || 3;
var obj = {};
for ( var i = 0; i < len; i++ ) {
obj[i] = "value" + i;
}
@bttmly
bttmly / poser-push.js
Created October 11, 2014 17:25
poser push benchmark
function grabIt (thing) {
var frame = document.createElement('iframe');
document.body.appendChild(frame);
var thing = frame.contentWindow[thing];
document.body.removeChild(frame);
return thing;
}
var Collection = grabIt("Array");
/**
* Enable route to __noSuchMethod__ when unknown method calling.
*
* @param {Object} obj Target object.
* @return {Object}
*/
function enableMethodMissing(obj) {
var functionHandler = createBaseHandler({});
functionHandler.get = function(receiver, name) {
@bttmly
bttmly / prototypal.js
Last active August 29, 2015 14:12
instanceof w prototypal instantiation
var helloProto = {
toString: function () {
return "Hello!";
}
}
function helloFactory () {
return Object.create(helloProto);
}
var Bluebird = require("bluebird");
var nba = require("nba");
Object.keys(nba.api).forEach(function (method) {
var original = nba.api[method];
nba.api[method] = Promise.promisify(original);
});
[..., last] = arr
@bttmly
bttmly / curry-es6.js
Last active August 29, 2015 14:27
Curry is a one-liner in idiomatic es6
let curry = (fn, ...args) => args.length >= fn.length ? fn(...args) : (...more) => curry(fn, ...args, ...more)
// just to make the gist longer.
let promisify = fn => (...args) =>
new Promise((resolve, reject) => {
fn(...args, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
@bttmly
bttmly / promisify-es6.js
Created September 17, 2015 00:58
promisify is a one-liner in idiomatic es6
let promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => err ? reject(err) : resolve(result)));
//
@bttmly
bttmly / parse-docs.js
Created February 26, 2016 04:36
parse the nba_py docs into an array of endpoint descriptions that can be used to dynamically construct an API client
// targets this file: https://github.com/seemethere/nba_py/wiki/stats.nba.com-Endpoint-Documentation
var fs = require("fs");
var markdown = fs.readFileSync("./stats.nba.com-Endpoint-Documentation.md", "utf8");
function createDescription (block) {
var name = block[0].slice(3, -1);
var params = block.slice(2).map(line => line.slice(4)).filter(Boolean);
return {name, params};
}