Skip to content

Instantly share code, notes, and snippets.

View qubyte's full-sized avatar

Aura Everitt qubyte

View GitHub Profile
@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 / 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 / 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) {
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 / 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 {
@qubyte
qubyte / range.js
Last active October 6, 2015 15:07
Simple python-like range function for ES2015. Requires at least start and stop parameters, and takes an optional step parameter.
function* range(start, stop, step = 1) {
for (let n = start; n < stop; n += step) {
yield n;
}
}
@qubyte
qubyte / shorthand-constructors.js
Last active November 19, 2015 16:25
In Node.js 4.2.2 and Chrome 47 (probably other versions too) object shorthands cannot be used as constructors.
const test1 = {
MyConstructor: function () {}
};
new test1.MyConstructor(); // fine
const test2 = {
MyConstructor() {}
};
@qubyte
qubyte / replace-labels.js
Last active February 7, 2017 15:49
Replace labels for a repo with standard set.
var token = '<insert token>';
var owner = '<insert owner>';
var repo = '<insert repo>';
var newLabels = [
{color: 'e11d21', name: 'Blocked'},
{color: '000000', name: 'Do Not Merge!'},
{color: 'eb6420', name: 'QA Defect'},
{color: '5319e7', name: 'Waiting for QA'},
{color: '009800', name: 'Waiting for Review'},
@qubyte
qubyte / example-1.js
Last active April 20, 2016 09:02
A promise inverter for testing rejected promises with promise aware unit testing libraries.
import inverter from 'promise-inverter';
// Mocha style test.
it('rejects with an error', () => {
return someRejectedPromise
.then(...inverter)
.then(err => assert.ok(err instanceof Error));
});
@qubyte
qubyte / observable.js
Created May 1, 2016 16:55
Example of an observable using vertebrate-event-emitter.
'use strict';
import Observable from 'an-observable-implementation';
import emitter from 'some-instance-of-vertebrate-event-emitter';
export default new Observable(observer => {
const refs = [
emitter.on('data', data => observer.next(data)),
emitter.on('error', error => observer.error(error)),
emitter.on('end', () => observer.complete())