UNPKG

935 BTypeScriptView Raw
1import type {SplitIncludingDelimiters} from './delimiter-case';
2import type {SnakeCase} from './snake-case';
3import type {Includes} from './includes';
4
5/**
6Returns a boolean for whether the string is screaming snake case.
7*/
8type IsScreamingSnakeCase<Value extends string> = Value extends Uppercase<Value>
9 ? Includes<SplitIncludingDelimiters<Lowercase<Value>, '_'>, '_'> extends true
10 ? true
11 : false
12 : false;
13
14/**
15Convert a string literal to screaming-snake-case.
16
17This can be useful when, for example, converting a camel-cased object property to a screaming-snake-cased SQL column name.
18
19@example
20```
21import type {ScreamingSnakeCase} from 'type-fest';
22
23const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
24```
25
26@category Change case
27@category Template literal
28*/
29export type ScreamingSnakeCase<Value> = Value extends string
30 ? IsScreamingSnakeCase<Value> extends true
31 ? Value
32 : Uppercase<SnakeCase<Value>>
33 : Value;