import type {TupleOf} from '../tuple-of.d.ts'; import type {Trim} from '../trim.d.ts'; import type {StringLength} from '../string-length.d.ts'; import type {Whitespace} from './characters.d.ts'; /** Return a string representation of the given string or number. Note: This type is not the return type of the `.toString()` function. */ export type ToString = T extends string | number ? `${T}` : never; /** Returns a boolean for whether the given string `S` starts with the given string `SearchString`. @example ``` type A = StartsWith<'abcde', 'abc'>; //=> true type B = StartsWith<'abcde', 'bc'>; //=> false type C = StartsWith; //=> never type D = StartsWith<'abcde', string>; //=> never ``` @category String @category Template literal */ export type StartsWith = string extends S | SearchString ? never : S extends `${SearchString}${infer T}` ? true : false; /** Returns a boolean for whether a string is whitespace. */ export type IsWhitespace = T extends Whitespace ? true : T extends `${Whitespace}${infer Rest}` ? IsWhitespace : false; /** Returns a boolean for whether the string is numeric. This type is a workaround for [Microsoft/TypeScript#46109](https://github.com/microsoft/TypeScript/issues/46109#issuecomment-930307987). */ export type IsNumeric = T extends `${number}` ? Trim extends T ? true : false : false; /** Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both numeric strings and have the same length. @example ``` type A = SameLengthPositiveNumericStringGt<'50', '10'>; //=> true type B = SameLengthPositiveNumericStringGt<'10', '10'>; //=> false ``` */ type SameLengthPositiveNumericStringGt = A extends `${infer FirstA}${infer RestA}` ? B extends `${infer FirstB}${infer RestB}` ? FirstA extends FirstB ? SameLengthPositiveNumericStringGt : PositiveNumericCharacterGt : never : false; type NumericString = '0123456789'; /** Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings. @example ``` type A = PositiveNumericStringGt<'500', '1'>; //=> true type B = PositiveNumericStringGt<'1', '1'>; //=> false type C = PositiveNumericStringGt<'1', '500'>; //=> false ``` */ export type PositiveNumericStringGt = A extends B ? false : [TupleOf, 0>, TupleOf, 0>] extends infer R extends [readonly unknown[], readonly unknown[]] ? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]] ? 0 extends Remain['length'] ? SameLengthPositiveNumericStringGt : true : false : never; /** Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters. @example ``` type A = PositiveNumericCharacterGt<'5', '1'>; //=> true type B = PositiveNumericCharacterGt<'1', '1'>; //=> false ``` */ type PositiveNumericCharacterGt = NumericString extends `${infer HeadA}${A}${infer TailA}` ? NumericString extends `${infer HeadB}${B}${infer TailB}` ? HeadA extends `${HeadB}${infer _}${infer __}` ? true : false : never : never; export {};