UNPKG

@cuppachino/type-space

Version:

A collection of type utilities for TypeScript.

11 lines (10 loc) 893 B
import type { NumberLiteral } from '../number-literal'; import type { Stringify } from '../stringify'; export type TrimNumberLiteral<N extends number | NumberLiteral> = Stringify<N> extends `${0}` ? '0' : PositiveRational<NoDecimalWithoutLeadingZero<Stringify<N>>>; type PositiveRational<N> = HandleLeadingZeroIntegral<IfWhole<N, Whole<N>, Fractional<N>>>; type HandleLeadingZeroIntegral<N> = Stringify<N> extends `${0}${infer _R extends 0}${infer R2}` ? R2 : N extends '' ? '0' : N; type Whole<N> = N extends `0${infer N2}` ? Whole<`${N2}`> : N; type Fractional<N> = N extends `${infer I}.` ? `${I}` : N extends `${infer I}.${infer F}` ? `${F}` extends `${infer F2}0` ? Fractional<`${I}.${F2}`> : N : never; type NoDecimalWithoutLeadingZero<N> = N extends `.${infer F}` ? `0.${F}` : N; type IfWhole<N, Then, Else> = `${N & string}` extends `${infer _I}.${infer _F}` ? Else : Then; export {};