bananas-commerce
Version:
A client for bananas-commerce with support for TypeScript
52 lines (51 loc) • 2.46 kB
TypeScript
/** Converts a {@link string} to the specified {@link casing}. */
export declare const casedString: StringGeneralCaserFunction;
export interface StringGeneralCaserFunction {
/**
* Converts a string to a {@link CaseType}.
*
* @param string - The string to convert the casing of.
* @param casing - The {@link CaseType}, i.e. `snake` or `camel`.
*/
<S extends string, T extends CaseType>(string: S, casing: T): CasedString<S, T>;
}
export interface StringCaserFunction<T extends CaseType> {
/**
* Converts a string to a different casing.
*
* @param string - The string to convert the casing of.
*/
<S extends string, T extends CaseType>(string: S): CasedString<S, T>;
}
export type StringCaser = StringGeneralCaserFunction & {
[T in CaseType]: StringCaserFunction<T>;
};
/**
* `type-fest` has better types for cased strings, but they are not exported to Deno at the moment.
*/
/**
* Converts a string to a {@link CaseType}.
*
* @typeparam `S` - The string to convert the casing of.
* @typeparam `T` - The {@link CaseType}, i.e. `snake` or `camel`.
*/
export type CasedString<S extends string, T extends CaseType> = T extends "camel" ? CamelCasedString<S> : T extends "snake" ? SnakeCasedString<S> : S;
/**
* Converts a string to camelCase.
*
* @remarks
* Only tested for snake_case.
*/
export type CamelCasedString<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}` ? P2 extends Alphabetic ? `${Lowercase<P1>}${Uppercase<P2>}${CamelCasedString<P3>}` : `${Lowercase<P1>}_${P2}${CamelCasedString<P3>}` : Lowercase<S>;
/**
* Converts a string to snake_case.
*
* @remarks
* Does not work with following uppercase characters. Only tested for camelCase.
*/
export type SnakeCasedString<S extends string> = S extends `${infer P1}${infer P2}` ? P1 extends UppercaseAlphabetic ? `_${Lowercase<P1>}${SnakeCasedString<P2>}` : P2 extends UppercaseAlphabetic ? `${Lowercase<P1>}_${SnakeCasedString<P2>}` : `${P1}${SnakeCasedString<P2>}` : Lowercase<S>;
/** The possibilities of casing using the {@link CasedString} type. */
export type CaseType = "snake" | "camel";
export type LowercaseAlphabetic = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "x" | "y" | "z";
export type UppercaseAlphabetic = Uppercase<LowercaseAlphabetic>;
export type Alphabetic = LowercaseAlphabetic | UppercaseAlphabetic;