Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/ja/reference/string/dedent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# dedent

複数行の文字列から各行の共通インデントを削除します。

## インターフェース

```typescript
function dedent(str: string): string;
function dedent(str: TemplateStringsArray, ...values: unknown[]): string;
function dedent<T>(
tagFn: (strings: TemplateStringsArray, ...values: unknown[]) => T
): (strings: TemplateStringsArray, ...values: unknown[]) => T;
```

### パラメータ

- `str` (`string | TemplateStringsArray | Function`): インデントを削除する文字列、テンプレートリテラル、またはタグ関数。
- `values` (`unknown[]`): タグ付きテンプレートリテラルとして使用する際に挿入する値。

### 戻り値

(`string | Function`): 共通インデントが削除された文字列、または合成されたタグ関数。

## 例

```typescript
import { dedent } from 'es-toolkit/string';

// 通常の関数として使用
dedent(' hello\n world');
// 結果: 'hello\nworld'

// タグ付きテンプレートリテラルとして使用
dedent`
hello
world
`;
// 結果: 'hello\nworld'

// 相対的なインデントを保持します
dedent`
hello
world
`;
// 結果: 'hello\n world'

// 補間をサポートします
const name = 'world';
dedent`
hello
${name}
`;
// 結果: 'hello\nworld'

// タグ合成
const html = dedent((strings, ...values) => strings.join(''));
html` <div>Hello</div> `;
// 結果: '<div>Hello</div>'
```
59 changes: 59 additions & 0 deletions docs/ko/reference/string/dedent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# dedent

여러 줄 문자열에서 각 줄의 공통 들여쓰기를 제거해요.

## 인터페이스

```typescript
function dedent(str: string): string;
function dedent(str: TemplateStringsArray, ...values: unknown[]): string;
function dedent<T>(
tagFn: (strings: TemplateStringsArray, ...values: unknown[]) => T
): (strings: TemplateStringsArray, ...values: unknown[]) => T;
```

### 파라미터

- `str` (`string | TemplateStringsArray | Function`): 들여쓰기를 제거할 문자열, 템플릿 리터럴, 또는 태그 함수예요.
- `values` (`unknown[]`): 태그 템플릿 리터럴로 사용할 때 삽입할 값이에요.

### 반환 값

(`string | Function`): 공통 들여쓰기가 제거된 문자열, 또는 합성된 태그 함수를 반환해요.

## 예시

```typescript
import { dedent } from 'es-toolkit/string';

// 일반 함수로 사용
dedent(' hello\n world');
// 결과: 'hello\nworld'

// 태그 템플릿 리터럴로 사용
dedent`
hello
world
`;
// 결과: 'hello\nworld'

// 상대적인 들여쓰기를 유지해요
dedent`
hello
world
`;
// 결과: 'hello\n world'

// 보간을 지원해요
const name = 'world';
dedent`
hello
${name}
`;
// 결과: 'hello\nworld'

// 태그 합성
const html = dedent((strings, ...values) => strings.join(''));
html` <div>Hello</div> `;
// 결과: '<div>Hello</div>'
```
59 changes: 59 additions & 0 deletions docs/reference/string/dedent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# dedent

Removes common leading whitespace from each line of a multi-line string.

## Signature

```typescript
function dedent(str: string): string;
function dedent(str: TemplateStringsArray, ...values: unknown[]): string;
function dedent<T>(
tagFn: (strings: TemplateStringsArray, ...values: unknown[]) => T
): (strings: TemplateStringsArray, ...values: unknown[]) => T;
```

### Parameters

- `str` (`string | TemplateStringsArray | Function`): The string, template literal, or tag function to dedent.
- `values` (`unknown[]`): The values to interpolate when used as a tagged template literal.

### Returns

(`string | Function`): The dedented string, or a dedented tag function when composed.

## Examples

```typescript
import { dedent } from 'es-toolkit/string';

// As a regular function
dedent(' hello\n world');
// Returns: 'hello\nworld'

// As a tagged template literal
dedent`
hello
world
`;
// Returns: 'hello\nworld'

// Preserves relative indentation
dedent`
hello
world
`;
// Returns: 'hello\n world'

// Handles interpolations
const name = 'world';
dedent`
hello
${name}
`;
// Returns: 'hello\nworld'

// Tag composition
const html = dedent((strings, ...values) => strings.join(''));
html` <div>Hello</div> `;
// Returns: '<div>Hello</div>'
```
59 changes: 59 additions & 0 deletions docs/zh_hans/reference/string/dedent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# dedent

