[go: nahoru, domu]

Skip to content

Instantly share code, notes, and snippets.

View albertms10's full-sized avatar
🏠
Working from home

Albert Mañosa albertms10

🏠
Working from home
View GitHub Profile
@albertms10
albertms10 / awaitedResults.ts
Last active November 28, 2023 10:59
TypeScript type to await an array of Results
type ResultSuccess<T> = { success: true; value: T };
type ResultFailure<T> = { success: false; error: T };
type ExtractFailure<T> = T extends ResultFailure<infer U> ? U : never;
type ExtractSuccess<T> = T extends ResultSuccess<infer U> ? U : never;
/**
* Represents the awaited results of an array of promises, each of which can resolve
* to either `ResultSuccess` or `ResultFailure`.
*
@albertms10
albertms10 / camelToKebab.ts
Last active May 14, 2024 16:42
TypeScript type to convert camel-cased strings to kebab-cased
/**
* Converts the given string from camel-case to kebab-case.
* @template T The string to convert the case.
* @see https://gist.github.com/albertms10/09f14ef7ebdc3ce0e95683c728616253
* @example
* type Kebab = CamelToKebab<'exampleVarName'>;
* // 'example-var-name'
*/
type CamelToKebab<S extends string> = S extends `${infer T}${infer U}`
? U extends Uncapitalize<U>
@albertms10
albertms10 / nonNullableCollection.ts
Last active October 26, 2023 13:56
TypeScript type that removes nullable properties from a collection (object or array)
/**
* Removes nullable properties (with `null` or `undefined` values) from an object or array.
* @template T The object or array to remove nullable properties from.
* @see https://gist.github.com/albertms10/fb5a6d87a97db584086241d5bad74a41
* @example
* type A = { a?: string; b: null; c?: number | null };
* type B = NonNullableCollection<A>;
* // { a: string; c: number; }
* @example
* type A = [string, undefined, number, null];
@albertms10
albertms10 / yarnWorkspacesDependencies.js
Last active October 24, 2023 21:45
Yarn workspaces dependencies
import { execSync } from 'child_process';
import { parseArgs } from 'node:util';
export const { values: args } = parseArgs({
options: {
workspace: { type: 'string', short: 'w' },
},
});
const foundDependencies = new Set();
@albertms10
albertms10 / nestedObjectPaths.ts
Last active June 5, 2024 23:39
TypeScript — all possible paths of an object
/**
* Object type that represents all possible paths of an object,
* including optional members.
* @template T The object type to get the paths from.
* @see https://stackoverflow.com/a/76131375/10851645
* @see https://gist.github.com/albertms10/5a8b83e436a1689aa4b425ec22058301
* @example
* interface Package {
* name: string;
* man?: string[];
@albertms10
albertms10 / graphNodes.md
Created January 5, 2023 13:34
Graph Nodes in TypeScript

Graph Nodes in TypeScript

Description

In the process of implementing a basic graph system with nodes that point to other nodes (node → node) using an identifier code, we got to this simple but acceptable solution:

interface GraphNode {
  code: number;
  next: number | null;
@albertms10
albertms10 / date-time-0-day.dart
Created March 11, 2022 12:12
Dart 0-day DateTime
void main() {
print(DateTime(2000, 1, 0)); // previous day
print(DateTime(2000, 1, 1)); // proper day
}
@albertms10
albertms10 / penney.py
Last active February 18, 2021 10:06
Implementation of the winning probabilities in Penney’s game
import collections
import itertools
import operator
import random
import sys
from enum import Enum
class Outcomes(Enum):
H = 0