Skip to content

Instantly share code, notes, and snippets.

View branneman's full-sized avatar

Bran van der Meer branneman

View GitHub Profile
@branneman
branneman / task-runner.js
Created April 21, 2016 08:32
Streaming task-runner (how gulp works, simplified)
'use strict';
const fs = require('fs');
const glob = require('glob').sync;
const mkdirp = require('mkdirp').sync;
const sass = require('node-sass');
const path = require('path');
const streams = require('stream');
const runner = {};
@branneman
branneman / MyModule.js
Last active June 15, 2016 13:26
Conditioner.js blueprint module
/**
* @module MyModule
*/
define(['dep1'], function(dep1) {
"use strict";
/**
* @param {HTMLElement} element
* @param {Object} options
@branneman
branneman / invoker.js
Last active June 17, 2016 13:50
Function Invoker - Repeatedly invokes a function until it returns a truthy value
/**
* Invoker
* Repeatedly invokes a function until it returns a truthy value
*
* @see https://gist.github.com/branneman/53b820be519b54bfc30a
* @param {Number} limit - The amount of total calls before we timeout
* @param {Number} interval - The amount of milliseconds between calls
* @param {Function} fn - The function to execute, must return a truthy value to indicate it's finished
* @param {Function} cb - The callback for when we're finished. Recieves 2 arguments: `error` and `result`
*/
// (c) copyright unscriptable.com / John Hann
// License MIT
// For more robust promises, see https://github.com/briancavalier/when.js.
function Promise () {
this._thens = [];
}
Promise.prototype = {
@branneman
branneman / Observer.js
Created September 5, 2016 14:08 — forked from peeke/observer.js
Used for inter-object communication. (Semi-)drop in replacement for Rik Schennink's Observer.
/**
* Used for inter-object communication.
* (Semi-)drop in replacement for Rik Schennink's Observer.
*
* Implementation differences:
* - ES6
* - The use of WeakMaps
* - inform() and conceal() don't return a boolean indicating success.
* - Subscription fn's are called with seperate arguments, instead of one data parameter. This is backwards compatible.
*
@branneman
branneman / es2015-returning-from-constructor.js
Last active September 20, 2016 16:34
ES2015: Returning a Promise from a constructor — Warning: I'm fairly sure this is always an anti-pattern.
class Parent {
constructor() {
return new Promise(resolve => {
setTimeout(() => resolve({ data: 'important' }), 1e3);
});
}
}
class Child extends Parent {
constructor() {
@branneman
branneman / index.html
Last active September 27, 2016 11:49
Conditioner.js bootstrap with JavaScript Mustard Cut & Polyfill loading
<!doctype html>
<html>
<head>
...
<script>
(function(d){
if (!('querySelector' in d && 'addEventListener' in window)) return;
d.documentElement.className += ' has-js';
d.addEventListener('DOMContentLoaded', function() {
var s = d.createElement('script');
@branneman
branneman / pair.js
Last active September 27, 2016 12:06
Pairs (length 2 tuples) in JavaScript
/**
* Pair
* @todo Implement algebraic structures: Setoid, Functor
*/
var Pair = function(fst, snd) {
if (this instanceof Pair) {
if (Array.isArray(fst) && fst.length === 2 && typeof snd == 'undefined') {
this[0] = fst[0];
this[1] = fst[1];
} else {
@branneman
branneman / public-key-crypto.js
Last active October 11, 2016 06:45
Node.js Public-key cryptography microlib :: Encypt against public key & Decrypt against private key
const crypto = require('crypto');
const path = require('path');
const fs = require('fs');
// Expose module
module.exports = { encrypt, decrypt };
/**
* Encypt against public key
* @param {String} str - The string to encrypt
@branneman
branneman / es5-features.js
Last active October 20, 2016 15:48
ECMAScript 5 In code examples
/**
* Strict mode
* Opt in to a restricted variant of JavaScript.
*/
'use strict';
(function() { 'use strict'; });
/**
* Array.prototype.forEach()
* Executes a provided function once per array element.