This file contains hidden or 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
/** | |
* 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. | |
* |
This file contains hidden or 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
// 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; |
This file contains hidden or 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
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 { | |
/** |
This file contains hidden or 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
/** | |
* # 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 |
This file contains hidden or 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
/** | |
* # 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: |
This file contains hidden or 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
type Point = { x: number; y: number }; | |
type Circle = Point & { radius: number; color: string }; | |
type StrokeCircle = Circle & { | |
width: number; | |
isDashed: boolean; | |
lineDash: number[]; | |
}; |
This file contains hidden or 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 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, |
This file contains hidden or 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 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))), | |
] |
This file contains hidden or 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
/** | |
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 |
This file contains hidden or 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
// 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); | |
} |