Skip to content

Instantly share code, notes, and snippets.

Bran van der Meer branneman

Block or report user

Report or block branneman

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@branneman
branneman / better-nodejs-require-paths.md
Last active May 18, 2019
Better local require() paths for Node.js
View better-nodejs-require-paths.md

Better local require() paths for Node.js

Problem

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

@branneman
branneman / client.js
Created Nov 8, 2014
Node.js TCP Socket - if either of them breaks, they'll keep trying to reconnect
View client.js
var net = require('net');
//
// Client
//
function openSocket() {
var socket = net.connect(3e3);
socket.setKeepAlive(true);
socket.on('connect', onConnect.bind({}, socket));
@branneman
branneman / gist:951847
Last active May 9, 2019
array_find() - A case insensitive array_search() with partial matches
View gist:951847
<?php
/**
* 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 <branmovic@gmail.com>
* @since 29-01-2010
*/
@branneman
branneman / app.js
Last active Apr 30, 2019
Node.js application entry-point files
View app.js
#!/usr/bin/env node
'use strict';
var spawn = require('child_process').spawn;
var args = [
'--harmony',
'app/bootstrap.js'
];
@branneman
branneman / getMousePosition.js
Last active Apr 28, 2019
getMousePosition(event) - cross browser normalizing of: clientX, clientY, screenX, screenY, offsetX, offsetY, pageX, pageY
View getMousePosition.js
/**
* @param {Event} evt
* @return {Object}
*/
function getMousePosition(evt) {
var pageX = evt.pageX;
var pageY = evt.pageY;
if (pageX === undefined) {
pageX = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
@branneman
branneman / create-zip.js
Last active Apr 23, 2019
Node.js script to create a zip file from a list of files and directories
View create-zip.js
const stat = require('fs').statSync;
const AdmZip = require('adm-zip');
/**
* Example usage
*/
newArchive(`test-${+new Date}.zip`, [
'index.js',
'package.json',
'node_modules'
@branneman
branneman / howto.md
Last active Apr 23, 2019
Run Debian as docker container
View howto.md

Run Debian as docker container

Setup

Start a container in detached mode:

docker run -dit --name deb -e "LANG=C.UTF-8" -w "/root" debian:latest bash

Connect to it and do initial setup:

@branneman
branneman / primitive-reference-types-javascript.md
Last active Apr 4, 2019
Primitive Types & Reference Types in JavaScript
View primitive-reference-types-javascript.md

Primitive Types & Reference Types in JavaScript

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

Facts

  • 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

@branneman
branneman / fp-lenses.js
Last active Apr 3, 2019
JavaScript: Lenses (Functional Programming)
View fp-lenses.js
// FP Lenses
const lens = get => set => ({ get, set });
const view = lens => obj => lens.get(obj);
const set = lens => val => obj => lens.set(val)(obj);
const over = lens => fn => obj => set(lens)(fn(view(lens)(obj)))(obj);
const lensProp = key => lens(prop(key))(assoc(key));
@branneman
branneman / modules.js
Last active Mar 29, 2019
A history of different JavaScript module formats
View modules.js
/**
* Globals
*/
var Carrousel = function(elem) { this.init() };
Carrousel.prototype = { init: function() {} };
new Carrousel();
/**
* Namespacing
* - No globals (only the namespace variable itself is global)
You can’t perform that action at this time.