Skip to content

Instantly share code, notes, and snippets.

View qubyte's full-sized avatar

Mark S. Everitt qubyte

View GitHub Profile
@qubyte
qubyte / asyncRunner.js
Last active October 7, 2015 08:04
Simple asyncRunner. Used as an example for what async-await might desugar to.
function subRunner(gen) {
const state = gen.next();
const thisPromise = Promise.resolve(state.value);
return state.done ? thisPromise : thisPromise.then(subRunner(gen));
}
function asyncWrapper(genFunc) {
return function () {
try {
const promises = ['some promises in here'];
const waitForPromises = asyncRunner(function* (promises) {
for (const promise of promises) {
yield promise;
}
// At this point all the promises have resolved.
});
@qubyte
qubyte / uuid.v4.js
Last active August 29, 2015 14:17
UUID version 4 using browser crypto API. Not particularly optimised.
function makeUuid(){
var randomValues = window.crypto.getRandomValues(new window.Uint8ClampedArray(16));
var randomHex = '';
for (var i = 0; i < randomValues.length - 1; i++) {
var hex = randomValues[i].toString(16);
if (hex.length === 0) {
randomHex += '00';
} else if (hex.length === 1) {
@qubyte
qubyte / fetchAntiShim.js
Last active August 29, 2015 14:12
Prevent the github/fetch shim actually shimming, and assign it instead to a variable.
// brfs is needed to use this anti-shim.
var fs = require('fs');
var functionBody = [
'var window = {};',
fs.readFileSync(__dirname + '/path/to/node_modules/fetch/fetch.js', 'utf8'), // Fix the path for your needs.
'return window.fetch;'
].join('\n');
// Assigns a function fetch function to module.exports. Here I elect to use a native Promise implementation (or
// polyfilled). This is trivial to adapt into a library that can take a user defined Promise.
@qubyte
qubyte / ibfe.markdown
Last active August 29, 2015 14:07
Fat arrow function placeholders.

Whilst I wait for better support for fat arrow functions, they can be emulated with what I like to call an immediately bound function expression (IBFE).

For example this:

var test = (a, b, c) => console.log(a, b, c, this.xyz);

Can be emulated with:

@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
@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 / 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:

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 / unshortener.dart
Last active October 3, 2018 13:34
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;