-
-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
9f4817e
commit ebb5a2d
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {ScreamingSnakeCase} from '../ts41/screaming-snake-case'; | ||
import {expectType} from 'tsd'; | ||
|
||
const screamingSnakeFromCamel: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR'; | ||
expectType<'FOO_BAR'>(screamingSnakeFromCamel); | ||
|
||
const screamingSnakeFromKebab: ScreamingSnakeCase<'foo-bar'> = 'FOO_BAR'; | ||
expectType<'FOO_BAR'>(screamingSnakeFromKebab); | ||
|
||
const screamingSnakeFromSpace: ScreamingSnakeCase<'foo bar'> = 'FOO_BAR'; | ||
expectType<'FOO_BAR'>(screamingSnakeFromSpace); | ||
|
||
const screamingSnakeFromSnake: ScreamingSnakeCase<'foo_bar'> = 'FOO_BAR'; | ||
expectType<'FOO_BAR'>(screamingSnakeFromSnake); | ||
|
||
const screamingSnakeFromScreamingSnake: ScreamingSnakeCase<'FOO_BAR'> = 'FOO_BAR'; | ||
expectType<'FOO_BAR'>(screamingSnakeFromScreamingSnake); | ||
|
||
const noScreamingSnakeFromMono: ScreamingSnakeCase<'foobar'> = 'FOOBAR'; | ||
expectType<'FOOBAR'>(noScreamingSnakeFromMono); | ||
|
||
const nonStringFromNonString: ScreamingSnakeCase<[]> = []; | ||
expectType<[]>(nonStringFromNonString); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |