Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
View mapping-on-unions.ts
// Helpers
type Expect<T extends true> = T
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2)
? true
: false;
View use-spring-animation.ts
import * as React from 'react';
function stepper(
value: number,
velocity: number,
destValue: number,
stiffness: number,
damping: number,
secondPerFrame: number,
precision: number
@gvergnaud
gvergnaud / assigning-properties-in-typescript.ts
Last active Jun 27, 2021
Assigning properties to an Object type in TypeScript
View assigning-properties-in-typescript.ts
/**
* # Assigning properties to an object type in TypeScript
*
* When we want to assign some properties on a object type
* we usually use the `&` operator:
*
* type B = A & { someProperty: string }
*
* It seems to work at first sight, but it doesn't behave exactly
* as we would expect when we try to override properties that already
@gvergnaud
gvergnaud / extends-in-typescript.ts
Last active Jun 27, 2021
How does the extends keyword work in typescript. Interactive playground: https://bit.ly/2XdCEfn
View extends-in-typescript.ts
/**
* # How does `A extends B` work in TypeScript?
*
* If you think about types in terms of sets containing possible values,
* the `string` type is the set of all possible strings,
* the `number` type is the set of all possible numbers,
* the `'hello'` type is a set containing only the string 'hello'
* and the `2` type is a set containing only the number 2.
*
* Then you can think of `A extends B` as asking this question:
@gvergnaud
gvergnaud / canvas.ts
Last active Apr 13, 2020
Some useful canvas utility functions to draw circles, lines and texts
View canvas.ts
type Point = { x: number; y: number };
type Circle = Point & { radius: number; color: string };
type StrokeCircle = Circle & {
width: number;
isDashed: boolean;
lineDash: number[];
};
View lazySpiralGenerator.ts
const between = (min: number, max: number, x: number) =>
Math.max(min, Math.min(max, x));
type Point = { x: number; y: number };
type Direction = 'up' | 'down' | 'left' | 'right';
export function* lazySpiralGenerator(
width: number,
height: number,
View quickSort.js
const quickSort = (xs, compare = (a, b) => a - b) =>
xs.length === 0
? xs
: [
...quickSort(xs.slice(1).filter(x => 0 < compare(xs[0], x))),
xs[0],
...quickSort(xs.slice(1).filter(x => 0 >= compare(xs[0], x))),
]
@gvergnaud
gvergnaud / 01_regexp-tag.js
Last active Mar 11, 2019
Composable RegExp in javascript using template literals.
View 01_regexp-tag.js
/**
Composable RegExps
`r` is an implementation of a tag function to create regular expressions from
a template litteral.
# The why
If you find yourself repeating several times the same pattern from one RegExp
to another one (for example `/[a-zA-Z0-9]{2,54}/`), you probably want to put
it in a variable and define your other RexExps with it. The problem is, the
View frag-shader-utilities.glsl
// Shaping functions
float impulse(float k, float x){
float h = k * x;
return h * exp(1.0 - h);
}
float parabola(float x, float k){
return pow(4.0 * x * (1.0 - x), k);
}
@gvergnaud
gvergnaud / batch.js
Last active Feb 27, 2020
A `batch` function to batch several IOs together to gain in performance with the same api as if you were doing one IO at a time.
View batch.js
import { debounce } from 'lodash';
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve
this.reject = reject
})
}
}