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 Nov 12, 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 / primitive-reference-types-javascript.md
Last active Nov 10, 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 / gist:951847
Last active Nov 10, 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 / svg-classes.js
Created Jan 15, 2014
SVG — hasClass, addClass, removeClass, toggleClass
View svg-classes.js
//
// SVG — hasClass, addClass, removeClass, toggleClass
// Source:
// https://gist.github.com/branneman/8436956
// Taken and adapted from:
// http://toddmotto.com/hacking-svg-traversing-with-ease-addclass-removeclass-toggleclass-functions/
//
if (SVGElement && SVGElement.prototype) {
@branneman
branneman / fp-lenses.js
Last active Oct 25, 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));
View snake.clj
(ns main (:gen-class))
;; Snake :: [[int int]…]
;; vector of x,y tuples
;; head is first, tail is last
;; out-of-bounds? :: (int int int) -> bool
;; grid-size is 1-indexed, x+y are 0-indexed
(defn out-of-bounds? [grid-size x y]
(or (neg? x) (neg? y) (>= x grid-size) (>= y grid-size)))
View clojure-predicate-functions.md

List of all Clojure Predicate functions

A predicate function returns a boolean true or false, and it's name ends with a ?. Ran against Clojure 1.10.0.

({:ns clojure.string,
  :predicates
  ({:fn ends-with?, :arglists ([s substr])}
   {:fn starts-with?, :arglists ([s substr])}
   {:fn includes?, :arglists ([s substr])}
@branneman
branneman / create-zip.js
Last active Sep 29, 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'
View Struct.php
<?php
class Struct
{
/**
* Define a new struct object, a blueprint object with only empty properties.
*/
public static function factory()
{
$struct = new self;
foreach (func_get_args() as $value) {
@branneman
branneman / fp.js
Last active Sep 18, 2019
JavaScript functional programming library
View fp.js
const { curryN, curry } = require('./curry') // https://gist.github.com/branneman/4ffb7ec3fc4a2091849ba5d56742960c
// Array higher-order
const map = curry((fn, list) => list.map(fn))
const filter = curry((fn, list) => list.filter(fn))
const foldl = curry((fn, init, list) => list.reduce(fn, init))
const foldr = curry((fn, init, list) => list.reduceRight(fn, init))
const find = curry((fn, list) => list.find(fn))
const sort = curry((fn, list) => list.slice().sort(fn))
const includes = curry((fn, list) => list.includes(fn))
You can’t perform that action at this time.