Skip to content

Instantly share code, notes, and snippets.

View qubyte's full-sized avatar

Aura Everitt qubyte

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / styleGuide.md
Created November 5, 2013 12:43
Qubyte's JavaScript style guide

Style guide

Well written code by several authors should look like well written code by one author. To this end, everyone should write code in the same style and formatting. This serves multiple purposes, and it should be pretty clear what the benefits of consistent style with no arguments are.

This is the guide for the Entrago CMS backend codebase.

Basic formatting

@qubyte
qubyte / nest.js
Created October 10, 2013 09:55
Nest one asynchronous function in another. Both functions are optional. MIT licence.
function nest(before, after) {
'use strict';
if (typeof before !== 'function') {
// Only after is defined, so just return it.
if (typeof after === 'function') {
return after;
}
// Neither function is defined, so return a function that just executes a callback.
@qubyte
qubyte / Gruntfile.js
Last active December 20, 2015 21:29
My Gruntfile.
/**
* Mark S Everitt 2013
* Licence: MIT
*/
var jshintConfig = {
files: ['gruntfile.js', 'lib/**/*.js', 'test/**/*.js', 'index.js'],
options: {
// Just an example.
globals: {
@qubyte
qubyte / haversine.js
Created May 28, 2013 17:12
Haversine formula for stable distance calculation between pairs of coordinates.
// All angles in radians.
function haversine(aLong, aLat, bLong, bLat) {
var R = 6371; // Radius of Earth.
var latDiff = bLat - aLat;
var longDiff = bLong - aLong;
var step1 = Math.sin(latDiff / 2);
var step2 = Math.sin(longDiff / 2);
var step3 = step1 * step1 + step2 * step2 * Math.cos(aLat) * Math.cos(bLat);
@qubyte
qubyte / humanJshintReporter.js
Last active August 2, 2016 12:41
The default JSHint output is somewhat wasteful, and very bland, making it hard for a human to parse. The following does IMO a better job and colours the output for you. This requires the `colors` package, available via npm. If you don't want the dependency, just remove the color commands from strings. MIT licence.
/**
* The default JSHint output is somewhat wasteful, and very bland, making it hard for a human to
* parse. The following does IMO a better job and colours the output for you. This requires the
* `colors` package, available via npm. If you don't want the dependency, just remove the color
* commands from strings.
*
* Usage: When invoking jshint from the command line, point it to this file with the --reporter
* flag. e.g.
*
* jshint someFile.js --config /path/to/config.cfg --reporter /path/to/jshintReporter.js