-
-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 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
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,43 @@ | ||
import type {IsNegative} from './numeric'; | ||
import type {Subtract} from './subtract'; | ||
|
||
/** | ||
Returns a new string which contains the specified number of copies of a given string, just like `String#repeat()`. | ||
@example | ||
``` | ||
import {StringRepeat} from 'type-fest'; | ||
declare function stringRepeat< | ||
Input extends string, | ||
Count extends number | ||
>(input: Input, count: Count): StringRepeat<Input, Count>; | ||
// The return type is the exact string literal, not just `string`. | ||
stringRepeat('foo', 2); | ||
//=> 'foofoo' | ||
stringRepeat('=', 3); | ||
//=> '===' | ||
``` | ||
@category String | ||
@category Template literal | ||
*/ | ||
export type StringRepeat< | ||
Input extends string, | ||
Count extends number, | ||
> = number extends Count | ||
? Input extends '' | ||
? '' | ||
: string | ||
: IsNegative<Count> extends true | ||
? never | ||
: Count extends 0 | ||
? '' | ||
: string extends Input | ||
? string | ||
: StringRepeat<Input, Subtract<Count, 1>> extends infer R extends string | ||
? `${Input}${R}` | ||
: never; |
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,19 @@ | ||
import {expectType} from 'tsd'; | ||
import type {StringRepeat} from '../index'; | ||
|
||
declare const unknown: unknown; | ||
|
||
expectType<StringRepeat<'', 0>>(''); | ||
expectType<StringRepeat<'', -1>>(unknown as never); | ||
expectType<StringRepeat<string, 0>>(''); | ||
expectType<StringRepeat<string, -1>>(unknown as never); | ||
expectType<StringRepeat<'', number>>(''); | ||
expectType<StringRepeat<string, number>>(unknown as string); | ||
expectType<StringRepeat<'0', number>>(unknown as string); | ||
expectType<StringRepeat<'0', -1>>(unknown as never); | ||
expectType<StringRepeat<'0', 0>>(''); | ||
expectType<StringRepeat<'0', 1>>('0'); | ||
expectType<StringRepeat<'0', 5>>('00000'); | ||
expectType<StringRepeat<'012345-', 0>>(''); | ||
expectType<StringRepeat<'012345-', 1>>('012345-'); | ||
expectType<StringRepeat<'012345-', 5>>('012345-012345-012345-012345-012345-'); |