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
@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
/**
@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 / .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)
}
@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 / 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 {
/**
* From AS2:
* class A {
* var foo = 5;
* }
*
* class A extends B {
* var foo = 6;
* }
*/
@davidaurelio
davidaurelio / lazy-property.js
Created December 3, 2012 08:54
Lazy properties for ES5
function lazyProperty(object, name, create) {
Object.defineProperty(object, name, {
configurable: true, // or false if defined on prototype objects
enumerable: false,
get: function() {
return this[name] = create(this);
},
set: function(value) {
Object.defineProperty(this, name, {
configurable: true,
@davidaurelio
davidaurelio / super-proxy.js
Created September 7, 2012 10:13
`super` using `Proxy.create`
Object.defineProperty(Object.prototype, 'super', {
value: function(Constructor) {
var context = this;
var superProto = Object.getPrototypeOf(Constructor.prototype);
return Proxy.create({
get: function(_, name) {
return function() {
return superProto[name].apply(context, arguments);
}
}
@davidaurelio
davidaurelio / naive-super.js
Created September 7, 2012 09:54
Naive implementation of super in JS
Object.defineProperty(Object.prototype, 'super', {
get: function() { return Object.getPrototypeOf(this); }
});
function Foo() {}
Foo.prototype.reset = function() {
this.fooProp = 'reset';
console.log('Foo#reset()');
}
@davidaurelio
davidaurelio / point.js
Created July 18, 2012 10:11
A Point class that supports addition and subtraction of instances. Precision is 16bit fixed point per coordinate.
/*
A point constructor that creates objects supporting addition and subtraction.
The "trick" is to use 32bit integers and stuff two fixed 16bit fixed point
numbers into them that represent the coordinates of the point.
This approach has problems with overflow. E.g. when adding two points (0, 2)
and (0, -2), the addition will cause the y values to overflow into the 17th
bit, which is part of the x-value. When subtracting two points, e.g.
(.00390625, 0) - (0, -128)