移除多行字符串中每行的公共前导空白。

## 签名

```typescript
function dedent(str: string): string;
function dedent(str: TemplateStringsArray, ...values: unknown[]): string;
function dedent<T>(
tagFn: (strings: TemplateStringsArray, ...values: unknown[]) => T
): (strings: TemplateStringsArray, ...values: unknown[]) => T;
```

### 参数

- `str` (`string | TemplateStringsArray | Function`): 要移除缩进的字符串、模板字面量或标签函数。
- `values` (`unknown[]`): 作为标签模板字面量使用时要插入的值。

### 返回值

(`string | Function`): 移除公共缩进后的字符串,或合成的标签函数。

## 示例

```typescript
import { dedent } from 'es-toolkit/string';

// 作为普通函数使用
dedent(' hello\n world');
// 结果: 'hello\nworld'

// 作为标签模板字面量使用
dedent`
hello
world
`;
// 结果: 'hello\nworld'

// 保留相对缩进
dedent`
hello
world
`;
// 结果: 'hello\n world'

// 支持插值
const name = 'world';
dedent`
hello
${name}
`;
// 结果: 'hello\nworld'

// 标签合成
const html = dedent((strings, ...values) => strings.join(''));
html` <div>Hello</div> `;
// 结果: '<div>Hello</div>'
```
123 changes: 123 additions & 0 deletions src/string/dedent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { describe, expect, it } from 'vitest';
import { dedent } from './dedent';

describe('dedent', () => {
it('should remove common leading whitespace from each line', () => {
expect(dedent(' hello\n world')).toBe('hello\nworld');
});

it('should preserve relative indentation', () => {
expect(dedent(' hello\n world')).toBe('hello\n world');
});

it('should handle a tagged template literal', () => {
const result = dedent`
hello
world
`;
expect(result).toBe('hello\nworld');
});

it('should preserve relative indentation in a tagged template literal', () => {
const result = dedent`
hello
world
`;
expect(result).toBe('hello\n world');
});

it('should handle interpolations in a tagged template literal', () => {
const name = 'world';
const result = dedent`
hello
${name}
`;
expect(result).toBe('hello\nworld');
});

it('should handle empty lines', () => {
const result = dedent`
hello

world
`;
expect(result).toBe('hello\n\nworld');
});

it('should handle a single line', () => {
expect(dedent(' hello')).toBe('hello');
});

it('should handle an empty string', () => {
expect(dedent('')).toBe('');
});

it('should handle a string with no indentation', () => {
expect(dedent('hello\nworld')).toBe('hello\nworld');
});

it('should handle tab indentation', () => {
expect(dedent('\t\thello\n\t\tworld')).toBe('hello\nworld');
});

it('should remove first and last empty lines', () => {
expect(dedent('\n hello\n world\n')).toBe('hello\nworld');
});

it('should handle lines with only whitespace', () => {
expect(dedent(' hello\n \n world')).toBe('hello\n\nworld');
});

it('should handle mixed indentation levels', () => {
expect(dedent(' a\n b\n c')).toBe(' a\nb\n c');
});

it('should handle Windows line endings', () => {
expect(dedent(' hello\r\n world')).toBe('hello\nworld');
});

it('should handle a string with only empty lines', () => {
expect(dedent('\n\n\n')).toBe('');
});

it('should compose with another tag function', () => {
const upper = (strings: TemplateStringsArray, ...values: unknown[]) => {
let result = '';
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
result += String(values[i]);
}
}
return result.toUpperCase();
};

const dedentedUpper = dedent(upper);
const result = dedentedUpper`
hello
world
`;
expect(result).toBe('HELLO\nWORLD');
});

it('should compose with another tag function and preserve interpolations', () => {
const identity = (strings: TemplateStringsArray, ...values: unknown[]) => {
let result = '';
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
result += String(values[i]);
}
}
return result;
};

const dedentedIdentity = dedent(identity);
const name = 'es-toolkit';
const result = dedentedIdentity`
Welcome to
${name}!
`;
expect(result).toBe('Welcome to\nes-toolkit!');
});
});
Loading