Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View davidaurelio's full-sized avatar
💭
I may be slow to respond.

David Aurelio davidaurelio

💭
I may be slow to respond.
View GitHub Profile
/**
* From AS2:
* class A {
* var foo = 5;
* }
*
* class A extends B {
* var foo = 6;
* }
*/
@davidaurelio
davidaurelio / async.js
Created March 28, 2013 08:35
Naïve parallel/chain implementation for asynchronous functions taking node-style callbacks
var concat = [].concat, slice = [].slice;
function chain(fns, initialArgs, callback) {
(function next(error) { // `next` is the callback for each chained function
if (error) { callback(error); return; }
var fn = fns.shift();
if (fn) {
fn.apply(null, slice.call(arguments, 1).concat(next));
} else {
@davidaurelio
davidaurelio / partial.js
Created April 30, 2013 10:15
Simple higher-order partial function that takes advantage of sparse arrays to make parameters skippable.
function partial(fn, fixedArgs) {
return function() {
var args = fixedArgs.slice(), skipped = 0;
for (var i = 0, n = arguments.length; i < n; i++) {
while (i + skipped in args) { skipped++; }
args[i + skipped] = arguments[i];
}
return fn.apply(this, args);
};
}
@davidaurelio
davidaurelio / .profile
Last active December 20, 2015 11:09
bash helper function to make debugging node cli programs easier.
node-debug () {
PROGRAM="$(which $1)" # make sure we get an absolute path for programs in PATH
shift # remove the original program argument
set -- "$PROGRAM" "$@" # prepend the program path
node --debug-brk $@ & # start node in paused debug mode, send to background
node-inspector # start node inspector
kill %% # kill the background task (if still running)
}
/**
@license
Copyright 2014 David Aurelio <dev@david-aurelio.com>
Full source at https://gist.github.com/davidaurelio/4bdafd219ebf610e8fc4
*/
(function() {
'use strict';
var GIVE_UP = 0;
var NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4;
@davidaurelio
davidaurelio / admin.py
Created December 13, 2014 07:23
Versioned slug model
from django.contrib import admin
from .models import MainThing, MainThingSlug
class SlugInline(admin.StackedInline):
model = MainThingSlug
fields = ('slug',)
can_delete = False
@davidaurelio
davidaurelio / container.js
Created March 4, 2015 15:07
React: custom validator for type of children
const {forEach} = React.Children;
class Child extends React.Component { /*…*/ }
class Container extends React.Component { /*…*/ }
Container.propTypes = {
children: function(props, propName, componentName) {
forEach(props[propName], (element) => {
const {type} = element;
if (type !== Child) {
var u = require('uglify-es');
var code = `
(function() {
var NUMBER = '[-+]?\\d*\\.?\\d+';
var PERCENTAGE = NUMBER + '%';
function call() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
!function() {
function n() {
for (var n = arguments.length, e = Array(n), r = 0; r < n; r++) e[r] = arguments[r];
return "(s*(" + e.join(")s*,s*(") + ")s*)";
}
new RegExp("rgb" + n()), new RegExp("rgba" + n()), new RegExp("hsl" + n()), new RegExp("hsla" + n());
}();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>count bits</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>