Skip to content

Instantly share code, notes, and snippets.

View qubyte's full-sized avatar

Mark S. Everitt qubyte

View GitHub Profile
@qubyte
qubyte / json.js
Last active September 3, 2017 12:24
rollup plugin for wrapping a JSON file in a module with a default export only. This avoids the issue of fields being invalid names.
// An as-simple-as-possible JSON plugin for rollup. This plugin turns a JSON
// file into a module with a default export. No named exports are given since
// field names are not always valid names.
// Originally drawn from the official JSON plugin.
function buildAst(code) {
return {
type: 'Program',
sourceType: 'module',
@qubyte
qubyte / observe-key.js
Created May 27, 2017 13:37
A module which returns an observable for a key. It streams `pressed` and `released` observations. Also has a getter for the pressed state of a key.
import Observable from 'zen-observable';
export default function observeKey(key) {
let pressed = false;
const observable = new Observable(observer => {
function downHandler(e) {
if (e.key === key && !pressed) {
pressed = true;
observer.next('pressed');
@qubyte
qubyte / arcade.ino
Last active July 4, 2021 13:10
Arduino Leonardo code to behave as a keyboard with pins set to MAME-ish key bindings.
#include <Keyboard.h>
// MAMEish. An array of pin-key pairs.
struct { int pin; int key; } pinsToKeys[] = {
{ 2, KEY_LEFT_ARROW },
{ 3, KEY_UP_ARROW },
{ 4, KEY_RIGHT_ARROW },
{ 5, KEY_DOWN_ARROW },
{ 6, KEY_LEFT_CTRL }, // Fire 1
{ 7, KEY_LEFT_ALT }, // Fire 2
@qubyte
qubyte / gyoza.md
Last active August 25, 2020 15:21

Miya's famous gyoza

Miya makes gyoza on the fly, and doesn't work to exact quantities. However, this recipe is what she used the last time we made some.

Step 1

For the first step you will need:

  • 3 cloves of garlic, finely diced or minced
  • 2 tbsp ginger, finely diced or minced
@qubyte
qubyte / getScrollAmount.js
Created October 13, 2016 17:17
Little function to get the amount a document has been scrolled by.
function getScrollAmount() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
return scrollTop / (scrollHeight - document.documentElement.clientHeight);
}
@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())
@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 / 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 / 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 / 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;
}
}