Skip to content

Instantly share code, notes, and snippets.

View alaindet's full-sized avatar

Alain D'Ettorre alaindet

View GitHub Profile
@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));
}
@alaindet
alaindet / async-examples.spec.ts
Created June 20, 2022 08:56
Async testing in Angular
it('With promises and setTimeouts', fakeAsync(() => {
let counter = 0;
Promise.resolve().then(() => {
counter += 10;
setTimeout(() => counter += 1, 100);
});
expect(counter).toBe(0);
flushMicrotasks(); // Run promises
@alaindet
alaindet / git-gotchas.md
Last active May 23, 2022 13:43
Git Gotchas

Git Gotchas

Rename a remote branch

git checkout OLD_NAME
git pull
git branch -m OLD_NAME NEW_NAME
git push --delete origin OLD_NAME
git push --set-upstream origin NEW_NAME
@alaindet
alaindet / climb-test.js
Last active April 1, 2022 14:07
CLImb - CLI parser in JavaScript
// node ./test1.js --bool1 -2 --foo aa --bar=bb -f aaa -abc arg1 arg2
const climb = require('./climb');
const input = climb(process.argv, {
bool1: {
longName: 'bool1',
},
bool2: {
longName: 'bool2',
shortName: '2',
@alaindet
alaindet / coding-challenge-intersecting-rectangles.js
Created February 8, 2022 20:46
Coding Challenge: Intersecting rectangles
/**
* Coding Challenge: Intersecting rectangles
*
* Write a function calculateRectanglesIntersection that accepts two rectangles
* (in string format like "(0,0),(0,4),(6,4),(0,6)") and returns the value of
* the intersection area
*/
const getRectangleCoordinates = (coordsInput) => {
let temp = coordsInput.slice(1, coordsInput.length - 1);