Skip to content

Instantly share code, notes, and snippets.

View qubyte's full-sized avatar

Aura Everitt qubyte

View GitHub Profile
@qubyte
qubyte / unshortener.go
Last active December 27, 2015 19:59
Super simple URL unshortener in Go
// Package unshortener takes a single parameter (url) and sends back an unshortened version.
//
// This version is in vanilla go, and requires no packages to be installed.
package main
import (
"encoding/json"
"io"
"log"
"net/http"
@qubyte
qubyte / async.switch.js
Last active December 29, 2015 17:59
async.switch
var async = require('async');
// ASYNC SWITCH patches the async module with a switch like construct for asynchronous functions.
// Use like:
// async.switch(sitchedVar, {
// 'case 1': function (callback) {/*...*/},
// 'case 2': function (callback) {/*...*/},
// 'default': function (callback) {/*...*/}
// }, callback);
@qubyte
qubyte / newReturn.md
Last active December 31, 2015 03:18
The behaviour of `return` when used with `new`.

Here is a regular constructor function:

var Hello = function (name) {
    this.name = name;
};

When used with new, you get what you would expect:

@qubyte
qubyte / writeHooksAsync.js
Last active December 31, 2015 13:59
A script to install git hooks. Run these as the `postinstall` script in package.json. The example here writes a pre-commit hook that runs `npm test`. By having this as a self-contained script and not as a Grunt or Gulp task, Gulp or Grunt can be strict devDependencies.
/* This script operates asynchronously. It can be written synchronously, but
* not a lot is saved once you try-catch everything. Uses the async module.
*
* MIT licence.
*/
var fs = require('fs');
var path = require('path');
var async = require('async');
// Write as many of these as you like.
@qubyte
qubyte / ES5.js
Last active January 2, 2016 03:29
Demonstrates the avoidance of a closure for the trivial task of getting a time difference from invocation using a generator.
var dt = (function () {
var start = Date.now();
return function () {
return Date.now() - start;
};
})();
// Some time later...
@qubyte
qubyte / unshortener.dart
Last active January 6, 2025 20:04
Super simple URL unshortener in Dart
import 'dart:io' show HttpRequest, HttpClient, HttpServer, ContentType;
import 'dart:convert' show JSON;
void handleRequest(HttpRequest request) {
// For convenience.
var response = request.response;
// No path is expected. Return with 404 for anything else.
if (request.uri.path != '' && request.uri.path != '/') {
response.statusCode = 404;
var config = require('minimist')(process.argv.slice(2));
module.exports = config;
var errors = [];
if (!config.hasOwnProperty('twitter-consumer-key')) {
errors.push('No twitter-consumer-key given.');
}
@qubyte
qubyte / keybase.md
Created June 13, 2014 00:26
Keybase GitHub verification gist.

Keybase proof

I hereby claim:

  • I am qubyte on github.
  • I am qubyte (https://keybase.io/qubyte) on keybase.
  • I have a public key whose fingerprint is 4388 5140 C3EC 6EDA 5AA5 4A2C 8BEC 9D71 1FB0 ED63

To claim this, I am signing this object:

@qubyte
qubyte / thislessFactory.js
Last active August 29, 2015 14:03
A quick look at what making objects without using this might look like. I'm particularly interested in keeping methods decoupled so that they can be put into their own modules and easily tested. This leads to a pseudo-this object (called context here) to give methods access to shared data.
// Methods will be bound. This allows them to be public or private
// later on without needing `this`. These can be required in etc.
function addToCounterMixin(context, num) {
context.counter += num;
}
// Use this to proxy a field on from one object to another.
function proxy(context, obj, fieldName) {
Object.defineProperty(obj, fieldName, {
set: function (val) {
@qubyte
qubyte / inconsistency.js
Last active June 11, 2016 08:13
Fat arrow inconsistency between Aurora 33.0a2 (2014-08-24) and Canary 39.0.2135.0 canary (64-bit)
var a = true;
var test = () => console.log(this.a);
test(); // true
var test2 = test.bind({ a: false });
test2(); // Canary: false, Firefox: true