Skip to content

Instantly share code, notes, and snippets.

Avatar
🖤
Loving programming

Andrea Simone Costa jfet97

🖤
Loving programming
View GitHub Profile
@jfet97
jfet97 / flattens.js
Created Aug 3, 2022
Recursive, tail-recursive and iterative versions of flatten
View flattens.js
{
function flatten_it(a) {
let res = [...a]
while (res.some(el => Array.isArray(el))) {
for (let i = 0; i < res.length; i++) {
if (Array.isArray(res[i])) {
res.splice(i, 1, ...res[i])
}
@jfet97
jfet97 / invert.js
Last active May 27, 2022
tail-call recursive binary tree inverter
View invert.js
function invert(tree, toInvR = [], path = [], res = null) {
// tree == nodo corrente
// path[path.length - 1] == nodo padre
if (!tree) {
return null;
}
const parent = path[path.length - 1]; // is undefined iif res === null
const newNode = Node(tree.value);
@jfet97
jfet97 / string_types.ts
Last active May 10, 2022
types for string long exact/at least/at most n chars
View string_types.ts
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
? 1
: 2
? true
: false;
interface StringCostrainType {
readonly _N: number;
readonly _E: unknown;
readonly _type: "exact" | "atleast" | "atmost";
View Extends.ts
export type Extends<A, B, Y = unknown, N = never> =
[A] extends [never] // <- ottimizzo evitando la distribution, altrimenti nello step dopo farei tot A extends B identici
? N
: A extends B
? Y
: N
// type IsNever<A> = A extends never ? true : false risulta in never se A = never
// (entra in gioco la distribution, ma non c'è nulla da distribuire!)
//
@jfet97
jfet97 / merge2.ts
Last active May 8, 2022
merge union into object mantaining keys union and modifiers
View merge2.ts
type AllKeys<T> = T extends unknown ? keyof T : never;
type Lookup<T, K extends PropertyKey> = T extends any ? T[K & keyof T] : never;
type MergeAsUnion<T> = { [K in AllKeys<T>]: Lookup<T, K> };
type IfEquals<T, U, Y = unknown, N = never> = (<V>() => V extends T
? 1
: 2) extends <V>() => V extends U ? 1 : 2
? Y
: N;
@jfet97
jfet97 / stringa_lunga_almeno_5.ts
Last active May 10, 2022
Il tipo di una stringa lunga almeno 5
View stringa_lunga_almeno_5.ts
// 6 any perché l'ultimo "viene inferito" come la stringa vuota "", gli altri 5 acchiappano un carattere ciascuno
type String5<T extends string> = T extends `${any}${any}${any}${any}${any}${any}` ? T : "The string must be at least 5 characters long" // oppure never
type testmeno1 = String5<string> // nope
type test0 = String5<""> // nope
type test1 = String5<"a"> // nope
type test2 = String5<"ab"> // nope
type test3 = String5<"abc"> // nope
type test4 = String5<"abcd"> // nope
type test5 = String5<"abcde"> // "abcde"
@jfet97
jfet97 / index_tuple.ts
Last active Apr 20, 2022
Valid numerical index tuple
View index_tuple.ts
type IndexesKeysOfTuple<T extends any[]> = Exclude<
keyof T & string,
keyof any[]
>;
type NumberToString<N extends number | bigint> = `${N}`;
type ValidIndex<T extends any[], I extends number> = any[] extends T
? number // T is an array but not a tuple type
: NumberToString<I> extends IndexesKeysOfTuple<T>
View typescript-exercises-6-solution.ts
/*
Intro:
Filtering requirements have grown. We need to be
able to filter any kind of Persons.
Exercise:
Fix typing for the filterPersons so that it can filter users
@jfet97
jfet97 / merge.ts
Last active May 7, 2022
Merge an union of objects with different types for the same properties
View merge.ts
type AllKeys<T> = T extends unknown ? keyof T : never
// type Idx<T, K extends PropertyKey> = T extends any ? T[K & keyof T] : never
type Idx<T, K extends PropertyKey> = T extends unknown ? K extends keyof T ? T[K] : never : never;
type MyMerge<T> = { [K in AllKeys<T>]: Idx<T, K> }
// TODO: result of test case must be `{foo: 1 | 2; bar: 3}`
type TestCase1 = MyMerge<{ foo: 1 } | { foo: 2; bar: 3 }>;
// TODO: result of test case must be `{foo: 1 | 2 | 7; bar: 3 | 8}`
type TestCase2 = MyMerge<
@jfet97
jfet97 / filterUsingIntersections.ts
Last active Apr 1, 2022
Use intersections to filter unions
View filterUsingIntersections.ts
type Decision = "yes" | "no" | 1 | 0 | true | false;
type JustStrings = Decision & string;
/// = ("yes" | "no" | 1 | 0 | true | false) & string
/// = | ("yes" & string) | ("no" & string)
/// | (1 & string) | (0 & string)
/// | (true & string)| (false & string))
/// = "yes" | "no" | never | never | never | never
/// = "yes" | "no"
//