Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
/**
* CancelablePromise is a wrapper around a regular Promise which
* can be cancelled by calling `promise.cancel()`.
*
* To do something on cancel, return a callback inside the
* function passed to the CancelablePromise constructor.
*
* Canceled promises will neither be rejected or resolved,
* but will stay unfulfilled.
*
// 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;
import mapValues from 'lodash/mapValues';
export type Step<Anims> = {
[K in keyof Anims]: { value: number; progress: number };
};
export type Values<Anims> = { [K in keyof Anims]: number };
export interface SpringOptions {
/**
@gvergnaud
gvergnaud / assigning-properties-in-typescript.ts
Last active June 27, 2021 15:59
Assigning properties to an Object type in TypeScript
/**
* # 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 June 27, 2021 15:59
How does the extends keyword work in typescript. Interactive playground: https://bit.ly/2XdCEfn
/**
* # 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 April 13, 2020 09:39
Some useful canvas utility functions to draw circles, lines and texts
type Point = { x: number; y: number };
type Circle = Point & { radius: number; color: string };
type StrokeCircle = Circle & {
width: number;
isDashed: boolean;
lineDash: number[];
};
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,
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 March 11, 2019 09:18
Composable RegExp in javascript using template literals.
/**
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
// 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);
}