Instantly share code, notes, and snippets.


Better local require() paths for Node.js


When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

const Article = require('../../../../app/models/article');

Those suck for maintenance and they're ugly.

Possible solutions

View event.js
$(function() {
var resizeEnd;
$(window).on('resize', function() {
resizeEnd = setTimeout(function() {
}, 100);
View _util.js
* This function polls the component's props for Apollo to set `` to `false`
* @example
* await waitDuringLoadingState(testRenderer, TodoContainer)
* @see {@link}
* @param {TestRenderer} testRenderer
* @param {React.Component} testComponent
* @returns {Promise}
export function waitDuringLoadingState(testRenderer, testComponent) {

Primitive Types & Reference Types in JavaScript

An explanation of JavaScript's pass-by-value, which is unlike pass-by-reference from other languages.


  • JavaScript has 2 kinds of variable types: primitive and reference.
  • A fixed amount of memory is reserved after creation of every variable.
  • When a variable is copied, it's in-memory value is copied.
  • Passing a variable to a function via a call also creates a copy of that variable.

Primitive Types

View gist:951847
* Case in-sensitive array_search() with partial matches
* @param string $needle The string to search for.
* @param array $haystack The array to search in.
* @author Bran van der Meer <>
* @since 29-01-2010
def application(environ, start_response):
import sys
if path not in sys.path:
from pyinfo import pyinfo
output = pyinfo()
start_response('200 OK', [('Content-type', 'text/html')])
return [output]
View javascript-duck-typing.js
// Factory
var encrypterFactory = function(config) {
var encrypter = eval('new Encrypt' + config.algorithm.toUpperCase());
if (typeof encrypter.encrypt !== 'function') {
throw new Error( + ' must implement the `encrypt` method!');
return encrypter;
// Implementations
View call-apply-bind-proxy.js
var fn = function(arg1, arg2) {
var str = '<p>aap ' + this.noot + ' ' + arg1 + ' ' + arg2 + '</p>';
document.body.innerHTML += str;
var context = {
'noot': 'noot'
var args = ['mies', 'wim'];
// Calls a function with a given 'this' value and arguments provided individually.

Visual Studio Code: My setup (macOS)

Global settings

git clone ~/.dotfiles
ln -s ~/.dotfiles/vscode-settings.json "~/Library/Application Support/Code/User/settings.json"

Run from terminal

  • Open the Command Palette ( + + P)