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 / fp-lenses.js
Last active Jul 20, 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 / better-nodejs-require-paths.md
Last active Jul 20, 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 / matchAll.js
Last active Jul 17, 2019
JavaScript RegExp: Get all matches and capture groups
View matchAll.js
'use strict'
const { isNull, isStr, isRegExp } = require('./types')
module.exports = matchAll
/**
* RegExp: Get all matches and capturing groups
* @param {RegExp} re
* @param {String} str
@branneman
branneman / primitive-reference-types-javascript.md
Last active Jul 17, 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 / salt-hash-password.js
Last active Jul 12, 2019
Node.js: Create salted hashed password
View salt-hash-password.js
const crypto = require('crypto')
const { promisify } = require('util')
const pbkdf2 = promisify(crypto.pbkdf2)
module.exports = { createHashPasswordFn, isPasswordCorrect }
/**
* @typedef {Object} HashPassword
* @property {String} hash
* @property {String} salt
@branneman
branneman / getMousePosition.js
Last active Jul 7, 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 / call-apply-bind-proxy.js
Last active Jul 2, 2019
JavaScript call() vs apply() vs bind() vs $.proxy()
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.
@branneman
branneman / gist:951847
Last active Jun 21, 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 / fp.js
Last active Jun 8, 2019
JavaScript functional programming library
View fp.js
const { curryN, curry } = require('./curry') // https://gist.github.com/branneman/4ffb7ec3fc4a2091849ba5d56742960c
// Array
const length = list => list.length
const nth = curry((key, list) => list[key])
const slice = curry((from, to, list) => list.slice(from, to))
const head = nth(0)
const init = slice(0, -1)
const tail = slice(1, Infinity)
const last = slice(-1, Infinity)
@branneman
branneman / curry.js
Last active Jun 7, 2019
Higher order auto curry function
View curry.js
const curryN = (fn, arity, accIn = []) => (...args) => {
const len = args.length
const accOut = accIn.concat(args)
if (len + accIn.length >= arity) {
return fn.apply(this, accOut)
}
return curryN(fn, arity, accOut)
}
const curry = fn => curryN(fn, fn.length)
You can’t perform that action at this time.