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