This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Observable { | |
constructor(fn) { | |
this._fn = fn; | |
this._children = []; | |
const handler = { | |
get(target, prop) { | |
return prop in target || !(prop in Array.prototype) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}) ~ * { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const trim = (html, wordCount = 16) => { | |
const wordsAndTags = /<(.|\n)*?>|[^\s\<]+/g; | |
const tag = /<(.|\n)*?>/; | |
return html | |
.match(wordsAndTags) | |
.reduce((result, match) => { | |
if (!wordCount) return result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The box pattern: | |
// Array chaining methods for single values | |
const box = v => [ v ] | |
box(1) | |
.map(n => n * 2) | |
.map(n => n + 2) | |
.pop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
OlderNewer