forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screaming-snake-case.d.ts
40 lines (33 loc) · 1.04 KB
/
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
34
35
36
37
38
39
40
import {SplitIncludingDelimiters} from './delimiter-case';
import {SnakeCase} from './snake-case';
/**
Returns a boolean for whether the given array includes the given item.
*/
type Includes<Value extends any[], Item> = {
[P in keyof Value & number as Value[P]]: true;
}[Item] extends true
? true
: false;
/**
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 {ScreamingSnakeCase} from 'type-fest';
const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
```
@category Template Literals
*/
export type ScreamingSnakeCase<Value> = Value extends string
? IsScreamingSnakeCase<Value> extends true
? Value
: Uppercase<SnakeCase<Value>>
: Value;