Skip to content

Instantly share code, notes, and snippets.

@harmenjanssen
harmenjanssen / home.ejs
Last active June 30, 2016 10:12
Reduced test-case showing the problem with sharing routes between Router instances
<p>This is the homepage.</p>
<a href="<% url('home') %>">A link to myself</a>
<!--
This will throw an error: "No route found with the name:home"
-->
@harmenjanssen
harmenjanssen / parameterizeJqCallback.js
Created November 15, 2016 12:33
Small utility if you like to use jQuery but not so much its this-scoped callbacks.
/**
* Usage:
* $('.my-nodes').each(parameterizeJqCallback(myFunction));
*
*/
const parameterizeJqCallback = (fn) => function(...args) {
return fn($(this), ...args);
};
<?php
/**
* Extract a substring from $input centered around $search with a maximum of $maxLength chars.
*
*
* @param string $input
* @param string $search The substring to search for.
* @param int $maxLength Maxlength of the excerpt, including delimiters.
* @param string $delimiter Character used to denote a break at the front or end of the excerpt.
* @return string
const range = (max, n = 0) =>
n >= max ?
[] :
[n].concat(range(max, n+1));
const sum = (collection, runningTotal = 0) =>
!collection.length ?
runningTotal :
sum(collection.slice(1), collection[0] + runningTotal);
@harmenjanssen
harmenjanssen / divide_functional.js
Created April 6, 2017 22:16
Divide an array into segments.
/**
* Sexy functional bits
* ----------------------------------------------------
*/
const range = max => Array.apply(null, Array(max)).map((_, i) => i);
const slice = (xs, index, size) => xs.slice(index, index + size);
const segmentSize = (len, iteration, segments) =>
Math.ceil((len - iteration) / segments);
@harmenjanssen
harmenjanssen / recursive-to-object.js
Last active August 12, 2017 20:16
Generate object from array of keys and array of values, recursively.
const toObj = (keys, values, index = keys.length - 1) =>
typeof keys[index] === "undefined"
? {}
: Object.assign(
toObj(keys, values, index - 1),
{
[keys[index]]: values[index]
}
);
@harmenjanssen
harmenjanssen / isEven.js
Created August 20, 2017 10:56
Recursive is-even function. A classic.
const isEven = n => n === 0 ?
true :
!isEven(n - 1);
@harmenjanssen
harmenjanssen / reverse.js
Last active August 20, 2017 11:57
Recursive reverse function.
const reverse = ([head, ...rest]) =>
!head ? [] : reverse([...rest]).concat([head]);
@harmenjanssen
harmenjanssen / bookmarklet.txt
Last active August 23, 2017 06:24
Bookmarklet for unfixing position:fixed elements
javascript:(function()%7B(function()%20%7B'use%20strict'%3Bconst%20isFixed%20%3D%20elm%20%3D%3E%20%7Bconst%20cs%20%3D%20window.getComputedStyle(elm%2C%20null)%3Breturn%20cs%20%3F%20cs.getPropertyValue('position')%20%3D%3D%3D%20'fixed'%20%3A%20false%3B%7D%3Bconst%20findInDomAncestry%20%3D%20(matchFn%2C%20node)%20%3D%3E!node%20%7C%7C%20node%20%3D%3D%3D%20document%20%3F%20undefined%20%3AmatchFn(node)%20%3F%20node%20%3AfindInDomAncestry(matchFn%2C%20node.parentNode)%3Bconst%20clickListener%20%3D%20(e)%20%3D%3E%20%7Bconst%20fixedTarget%20%3D%20findInDomAncestry(isFixed%2C%20e.target)%3Bif%20(fixedTarget)%20%7BfixedTarget.style.position%20%3D%20'static'%3B%7Ddocument.body.style.cursor%20%3D%20''%3Bdocument.removeEventListener('click'%2C%20clickListener)%3B%7D%3Bdocument.body.style.cursor%20%3D%20'crosshair'%3Bdocument.addEventListener('click'%2C%20clickListener)%3B%7D)()%7D)()