Skip to content

Instantly share code, notes, and snippets.

View hugoaboud's full-sized avatar

Hugo Aboud hugoaboud

  • Nesoi
  • Pindamonhangaba, SP
View GitHub Profile
@hugoaboud
hugoaboud / union_to_intersection.ts
Created June 17, 2025 23:01
TypeScript: Union to Intersection
/**
* An efficient TypeScript utility type to transform a union of objects into the intersection of such objects.
*
* - TypeScript 5.8.2
* - Benchmarked against the similar type declared at [utility-types](https://github.com/piotrwitek/utility-types):
* - `utility-types`: ~250ms
* - `this`: ~30ms
*
* - This haven't been deeply tested for correctness yet.
*/
@hugoaboud
hugoaboud / TypeFlatten.ts
Created March 31, 2023 00:54
Generic Type that recursively flattens a Type by merging property names with dots
type DeepKeys<M> = {
[J in keyof M as `${J & string}${
M[J] extends Record<string, any> ?
M[J] extends {constructor: M[J]} ? '' :
M[J] extends Array<any> ? '' :
`.${keyof DeepKeys<M[J]> & string}` : ''
}`]:
M[J] extends Record<string, any> ? M : M[J]
}
@hugoaboud
hugoaboud / dirtree.py
Last active February 19, 2023 02:32
Build a directory tree, including files, for a given path
import os
def dirtree(path):
steps = list(os.walk(path))
def _tree():
_, child_dirs, files = steps.pop(0)
obj = { child: _tree() for child in child_dirs }
obj['__files__'] = files
return obj
return _tree()