Skip to content

Instantly share code, notes, and snippets.

View alaindet's full-sized avatar

Alain D'Ettorre alaindet

View GitHub Profile
@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
@alaindet
alaindet / index.html
Created April 21, 2023 13:05
YALL: Yet Another Layout Library
<!--
Reference
https://codepen.io/alaindet/pen/MWmxrJb
-->
<h1>YALL: Yet Another Layout Library</h1>
<p>
This whole library weights about 5 Kb minified (less than 1 Kb gzipped) and serves the only purpose of <strong>helping you place elements on the page</strong>. YALL provides a responsive grid and a set of utility classes for spacing. That's it. No colors, no components, no typography. Just -SPACE-!
</p>
@alaindet
alaindet / ts-better-enums.ts
Last active November 14, 2023 14:22
TypeScript better enums
//
// Thanks to Matt Pocock's video "Enums considered harmful"
// https://www.youtube.com/watch?v=jjMbPt_H3RQ
//
// Please note that enums per se are great in any language,
// but TypeScript's enums are implemented in a convoluted and verbose way (as of version 4.9)
// since JavaScript (as of ES2022) does not natively support enums
//
// This can be moved elsewhere
@alaindet
alaindet / data-source.ts
Created September 23, 2022 13:39
RxJS self-closing sources
import { BehaviorSubject, Observable, takeUntil } from 'rxjs';
import { OnceSource } from './once-source';
export type UpdaterFn<T> = (prev: T) => T;
/**
* # Data Source
*
* This wraps a data source in a RxJS BehaviorSubject and exposes an API for
@alaindet
alaindet / freeze-files.md
Created September 7, 2022 12:46
Freeze/unfreeze files

How to freeze files in Git

Frozen files are different that ignored files, so that

  • Ignored files are listed in .gitignore and Git never picks them up
  • Frozen files are previously committed files which are later ignored by Git

Frozen files can be configuration files that are frequently changed with secret values and should not be committed.

How to freeze files

@alaindet
alaindet / main.go
Created August 12, 2022 22:26
[GO] Binary string to decimal converter
package main
import "fmt"
func main() {
fmt.Println(
BinaryStringToDecimal("0110"), // 6
BinaryStringToDecimal("0011"), // 3
)
}
@alaindet
alaindet / ranges.ts
Created August 11, 2022 06:03
[TypeScript] Range and Elided Range
function range(fromOrTo: number, to?: number): number[] {
const result: number[] = [];
const [start, stop] = to ? [fromOrTo, to] : [0, fromOrTo];
for (let i = start; i <= stop; i++) {
result.push(i);
}
return result;
}
function elidedRange(total: number, current: number = 1, diameter: number = 5): (number | null)[] {
@alaindet
alaindet / inputs.ts
Created July 11, 2022 13:42
Angular utilities
import { SimpleChange } from '@angular/core';
export function didInputChange(change?: SimpleChange): boolean {
if (!change) return false;
return change.firstChange || change.currentValue !== change.previousValue;
}
export function didInputsChange(changes: SimpleChange[]): boolean {
return changes.some(ch => didInputChange(ch));
}