UNPKG

typedash

Version:

modern, type-safe collection of utility functions

48 lines (45 loc) 1.64 kB
import { KebabCase as KebabCase$1 } from 'type-fest'; /** * Changes the casing of a string to kebab case. * @param string The input string to change the casing of. * @returns A new string with the casing changed to kebab case. * @example * ```ts * kebabCase('fooBar') // 'foo-bar' * kebabCase('foo bar') // 'foo-bar' * kebabCase('foo-bar') // 'foo-bar' * kebabCase('fooBar42') // 'foo-bar42' * ``` */ declare function kebabCase<S extends string>(string: S): KebabCase<S>; /** * Changes the casing of a string to kebab case. * @see {@link kebabCase}. */ type KebabCase<S extends string> = TrimDashes<ReduceDashes<KebabCase$1<S>>>; /** * Reduces multiple dashes to a single dash. * @param S The input string. * @returns A new string with multiple dashes reduced to a single dash. * @example * ```ts * ReduceDashes<'foo--bar'> // 'foo-bar' * ReduceDashes<'foo-bar'> // 'foo-bar' * ReduceDashes<'foo---bar'> // 'foo-bar' * ``` */ type ReduceDashes<S extends string> = S extends `${infer L}--${infer R}` ? ReduceDashes<`${L}-${R}`> : S extends `${infer L}${infer D}--${infer R}` ? ReduceDashes<`${L}${D}-${R}`> : S; /** * Trims dashes from the start and end of a string. * @param S The input string. * @returns A new string with dashes trimmed from the start and end. * @example * ```ts * TrimDashes<'-foo-bar-'> // 'foo-bar' * TrimDashes<'foo-bar-'> // 'foo-bar' * TrimDashes<'-foo-bar'> // 'foo-bar' * TrimDashes<'foo-bar'> // 'foo-bar' * ``` */ type TrimDashes<S extends string> = S extends `-${infer R}` ? TrimDashes<R> : S extends `${infer L}-` ? TrimDashes<L> : S; export { type KebabCase, kebabCase };