0b28b15e39d012019432d5178bd530a7dd1fabac6d7501fa0934d92ed294c932591a244f3866dc8866a6d2f643fd88f37cf38cdac088a27ae41ebb2143f449 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. type Splitter = "-" | "_" | "/" | ".";
  2. type FirstOfString<S extends string> = S extends `${infer F}${string}` ? F : never;
  3. type RemoveFirstOfString<S extends string> = S extends `${string}${infer R}` ? R : never;
  4. type IsUpper<S extends string> = S extends Uppercase<S> ? true : false;
  5. type IsLower<S extends string> = S extends Lowercase<S> ? true : false;
  6. type SameLetterCase<X extends string, Y extends string> = IsUpper<X> extends IsUpper<Y> ? true : IsLower<X> extends IsLower<Y> ? true : false;
  7. type CapitalizedWords<T extends readonly string[], Accumulator extends string = "", Normalize extends boolean | undefined = false> = T extends readonly [infer F extends string, ...infer R extends string[]] ? CapitalizedWords<R, `${Accumulator}${Capitalize<Normalize extends true ? Lowercase<F> : F>}`, Normalize> : Accumulator;
  8. type JoinLowercaseWords<T extends readonly string[], Joiner extends string, Accumulator extends string = ""> = T extends readonly [infer F extends string, ...infer R extends string[]] ? Accumulator extends "" ? JoinLowercaseWords<R, Joiner, `${Accumulator}${Lowercase<F>}`> : JoinLowercaseWords<R, Joiner, `${Accumulator}${Joiner}${Lowercase<F>}`> : Accumulator;
  9. type LastOfArray<T extends any[]> = T extends [...any, infer R] ? R : never;
  10. type RemoveLastOfArray<T extends any[]> = T extends [...infer F, any] ? F : never;
  11. type CaseOptions = {
  12. normalize?: boolean;
  13. };
  14. type SplitByCase<T, Separator extends string = Splitter, Accumulator extends unknown[] = []> = string extends Separator ? string[] : T extends `${infer F}${infer R}` ? [LastOfArray<Accumulator>] extends [never] ? SplitByCase<R, Separator, [F]> : LastOfArray<Accumulator> extends string ? R extends "" ? SplitByCase<R, Separator, [
  15. ...RemoveLastOfArray<Accumulator>,
  16. `${LastOfArray<Accumulator>}${F}`
  17. ]> : SameLetterCase<F, FirstOfString<R>> extends true ? F extends Separator ? FirstOfString<R> extends Separator ? SplitByCase<R, Separator, [...Accumulator, ""]> : IsUpper<FirstOfString<R>> extends true ? SplitByCase<RemoveFirstOfString<R>, Separator, [
  18. ...Accumulator,
  19. FirstOfString<R>
  20. ]> : SplitByCase<R, Separator, [...Accumulator, ""]> : SplitByCase<R, Separator, [
  21. ...RemoveLastOfArray<Accumulator>,
  22. `${LastOfArray<Accumulator>}${F}`
  23. ]> : IsLower<F> extends true ? SplitByCase<RemoveFirstOfString<R>, Separator, [
  24. ...RemoveLastOfArray<Accumulator>,
  25. `${LastOfArray<Accumulator>}${F}`,
  26. FirstOfString<R>
  27. ]> : SplitByCase<R, Separator, [...Accumulator, F]> : never : Accumulator extends [] ? T extends "" ? [] : string[] : Accumulator;
  28. type JoinByCase<T, Joiner extends string> = string extends T ? string : string[] extends T ? string : T extends string ? SplitByCase<T> extends readonly string[] ? JoinLowercaseWords<SplitByCase<T>, Joiner> : never : T extends readonly string[] ? JoinLowercaseWords<T, Joiner> : never;
  29. type PascalCase<T, Normalize extends boolean | undefined = false> = string extends T ? string : string[] extends T ? string : T extends string ? SplitByCase<T> extends readonly string[] ? CapitalizedWords<SplitByCase<T>, "", Normalize> : never : T extends readonly string[] ? CapitalizedWords<T, "", Normalize> : never;
  30. type CamelCase<T, Normalize extends boolean | undefined = false> = string extends T ? string : string[] extends T ? string : Uncapitalize<PascalCase<T, Normalize>>;
  31. type KebabCase<T extends string | readonly string[], Joiner extends string = "-"> = JoinByCase<T, Joiner>;
  32. type SnakeCase<T extends string | readonly string[]> = JoinByCase<T, "_">;
  33. type TrainCase<T, Normalize extends boolean | undefined = false, Joiner extends string = "-"> = string extends T ? string : string[] extends T ? string : T extends string ? SplitByCase<T> extends readonly string[] ? CapitalizedWords<SplitByCase<T>, Joiner> : never : T extends readonly string[] ? CapitalizedWords<T, Joiner, Normalize> : never;
  34. type FlatCase<T extends string | readonly string[], Joiner extends string = ""> = JoinByCase<T, Joiner>;
  35. declare function isUppercase(char?: string): boolean | undefined;
  36. declare function splitByCase<T extends string>(str: T): SplitByCase<T>;
  37. declare function splitByCase<T extends string, Separator extends readonly string[]>(str: T, separators: Separator): SplitByCase<T, Separator[number]>;
  38. declare function upperFirst<S extends string>(str: S): Capitalize<S>;
  39. declare function lowerFirst<S extends string>(str: S): Uncapitalize<S>;
  40. declare function pascalCase(): "";
  41. declare function pascalCase<T extends string | readonly string[], UserCaseOptions extends CaseOptions = CaseOptions>(str: T, opts?: CaseOptions): PascalCase<T, UserCaseOptions["normalize"]>;
  42. declare function camelCase(): "";
  43. declare function camelCase<T extends string | readonly string[], UserCaseOptions extends CaseOptions = CaseOptions>(str: T, opts?: UserCaseOptions): CamelCase<T, UserCaseOptions["normalize"]>;
  44. declare function kebabCase(): "";
  45. declare function kebabCase<T extends string | readonly string[]>(str: T): KebabCase<T>;
  46. declare function kebabCase<T extends string | readonly string[], Joiner extends string>(str: T, joiner: Joiner): KebabCase<T, Joiner>;
  47. declare function snakeCase(): "";
  48. declare function snakeCase<T extends string | readonly string[]>(str: T): SnakeCase<T>;
  49. declare function flatCase(): "";
  50. declare function flatCase<T extends string | readonly string[]>(str: T): FlatCase<T>;
  51. declare function trainCase(): "";
  52. declare function trainCase<T extends string | readonly string[], UserCaseOptions extends CaseOptions = CaseOptions>(str: T, opts?: UserCaseOptions): TrainCase<T, UserCaseOptions["normalize"]>;
  53. declare function titleCase(): "";
  54. declare function titleCase<T extends string | readonly string[], UserCaseOptions extends CaseOptions = CaseOptions>(str: T, opts?: UserCaseOptions): TrainCase<T, UserCaseOptions["normalize"], " ">;
  55. export { type CamelCase, type CaseOptions, type FlatCase, type JoinByCase, type KebabCase, type PascalCase, type SnakeCase, type SplitByCase, type TrainCase, camelCase, flatCase, isUppercase, kebabCase, lowerFirst, pascalCase, snakeCase, splitByCase, titleCase, trainCase, upperFirst };