Skip to content

Instantly share code, notes, and snippets.

View alaindet's full-sized avatar

Alain D'Ettorre alaindet

View GitHub Profile
@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);
@alaindet
alaindet / flatten.ts
Created December 29, 2021 06:41
Flatten object in Typescript
interface NestedObject {
[key: string]: NestedObject | string;
}
interface FlatObject {
[key: string]: string;
}
const flatten = (input: NestedObject): FlatObject => {
const result: FlatObject = {};
@alaindet
alaindet / serial-parallel.js
Last active November 14, 2021 20:50
JavaScript serial and parallel execution
const delay = 1000;
const iterations = 3;
const fakeRequest = () => new Promise((resolve, reject) => {
setTimeout(() => resolve(11), delay);
});
// Sync
(async () => {
console.time('sync');
const result = [];
@alaindet
alaindet / typescript-sorting-helpers.ts
Created October 24, 2021 13:00
Sorting helpers in TypeScript
type SortingFunction<T = any> = (a: T, b: T) => number;
/**
* Accepts a key, returns an ascending sorting function for that key
*/
const ascending = <T = any>(key: keyof T): SortingFunction<T> => {
return (a: any, b: any) => {
const aValue = a[key];
const bValue = b[key];
return (aValue === bValue) ? 0 : (aValue < bValue ? -1 : 1);
const isNumericString = (digits) => {
if (typeof digits !== 'string') {
return false;
}
return digits.match(/^\d+$/);
};
const deflatten = (flat, separator = '.') => {
const tree = {};
const toBoolean = (context: any, flags: string[]) => {
for (const flag of flags) {
const propValue = context[flag];
const propType = typeof propValue;
if ('boolean' === propType) {
context[flag] = propValue;
}
@alaindet
alaindet / AHK: homeend
Created April 17, 2020 12:30
AutoHotKey script to simulate Home and End with Ctrl+arrow left and Ctrl+arrow right
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
^Left::Send {Home} ; Ctrl + Left = Home
+^Left::Send {LShift down}{Home}{LShift up} ; Ctrl+Shift+Left = Home (select to BOL)
^Right::Send {End} ; Ctrl+Right = End
+^Right::Send {LShift down}{End}{LShift up} ; Ctrl+Shift+Right = End (select to EOL)