typedash
Version:
modern, type-safe collection of utility functions
49 lines (48 loc) • 1.85 kB
text/typescript
import { SnakeCase } from "type-fest";
//#region src/functions/snakeCase/snakeCase.d.ts
/**
* Changes the casing of a string to snake case.
* @param string The input string to change the casing of.
* @returns A new string with the casing changed to snake case.
* @example
* ```ts
* snakeCase('fooBar') // 'foo_bar'
* snakeCase('foo bar') // 'foo_bar'
* snakeCase('foo-bar') // 'foo_bar'
* snakeCase('fooBar42') // 'foo_bar42'
* ```
*/
declare function snakeCase<S extends string>(string: S): SnakeCase$1<S>;
/**
* Changes the casing of a string to snake case.
* @see {@link snakeCase}.
*/
type SnakeCase$1<S extends string> = TrimUnderscores<ReduceUnderscores<SnakeCase<S>>>;
/**
* Reduces multiple underscores to a single underscore.
* @param S The input string.
* @returns A new string with multiple underscores reduced to a single underscore.
* @example
* ```ts
* ReduceUnderscores<'foo__bar'> // 'foo_bar'
* ReduceUnderscores<'foo_bar'> // 'foo_bar'
* ReduceUnderscores<'foo___bar'> // 'foo_bar'
* ```
*/
type ReduceUnderscores<S extends string> = S extends `${infer L}__${infer R}` ? ReduceUnderscores<`${L}_${R}`> : S extends `${infer L}${infer D}__${infer R}` ? ReduceUnderscores<`${L}${D}_${R}`> : S;
/**
* Trims underscores from the start and end of a string.
* @param S The input string.
* @returns A new string with underscores trimmed from the start and end.
* @example
* ```ts
* TrimUnderscores<'_foo_bar_'> // 'foo_bar'
* TrimUnderscores<'foo_bar_'> // 'foo_bar'
* TrimUnderscores<'_foo_bar'> // 'foo_bar'
* TrimUnderscores<'foo_bar'> // 'foo_bar'
* ```
*/
type TrimUnderscores<S extends string> = S extends `_${infer R}` ? TrimUnderscores<R> : S extends `${infer L}_` ? TrimUnderscores<L> : S;
//#endregion
export { snakeCase as n, SnakeCase$1 as t };
//# sourceMappingURL=snakeCase-CTKD7DMh.d.cts.map