Skip to content

Instantly share code, notes, and snippets.

type FIBONACCI<
C extends Byte,
A extends Byte = ONE_BYTE,
B extends Byte = ONE_BYTE
> = ONE_BYTE extends C
? A
: AddOne<ONE_BYTE> extends C
? A
: FIBONACCI<SubtractOne<C>, Add<A, B>, A>;
type LTPartial<LHS extends Bit[], RHS extends Bit[]> = LHS extends [infer LHSU extends Bit, ...infer LHSR extends Bit[]]
? RHS extends [infer RHSU extends Bit, ...infer RHSR extends Bit[]]
? true extends BitLTB<LHSU, RHSU>
? true
: false extends BitLTB<LHSU, RHSU>
? false
: "undecided" extends BitLTB<LHSU, RHSU>
? LTPartial<LHSR, RHSR>
: false
: false
type PartialAdd<
LHS extends Bit[],
RHS extends Bit[],
CF extends CarryFlag = Zero
> = LHS extends [...infer LHSU extends Bit[], infer LHSR extends Bit]
? RHS extends [...infer RHSU extends Bit[], infer RHSR extends Bit]
? [
...PartialAdd<LHSU, RHSU, AddBitsWithCarry<LHSR, RHSR, CF>["cf"]>,
AddBitsWithCarry<LHSR, RHSR, CF>["value"]
]
type PartialAddOne<T extends Bit[]> = T extends [
...infer U extends Bit[],
infer R extends Bit
]
? R extends One
? [...PartialAddOne<U>, Zero]
: [...U, One]
: [];
type AddOne<T extends Byte> = T extends [
...infer U extends Bit[],
type PartialByteXOR<LHS extends Bit[], RHS extends Bit[]> = LHS extends [
infer LHSU extends Bit,
...infer LHSR extends Bit[]
]
? RHS extends [infer RHSU extends Bit, ...infer RHSR extends Bit[]]
? [BitXor<LHSU, RHSU>, ...PartialByteXOR<LHSR, RHSR>]
: []
: [];
type ByteXOR<LHS extends Byte, RHS extends Byte> = LHS extends [
infer LHSU extends Bit,
type PartialByteNot<T extends Bit[]> = T extends [
infer U extends Bit,
...infer R extends Bit[]
]
? [BitNot<U>, ...PartialByteNot<R>]
: [];
type ByteNot<T extends Byte> = T extends [
infer U extends Bit,
...infer R extends Bit[]
]
type ShiftLeft<T extends Byte> = T extends [
infer U extends Bit,
...infer R extends Bit[]
]
? [...R, Zero]
: never;
type ShiftRight<T extends Byte> = T extends [
...infer U extends Bit[],
infer R extends Bit
]
type BitNot<T extends Bit> = T extends Zero ? One : Zero;
type BitAnd<LHS extends Bit, RHS extends Bit> = LHS extends One
? RHS extends One
? One
: Zero
: Zero;
type BitOr<LHS extends Bit, RHS extends Bit> = LHS extends Zero
? RHS extends Zero
? Zero
: One
type Zero = {
__type: 'zero',
}
type One = {
__type: 'one'
}
type Bit = Zero | One;
@hades2510
hades2510 / limit_file_size_upload.ts
Created March 28, 2021 11:45
Limit Google Storage file upload
import { GetSignedUrlConfig, Storage } from '@google-cloud/storage'
const storage = new Storage()
// this comes from the front end
const input = {
contentType: 'application/jpeg',
contentLength: 10000 // size in bytes
}