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 / implementation.js
Last active October 27, 2017 11:58
Object.assign, mergeObjectsShallow, mergeObjectsDeep
/**
* Shallow merge objects
*/
function mergeObjectsShallow(target, ...sources) {
for (let i = 0; i < sources.length; i++) {
const source = sources[i];
for (var prop in source) {
if (!source.hasOwnProperty(prop)) continue;
target[prop] = source[prop];
}
@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 / 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 / 1-globals.js
Last active December 7, 2022 22:01
A history of different JavaScript module formats - https://youtu.be/GebL7ToOohE
var MY_CONSTANT = 42
// Implementation of a class
var MyClass = function () {
// constructor
}
MyClass.prototype.getAnswer = function () {
return MY_CONSTANT
}
@branneman
branneman / executedBatched.js
Created November 1, 2016 12:05
executeBatched() – Run a list of async tasks as promises and wait after each batch
/**
* Run a list of async tasks as promises and wait after each batch
* @param {Number} batchSize - Number of tasks to run in parallel
* @param {Number} waitTime - Time to wait after each batch, in milliseconds
* @param {Array<Function>} tasks - Promise executor functions (not promise objects!)
* @return {Promise<Array<*>>}
*/
function executeBatched(batchSize, waitTime, tasks) {
tasks = tasks.slice();
@branneman
branneman / prefixEventListener.js
Created February 10, 2017 10:23
Prefix EventListener
/**
* Capitalize
*/
const capitalize = string => {
return string.charAt(0).toUpperCase() + string.slice(1);
};
/**
* Prefix EventListener
*/
@branneman
branneman / app.js
Last active January 2, 2018 02:23
[FP playground] Generate diceware passwords with NodeJS. See also: https://www.rempe.us/diceware/
const map = fn => list => list.map(fn);
const filter = fn => list => list.filter(fn);
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
const tail = list => list.slice(1);
const fill = val => num => new Array(num).fill(val);
const getFileContents = file => require('fs').readFileSync(file, { encoding: 'utf8' });
const split = char => str => str.split(char);
const join = char => list => list.join(char);
@branneman
branneman / lambda-img-to-webp.js
Created May 5, 2017 12:26
AWS Lambda@Edge whitelisted image to webp request mapper
'use strict';
const whitelist = {
'/static/img/photo.jpg': '/static/img/photo.webp'
};
exports.handler = (event, context, callback) => {
// Grab HTTP request and it's headers
const request = event.Records[0].cf.request;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Homepage</title>
<!-- If HTTP/1: Inline with <style>, remove this <link> -->
<!-- If HTTP/2: Server push this CSS file, and leave this <link> -->
<link rel="stylesheet" href="critical-homepage.css">