UNPKG

@jahands/dagger-helpers

Version:

dagger.io helpers for my own projects - not meant for public use

53 lines 3.07 kB
type IsLower<C extends string> = Lowercase<C> extends Uppercase<C> ? false : C extends Lowercase<C> ? true : false; type IsUpper<C extends string> = Lowercase<C> extends Uppercase<C> ? false : C extends Uppercase<C> ? true : false; type IsDigit<C extends string> = C extends `${number}` ? true : false; type CamelToSnakeInner<S extends string, PrevChar extends string = ''> = S extends '' ? '' : S extends `${infer First}${infer Rest}` ? IsUpper<First> extends true ? IsLower<PrevChar> extends true ? `_${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : IsDigit<PrevChar> extends true ? `_${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : Rest extends `${infer NextChar}${string}` ? IsLower<NextChar> extends true ? `_${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : `${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : `${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : IsDigit<First> extends true ? IsLower<PrevChar> extends true ? `_${First}${CamelToSnakeInner<Rest, First>}` : `${First}${CamelToSnakeInner<Rest, First>}` : `${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : ''; /** * Type helper to convert a camelCase/PascalCase string literal type * to an UPPER_SNAKE_CASE string literal type. Handles common cases * including transitions from lower to upper, letters to digits, and basic acronyms. * * Examples: * 'userId' -> 'USER_ID' * 'userName' -> 'USER_NAME' * 'isActive' -> 'IS_ACTIVE' * 'APIKey' -> 'API_KEY' * 'version10' -> 'VERSION_10' * 'apiV2Client' -> 'API_V2_CLIENT' */ type CamelToSnakeCase<S extends string> = S extends `${infer First}${infer Rest}` ? `${Uppercase<First>}${CamelToSnakeInner<Rest, First>}` : Uppercase<S>; /** * Converts a camelCase or PascalCase string to UPPER_SNAKE_CASE. * Handles acronyms and numbers within the string. * * **Examples:** * ``` * 'helloWorld' -> 'HELLO_WORLD' * 'HelloWorld' -> 'HELLO_WORLD' * 'someAPIKey' -> 'SOME_API_KEY' * 'getHttpResponseCode' -> 'GET_HTTP_RESPONSE_CODE' * 'version10' -> 'VERSION_10' // Fixed * 'version10Alpha' -> 'VERSION_10_ALPHA' // Fixed * 'apiV2Client' -> 'API_V2_CLIENT' * 'releaseV10' -> 'RELEASE_V10' * 'version2Data' -> 'VERSION_2_DATA' // Fixed * 'word' -> 'WORD' *``` * @param str The input string in camelCase or PascalCase format. * @returns The converted string in UPPER_SNAKE_CASE format. */ export declare function camelToSnake(str: string): string; /** * Converts an object's keys from camelCase/PascalCase to UPPER_SNAKE_CASE. * Provides compile-time type safety for the converted keys based on common patterns. * * @template T The type of the input object. * @param input The object with camelCase/PascalCase keys. * @returns A new object with the same values but UPPER_SNAKE_CASE keys, * with an inferred type reflecting the key transformation. */ export declare function convertToSnake<T extends Record<string, any>>(input: T): { [K in keyof T as CamelToSnakeCase<K & string>]: T[K]; }; export {}; //# sourceMappingURL=camel-to-snake.d.ts.map