Skip to content

Instantly share code, notes, and snippets.

View alexandercerutti's full-sized avatar

Alexander Cerutti alexandercerutti

View GitHub Profile
type BaseList = ["🛹", "🚲", "🛴", "🏄"];
type RepeatAsTuple<
Item extends string,
Times extends number,
Result extends string[] = [],
> = Result["length"] extends Times ? Result : RepeatAsTuple<Item, Times, [...Result, Item]>;
type Rebuild<
AmountsList extends number[],
enum States {
LOADING /***/ = 0b0000,
FAILED /****/ = 0b0001,
LOADED /****/ = 0b1110,
PLAYING /***/ = 0b0100,
STOPPED /***/ = 0b1000
}
const currentState: number = getStateSomehow();
@alexandercerutti
alexandercerutti / right-identifiers.ts
Last active December 29, 2021 14:42
bitwise-article
/**
* In grid reading, it is important to have all the bits aligned.
* Unaligned bits can lead to misreading and therefore to bugs.
*
* Trust me, I've shipped in production a product that had a
* broken feature due to a wrong bit.
*/
enum States {
LOADING /***/ = 0b0000, /* Set to 0 because it is the initial state */
@alexandercerutti
alexandercerutti / wrong-identifiers.ts
Last active December 29, 2021 14:42
bitwise-article
/**
* In grid reading, it is important to have all the bits aligned.
* Unaligned bits can lead to misreading and therefore to bugs.
*
* Trust me, I've shipped in production a product that had a
* broken feature due to a wrong bit.
*/
enum States {
LOADING /***/ = 0b000000, /* Set to 0 because it is the initial state */
@alexandercerutti
alexandercerutti / bitwise-grid-reading.ts
Last active December 28, 2021 23:19
bitwise-article
/**
* In JS, we can write bits by using prefix `0b`.
* In this example, bits are separated by a space just to make
* it simple to apply grid reading.
*
* Grid Reading means picking one column at time and reasoning only
* on that column. This will be helpful in the article.
*
* Please, also note that in JS, 0b numbers can use up to 32 bits (2^32 = 4294967296).
*/
@alexandercerutti
alexandercerutti / state-usage.ts
Last active December 28, 2021 22:54
Bitwise-article-example
const currentScriptState = getCurrentScriptStateSomehow();
if (currentScriptState === States.LOADING) {
abortCurrentOperationUntilLoaded();
}
if (currentScriptState === States.FAILED) {
skipScriptOperation();
}
@alexandercerutti
alexandercerutti / state-consts-1.ts
Last active December 28, 2021 22:46
bitwise-article-example-code
const LOADING = "Loading";
const FAILED = "Failed";
const LOADED = "Loaded";
const PLAYING = "Playing";
const STOPPED = "Stopped";
// or
enum States {
LOADING = "Loading",