Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@bartwttewaall
bartwttewaall / flatten_unique.js
Created September 2, 2019 15:14
Flatten an array with ES6 functionalities
const getUniqueArr = array => Array.from(new Set(array));
const mergeArrs = (...arrs) => [].concat(...arrs);
const getUniqueMerge = (...arrs) => getUniqueArr(mergeArrs(...arrs));
/** example with nested MenuItems
export interface MenuItem {
label: string;
icon?: string;
link?: Array<any>;
children?: Array<MenuItem>;
@bartwttewaall
bartwttewaall / math-utils.js
Last active June 23, 2022 09:50
Bunch of small math methods like clamp and range
export const RAD2DEG = 180 / Math.PI;
export const DEG2RAD = Math.PI / 180;
// compute euclidean modulo of m % n
// https://en.wikipedia.org/wiki/Modulo_operation
export function euclideanModulo(n: number, m: number) {
return ((n % m) + m) % m;
}
export function clamp(value: number, min: number, max: number) {
# On the server use the htpasswd command to generate a hashed login:
# $ htpasswd -c ~/domains/{domainname}/.htpasswd {username}
# then input a secret password.
<If "req('Host') == '{domainname}.wiredev.nl'">
AuthType Basic
AuthUserFile ~/domains/{domainname}.wiredev.nl/.htpasswd/.htpasswd
AuthName "Acceptance"
# Make an exception for the letsencrypt folder
Require expr %{REQUEST_URI} =~ m#^/.well-known/acme-challenge/#
import { Component, Input } from '@angular/core';
const currencyPattern = /^([\D\s]+?)(?=[+-\d])/;
const trailingZeroesPattern = /(,|.)00$/;
@Component({
selector: 'price',
template: '{{ formattedPrice }}',
styles: [':host { vertical-align: middle; }']
})
@bartwttewaall
bartwttewaall / divide-evenly.js
Created October 3, 2019 10:57
An algorithm that divides items from one or more arrays over one or more output arrays, all in a single for-loop
function divideEvenly(arrays, numOutputs) {
const arraysLength = arrays.length;
const totalLength = arrays.reduce((amount, pool) => amount + pool.length, 0);
const result = [];
let pool, item, column;
for (let i = 0; i < totalLength; i++) {
pool = arrays[i % arraysLength];
item = pool[Math.floor(i / arraysLength)];
column = Math.floor(i / (totalLength / numOutputs));
@bartwttewaall
bartwttewaall / array-fill.js
Last active December 16, 2021 10:31
Array methods, akin to those from lodash / micro-dash
// create an array with indices 0..(amount-1)
export function fill(amount) {
return Array.from(Array(amount).keys());
}
@bartwttewaall
bartwttewaall / column-size-twig.twig
Created November 7, 2019 11:00
Calculate best fitting column size for dynamic list of content (bootstrap 12-columns layout)
{% set colClass = 'col-' ~ (12 / max(2, min(blogPosts.length, 4))) %}
<div class="row">
{% for article in blogPosts %}
<article class="{{ colClass }}">
...
</article>
{% endfor %}
</div>
@bartwttewaall
bartwttewaall / svg-examples.html
Created November 12, 2019 14:13
Settings to layout an svg element
<svg preserveAspectRatio="xMidYMid meet"> <!-- fits the parent, centered -->
<svg preserveAspectRatio="xMidYMid slice"> <!-- fills the parent, centered -->
@bartwttewaall
bartwttewaall / absolute-url.ts
Created November 19, 2019 12:43
Angular tips
// Get the absolute url in various ways:
const url1 = '/' + this.route.pathFromRoot.map(r => r.snapshot.url).filter(f => !!f[0]).map(([f]) => f.path).join('/');
const url2 = this.location.prepareExternalUrl(this.location.path());
@bartwttewaall
bartwttewaall / Vector2.js
Created November 25, 2019 09:27
Vector2 class with many handy methods. Needs to be written as TypeScript some day
export default class Vector2 {
constructor(x, y) {
this.x = x || 0;
this.y = y || 0;
}
// ---- public instance methods ----
get name() {
const v = this.clone().normalize();