Skip to content

Instantly share code, notes, and snippets.

View alaindet's full-sized avatar

Alain D'Ettorre alaindet

View GitHub Profile
@alaindet
alaindet / prettify.ts
Created January 6, 2026 08:08
Prettify TypeScript types in code
/**
* https://www.youtube.com/watch?v=q5DFpyIN5Xs&t=180s
*
* This type helps resolve complex TypeScript types in simpler readable types
*
* - Take a layered complex type (extending multiple types, using generics, inheriting etc.)
* - Create a dummy type via Prettify (type Dummy = Prettify<ComplexType>)
* - Hover with the mouse on the dummy type to see the "resolved" type
*/
export type Prettify<T> = {
class DoubleArrayQueue<T> {
inElements: T[] = [];
outElements: T[] = [];
enqueue(element: T) {
this.inElements.push(element);
}
dequeue() {
this.transferElements();
@alaindet
alaindet / game.mjs
Created October 14, 2024 15:10
[WIP] CLI RPG v1.0
import { createInterface } from "node:readline";
const DIRECTION_AHEAD = "ahead";
const DIRECTION_LEFT = "left";
const DIRECTION_RIGHT = "right";
const DIRECTION_BACK = "back";
const MOVE_ATTACK = "attack";
const MOVE_FLEE = "flee";
@alaindet
alaindet / cached-promises.js
Created September 28, 2024 12:33
Cached Promises
// Thanks to
// https://github.com/microsoft/tsyringe/issues/66#issuecomment-566755746
// https://262.ecma-international.org/6.0/#sec-promise-resolve-functions
main();
async function main() {
const rnd = getRandom();
console.log(await rnd);
console.log(await rnd);
@alaindet
alaindet / range.js
Created September 15, 2024 23:14
Python's `range` in JavaScript
// This is a generator function
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator
function* range(minOrMax: number, max?: number) {
const maxExists = max !== undefined;
const inf = maxExists ? minOrMax : 0;
const sup = maxExists ? max : minOrMax;
for (let i = inf; i < sup; i++) {
yield i;
}
@alaindet
alaindet / visitor-pattern.ts
Last active August 21, 2024 14:27
Visitor Pattern in TypeScript
interface Visitor<TElementTypes = any> {
visit(element: TElementTypes): void;
}
interface ConcreteItem {
accept(visitor: Visitor): void;
}
class ConcreteItemA implements ConcreteItem {
accept(visitor: Visitor): void {
@alaindet
alaindet / main.go
Created July 6, 2024 21:23
Go exercise #1
/**
* What does this script output? Watch out!
*/
package main
import "fmt"
type mystring string
func (s mystring) String() string {
@alaindet
alaindet / many-to-many.sql
Last active April 6, 2024 16:53
SQLite: Example for putting a many-to-many relationship in a single column
CREATE TABLE "users" (
"id" INTEGER NOT NULL UNIQUE,
"email" TEXT NOT NULL UNIQUE,
PRIMARY KEY("id" AUTOINCREMENT)
);
INSERT INTO "users" ("id", "email") VALUES
(1, "alice@example.com"),
(2, "bob@example.com"),
(3, "charlie@example.com");
@alaindet
alaindet / benchmarks_test.go
Created January 8, 2024 16:36
Go slice mapping concurrently
package main
import "testing"
func BenchmarkConcMap100(b *testing.B) {
b.StopTimer()
input := createRange(100)
output := make([]int, 0, len(input))
b.StartTimer()
@alaindet
alaindet / loop-over-promises.js
Last active November 14, 2023 15:03
Loop over Promises
/**
* How can you loop on promises, really?
*
* This experiment tests how promises and loops interact in JavaScript
*
* TL;DR: for await...of is the clear winner
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of
*/
run(); // <-- Start here