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
/**
@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) {
#!/usr/bin/env python
"""
Fixes file names for downloads from jil.org.
jil.org delivers downloaded files with file names in e-mail header format. The
fix_filename function from this module can decode such file names.
When this module is executed as main program, all file names passed as program
arguments are renamed properly:
@davidaurelio
davidaurelio / hastouch.html
Created September 6, 2011 17:10
Checking for touch support
<!doctype html>
<title>has touch?</title>
<h1>Has this device touch? </h1>
<script>
var isTouch = (function(){
try{
var event = document.createEvent("TouchEvent"); // Should throw an error if not supported
return !!event.initTouchEvent; // Check for existance of initialization method
}catch(error){
return false;
@davidaurelio
davidaurelio / Usage
Created April 2, 2012 12:12
Angle type
var a = new Angle().radians(Math.PI * 3 / 4),
b = new Angle().degrees(135);
var c = new Angle(a + b);
Math.sin(a);
Math.cos(b);
a.add(new Angle().degrees(30));
b.add(Math.PI);
@davidaurelio
davidaurelio / point.js
Created July 18, 2012 08:40
A toString()’able point that allows addition
function Point(x, y) {
if (arguments.length === 1 && typeof x === 'string') {
return x.slice(1, -1)
.split('}{')
.map(function(tuple) {
return tuple.split(',', 2).map(parseFloat)
})
.reduce(function(point, coords) {
point.x += coords[0];
point.y += coords[1];
@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)
@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 / 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);
}
}