Skip to content

Instantly share code, notes, and snippets.

View peeke's full-sized avatar

Peeke peeke

View GitHub Profile
@peeke
peeke / stitch-2d-arrays.js
Last active September 30, 2016 14:25
Stitch multiple 2d arrays together to create a single (larger) 2d array.
stitch2dArrays(arrs) {
return arrs.map(row => {
return row[0].map((col, i) => row.reduce((prev, current) => prev.concat(current[i]), []));
}).reduce((prev, current) => prev.concat(current), []);
}
let arr = [[0, 0, 1],
[0, 0, 1],
[0, 0, 1]];
@peeke
peeke / observer.js
Last active September 21, 2017 07:43
Used for inter-object communication. (Semi-)drop in replacement for Rik Schennink's Observer.
/**
* Used for inter-object communication.
* (Semi-)drop in replacement for Rik Schennink's Observer.
*
* Implementation differences:
* - ES6
* - The use of WeakMaps
* - inform() and conceal() don't return a boolean indicating success.
* - Subscription fn's are called with seperate arguments, instead of one data parameter. This is backwards compatible.
*
@peeke
peeke / optional-singleton.js
Last active February 3, 2017 20:49
Optional singleton pattern: provides a class with a method to retrieve a key based instance.
/**
* Pattern which allows for a class to be optionally instantiated as singleton, using the Foo.getSingleton('foo') method
*/
const instances = new Map();
class Foo {
constructor() {
// Do all kinds of stuff, e.g. keeping state for a page
@peeke
peeke / DataStore.js
Last active October 21, 2016 08:17
A single data store for modules to communicate with.
/**
* A single data store for modules to communicate with. Keeping 'the truth' in a single place reduces bugs and allows
* for greater seperation of concerns.
*
* The module can be used as a singleton through DataStore.getSingleton('key'), or on instance basis through the new keyword.
*
* Uses the Observer module found here: https://gist.github.com/peeke/42a3f30c2a3856c65c224cc8a47b95f9
*
* @name DataStore
* @author Peeke Kuepers
@peeke
peeke / attributeObserver.es6
Last active November 20, 2016 15:09
Function to listen for attribute changes. Useful for preventing inconsistency between your internal module state and the DOM. (IE11+)
const attributeObserver = (element, attr, cb) => {
const mutObserver = new MutationObserver(mutations => {
mutations.forEach(mut => {
mut.attributeName === attr && cb(element.getAttribute(attr));
});
});
mutObserver.observe(element, {
attributes: true,
@peeke
peeke / ArrayLikeObservable.es6
Last active May 30, 2017 21:25
Array function based Steam & EventStream implementation. Also handles promises.
class Observable {
constructor(fn) {
this._fn = fn;
this._children = [];
const handler = {
get(target, prop) {
return prop in target || !(prop in Array.prototype)
@peeke
peeke / _remaining-items.scss
Created December 13, 2016 09:02
Hide items that remains after a division by n.
// Hide items that remains after a division by n.
// E.g.: when using remaining-items(3) on a set of 8 elements, the last two items will be hidden.
@mixin remaining-items($n-per-row) {
&:nth-child(n+#{$n-per-row}) {
&:nth-last-child(-n+#{$n-per-row}) {
&:nth-child(#{$n-per-row}n+#{$n-per-row}) ~ * {
@peeke
peeke / trim.es6
Last active March 22, 2017 12:45
Trim arbitrary html code based on word count. To be used in conjuction with .innerHTML, so unclosed tags are closed automatically.
const trim = (html, wordCount = 16) => {
const wordsAndTags = /<(.|\n)*?>|[^\s\<]+/g;
const tag = /<(.|\n)*?>/;
return html
.match(wordsAndTags)
.reduce((result, match) => {
if (!wordCount) return result;
@peeke
peeke / box.es6
Created May 22, 2017 07:32
The box pattern
// The box pattern:
// Array chaining methods for single values
const box = v => [ v ]
box(1)
.map(n => n * 2)
.map(n => n + 2)
.pop()
@peeke
peeke / KdTree.js
Last active June 20, 2018 11:02
Quickly find the nearest neighbor of a point
class KdTree {
constructor(points, axes = ['x', 'y']) {
this._axes = axes
this._tree = KdTree.build(points, axes)
}
static build(points, axes, depth = 0) {
if (!points.length) return null