Skip to content

Instantly share code, notes, and snippets.

View ljbc1994's full-sized avatar
🎯
Focusing

Louie Colgan ljbc1994

🎯
Focusing
View GitHub Profile
@ljbc1994
ljbc1994 / Spacing.js
Last active October 28, 2018 14:54
Styled Component Spacing Props Generation
import React from "react";
import styled, { css } from "styled-components";
const directions = {
T: "top",
L: "left",
R: "right",
B: "bottom"
};
@ljbc1994
ljbc1994 / RandomTypes.ts
Last active May 7, 2022 15:37
Random Types!
type CensorWithChar<TPhrase extends string, TChar extends string, TResult extends string = ''> =
TPhrase extends `${infer Head}${infer Tail}`
? CensorWithChar<Tail, TChar, `${TResult}${Head extends TChar ? '*' : Head}`>
: TResult
type CensorWithCharTest = CensorWithChar<'my name is mary celeste', 'm' | 'c'>
// type CensorWithCharTest = "*y na*e is *ary *eleste"
type Reverse<TPhrase extends string, TResult extends string = ''> =
TPhrase extends `${infer Head}${infer Tail}`
@ljbc1994
ljbc1994 / Spongebobify.ts
Created May 7, 2022 15:37
If you need a spongebob type
type CharMap<T extends string = 'aA-bB-cC-dD-eE-fF-gG-hH-iI-jJ-kK-lL-mM-nN-oO-pP-qQ-rR-sS-tT-uU-vV-wW-xX-yY-zZ'> =
T extends `${infer Lower}-${infer Upper}`
? Lower | CharMap<Upper>
: T
type CharSplit<T extends string> = T extends `${infer Lower}${infer Upper}` ? [Lower, Upper] : T
type CharObj = { [T in CharMap as CharSplit<T>[0]]: CharSplit<T>[1] };
type GetUpperCase<TChar extends keyof CharObj> = CharObj[TChar]
type Spongebobify<TPhrase extends string, TResult extends string = '', TFlip = 0> =
TPhrase extends `${infer Head}${infer Tail}`
? Head extends keyof CharObj
@ljbc1994
ljbc1994 / Arithmetic.ts
Last active May 7, 2022 19:39
Playing about with arithmetic
type Count<A, S extends 0[] = []> = A extends S['length'] ? S : Count<A, [...S, 0]>
type Increment<A> = [...Count<A>, 0]['length']
type Decrement<A> = Count<A> extends [infer A, ...infer Rest] ? Rest['length'] : A
type Add<A, B> = [...Count<A>, ...Count<B>]['length']
type Multiply<A, B, S = 0> = B extends 0 ? S : Multiply<A, Decrement<B>, Add<S, A>>
type Power<A, B, S = A> = B extends 1 ? S : Power<A, Decrement<B>, Multiply<S, A>>
type Square<A> = Power<A, 2>
type Cube<A> = Power<A, 3>
type Incremented = Increment<10>
@ljbc1994
ljbc1994 / crazycards.ts
Last active June 30, 2022 06:42
Crazy cards application with types 👀
// ====
type Count<A, S extends 0[] = []> = A extends S['length'] ? S : Count<A, [...S, 0]>
type Add<A, B> = [...Count<A>, ...Count<B>]['length']
type Min<A, B, C extends 0[] = []> = C['length'] extends A
? B
: C['length'] extends B
? A
: Min<A, B, [0, ...C]>
// ====
@ljbc1994
ljbc1994 / day-1.ts
Created December 4, 2023 17:21
Advent of Code 2023 - Day 1 (Part A)
type NumberArr = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9 }
type Count<A, S extends 0[] = []> = A extends S['length'] ? S : Count<A, [...S, 0]>
type Add<A, B> = [...Count<A>, ...Count<B>]['length']
type ParseInt<T extends string>
= T extends `${infer N extends number}` ? N : never;
type Combine<A extends number, B extends number> =
ParseInt<`${A}${B}`>