Skip to content

Instantly share code, notes, and snippets.

View bartwttewaall's full-sized avatar

Bart Wttewaall bartwttewaall

View GitHub Profile
@bartwttewaall
bartwttewaall / FitFillTest.js
Last active September 15, 2024 16:44
Fit, fill, stretch a texture on a mesh when given the mesh's aspect ratio
import {
Object3D,
Cache,
Texture,
PlaneGeometry,
MeshLambertMaterial,
Mesh,
ClampToEdgeWrapping,
RepeatWrapping,
MirroredRepeatWrapping,
@bartwttewaall
bartwttewaall / debounce.js
Created November 20, 2020 10:52
Debounce function with jsdoc and example
/**
* Debounce function that, as long as it continues to be invoked, will not be triggered.
* @param {Function} func - Function to be debounced
* @param {number} wait - Time in milliseconds to wait before the function gets called.
* @param {boolean} [immediate] - Optional immediate flag, if passed, trigger the function on the leading edge, instead of the trailing.
* @returns {Function}
* @example
window.addEventListener('resize', debounce((evt) => console.log(evt), 250));
*/
export function debounce(func, wait, immediate) {
@bartwttewaall
bartwttewaall / assert.ts
Created September 25, 2020 07:20
Assert methods for testing purposes
export const assert = (expectation: any, result: any, test: string = '') => {
if (typeof expectation !== typeof result) {
test += ` TYPE DIFFERENCE ${typeof expectation} compared to ${typeof result}`;
return output(expectation, result, test, false);
}
if (typeof expectation === 'object') {
return assertObj(expectation, result, test);
} else if (Array.isArray(expectation)) {
return assertArray(expectation, result, test);
} else {
@bartwttewaall
bartwttewaall / CssStyleObject.ts
Last active January 4, 2022 08:46
HashTable and CssStyleObject
type Dictionary<T> = { [key: string]: T };
type Nullable<T> = T | null;
// Use `&` for creating an intersection type!
type CssStyleObject = Partial<CSSStyleDeclaration> & Dictionary<Nullable<string>>;
/** Example
* used for declaring partial css style objects
**/
interface MyState {
@bartwttewaall
bartwttewaall / html-link-to-object.ts
Last active July 29, 2020 09:46
Parse html links in a string to an object
@bartwttewaall
bartwttewaall / html-link-to-object.ts
Created July 29, 2020 09:17
Parse html links in a string to an object
@bartwttewaall
bartwttewaall / array-set-operations.js
Last active May 25, 2021 08:28
Set opterations on 2 arrays: intersection, difference, union
// Intersection
let intersection = arrA.filter(x => arrB.includes(x));
// Difference
let difference = arrA.filter(x => !arrB.includes(x));
// Symmetrical Difference 1st syntax
let symmetricalDifference = arrA
.filter(x => !arrB.includes(x))
.concat(arrB.filter(x => !arrA.includes(x)));
@bartwttewaall
bartwttewaall / MyExtensions.php
Created May 1, 2020 09:40
Twig usort extension
<?php
declare(strict_types=1);
namespace App\MyBundle\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
final class MyExtensions extends AbstractExtension
@bartwttewaall
bartwttewaall / sync-fork.git
Created April 30, 2020 12:35
Sync a private repository to a fork repository (used by Spicytrip)
I forked the repo to my own account and then cloned it to my PC
git clone https://github.com/bartwttewaall/spicytrip-web.git
Then I added a new upstream repository as specified in this article with the original repository url from wirelab
git remote add upstream https://github.com/wirelab/spicytrip-web.git
I then double checked the remotes with
git remote -v
and I now see both 2 origin and 2 upstream remotes.
function setupDownload() {
// quick test to check if download is available on anchors (fake Modernizr implementation)
var anchor = document.createElement("A");
var Modernizr = { adownload: "download" in anchor };
delete anchor;
if (Modernizr.adownload) {
var downloads = document.querySelectorAll("a[download]");
for (var i = 0; i < downloads.length; i++) {