Skip to content

Commit

Permalink
feat(WithPrefix): Add WithPrefix (#38)
Browse files Browse the repository at this point in the history
* feat(WithPrefix): Add WithPrefix type

* test(WithPrefix): Add WithPrefix type test

* docs(WithPrefix): Add WithPrefix type documentation
  • Loading branch information
haejunejung authored Aug 25, 2024
1 parent d7f5419 commit d77873f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default defineConfig({
{ text: 'Replace', link: '/reference/string/Replace' },
{ text: 'ReplaceAll', link: '/reference/string/ReplaceAll' },
{ text: 'Split', link: '/reference/string/Split' },
{ text: 'WithPrefix', link: '/reference/string/WithPrefix' },
],
},
{
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default defineConfig({
{ text: 'Replace', link: '/ko/reference/string/Replace' },
{ text: 'ReplaceAll', link: '/ko/reference/string/ReplaceAll' },
{ text: 'Split', link: '/ko/reference/string/Split' },
{ text: 'WithPrefix', link: '/ko/reference/string/WithPrefix' },
],
},
{
Expand Down
28 changes: 28 additions & 0 deletions docs/ko/reference/string/WithPrefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# WithPrefix\<BaseString, Prefix>;

## 개요

`WithPrefix` 타입은 기존 문자열 타입 앞에 지정된 접두사를 추가하여 새로운 문자열 타입을 생성해요. 이를 통해 문자열 타입에 일관된 접두사를 자동으로 추가할 수 있어요.

## 문법

```ts
type WithPrefix<
BaseString extends string,
Prefix extends string,
> = `${Prefix}${BaseString}`;
```

- **BaseString**: 접두사를 추가할 기존 문자열 타입이에요.
- **Prefix**: 추가될 접두사예요.

## 예제

#### 예제 #1

```ts
type FormEventNames = 'submit' | 'reset' | 'change';
type CapitalizedFormEventNames = Capitalize<FormEventNames>;
type FormEventHandlers = WithPrefix<CapitalizedFormEventNames, 'on'>;
// Result: "onSubmit" | "onReset" | "onChange";
```
28 changes: 28 additions & 0 deletions docs/reference/string/WithPrefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# WithPrefix\<BaseString, Prefix>;

## Overview

The `WithPrefix` type creates a new string type by prepending a specified prefix to an existing string type. This enables automatic addition of a consistent prefix to string types.

## Syntax

```ts
type WithPrefix<
BaseString extends string,
Prefix extends string,
> = `${Prefix}${BaseString}`;
```

- **BaseString**: The base string type to which the prefix will be added.
- **Prefix**: The prefix to be added.

## Examples

#### Example #1

```ts
type FormEventNames = 'submit' | 'reset' | 'change';
type CapitalizedFormEventNames = Capitalize<FormEventNames>;
type FormEventHandlers = WithPrefix<CapitalizedFormEventNames, 'on'>;
// Result: "onSubmit" | "onReset" | "onChange";
```
22 changes: 22 additions & 0 deletions source/string/WithPrefix.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @description
* The `WithPrefix` type creates a new string type by prepending a specified prefix to an existing
* string type. This enables automatic addition of a consistent prefix to string types.
*
* @template {string} BaseString - The base string type to which the prefix will be added.
* @template {string} Prefix - The prefix to be added.
*
* @returns
* A new string type with the specified prefix prepended.
*
* @example
* type FormEventNames = "submit" | "reset" | "change";
* type CapitalizedFormEventNames = Capitalize<FormEventNames>;
* type FormHandlerNames = WithPrefix<CapitalizedFormEventNames, "on">
* // Result: "onSubmit" | "onReset" | "onChange"
*/

export type WithPrefix<
BaseString extends string,
Prefix extends string,
> = `${Prefix}${BaseString}`;
1 change: 1 addition & 0 deletions source/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type { Replace } from './Replace';
export type { ReplaceAll } from './ReplaceAll';
export type { Split } from './Split';
export type { WithPrefix } from './WithPrefix';
16 changes: 16 additions & 0 deletions test-d/string/WithPrefix.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { WithPrefix } from '@/string';
import { expectType } from 'tsd';

declare function withPrefix<
S extends string,
Prefix extends string,
>(): WithPrefix<S, Prefix>;

expectType<'onSubmit'>(withPrefix<'Submit', 'on'>());

type FormEventNames = 'submit' | 'reset' | 'change';
type CapitalizedFormEventNames = Capitalize<FormEventNames>;

expectType<'onSubmit' | 'onReset' | 'onChange'>(
withPrefix<CapitalizedFormEventNames, 'on'>()
);

0 comments on commit d77873f

Please sign in to comment.