docs(rest): Improve docs for rest

This commit is contained in:
Sojin Park 2024-08-15 21:19:59 +09:00
parent 71a710b762
commit 98af4a3a11
6 changed files with 139 additions and 30 deletions

View File

@ -140,6 +140,7 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'bind (호환성)', link: '/ko/reference/compat/function/unary' },
{ text: 'partial', link: '/ko/reference/function/partial' },
{ text: 'partialRight', link: '/ko/reference/function/partialRight' },
{ text: 'rest', link: '/ko/reference/function/rest' },
],
},
{

View File

@ -0,0 +1,38 @@
# rest
주어진 함수 `func`에게 특정 인덱스부터 인자를 배열로 묶어서 전달하는 새로운 함수를 만들어요.
특정 인덱스 전의 인자들은 개별로 전달되고, 이후 인자들은 배열로 한꺼번에 묶여서 전달돼요.
## 인터페이스
```typescript
function rest<F extends (...args: any[]) => any>(func: F, startIndex: number): (...args: any[]) => ReturnType<F>;
```
### 파라미터
- `func` (`F`): 인자를 받는 방식을 바꿀 함수.
- `startIndex` (`number`, 선택): 인자를 묶어서 전달하기 시작할 인덱스. 기본값은 `func.length - 1`으로, 마지막 파라미터부터는 배열로 묶어서 전달해요.
### 반환 값
(`(...args: any[]) => ReturnType<F>`): 특정 인덱스부터의 인자를 배열로 묶어서 `func`에 전달하는 새로운 함수.
## 예시
```typescript
function fn(a, b, c) {
return Array.from(arguments);
}
// 기본적으로는 마지막 인자부터를 배열로 묶어서 전달해요
const func1 = rest(fn);
console.log(func1(1, 2, 3, 4)); // [1, 2, [3, 4]]
// 2번째 인자부터 배열로 묶어서 전달해요
const func2 = rest(fn, 1);
console.log(func2(1, 2, 3, 4)); // [1, [2, 3, 4]]
// 인자를 부족하게 전달하는 경우
console.log(func1(1)); // [1, undefined, []]
```

View File

@ -1,21 +1,27 @@
# rest
Creates a function that invokes `func` with the `this` binding of the created function and arguments from `start` and beyond provided as an array.
Creates a function that transforms the arguments of the provided function `func`.
The transformed arguments are passed to `func` such that the arguments starting from a specified index
are grouped into an array, while the previous arguments are passed as individual elements.
## Signature
```typescript
function rest<F extends (...args: any[]) => any>(func: F, start: number): (...args: any[]) => ReturnType<F>;
function rest<F extends (...args: any[]) => any>(func: F, startIndex: number): (...args: any[]) => ReturnType<F>;
```
### Parameters
- `func` (`F`): The function to apply a rest parameter to.
- `start` (`number`, optional): The start position of the rest parameter, defaulting to `func.length - 1`.
- `func` (`F`): The function whose arguments are to be transformed.
- `startIndex` (`number`, optional): The index from which to start grouping the remaining arguments into an array. Defaults to `func.length - 1`, grouping all arguments after the last parameter.
### Returns
(`(...args: any[]) => ReturnType<F>`): Returns the new function.
(`(...args: any[]) => ReturnType<F>`): A new function that, when called, returns the result of calling `func` with the transformed arguments.
- The transformed arguments are:
- The first `start` arguments as individual elements.
- The remaining arguments from index `start` onward grouped into an array.
## Examples
@ -24,7 +30,14 @@ function fn(a, b, c) {
return Array.from(arguments);
}
rest(fn)(1, 2, 3, 4); // [1, 2, [3, 4]]
rest(fn, 1)(1, 2, 3, 4); // [1, [2, 3, 4]]
rest(fn)(1); // [1, undefined, []]
// Using default start index (func.length - 1, which is 2 in this case)
const func1 = rest(fn);
console.log(func1(1, 2, 3, 4)); // [1, 2, [3, 4]]
// Using start index 1
const func2 = rest(fn, 1);
console.log(func2(1, 2, 3, 4)); // [1, [2, 3, 4]]
// With fewer arguments than the start index
console.log(func1(1)); // [1, undefined, []]
```

View File

@ -1,21 +1,26 @@
# rest
创建一个函数,调用 `func` 时,`this` 绑定到创建的新函数,并且 `start` 之后的参数作为数组传入。
创建一个函数,该函数会转换提供的函数 `func` 的参数。
转换后的参数会传递给 `func`,使得从指定索引开始的参数被分组到一个数组中,而之前的参数则作为单独的元素传递。
## 签名
```typescript
function rest<F extends (...args: any[]) => any>(func: F, start: number): (...args: any[]) => ReturnType<F>;
function rest<F extends (...args: any[]) => any>(func: F, startIndex: number): (...args: any[]) => ReturnType<F>;
```
### 参数
- `func` (`F`): 要应用的函数。
- `start` (`number`, 可选): rest 参数的开始位置,默认为 `func.length - 1`.
- `func` (`F`): 需要转换参数的函数。
- `startIndex` (`number`, 可选):从哪个索引开始将剩余的参数分组到数组中。默认为 `func.length - 1`,即将最后一个参数后的所有参数分组。
### 返回值
(`(...args: any[]) => ReturnType<F>`): 返回新的函数。
(`(...args: any[]) => ReturnType<F>`): 一个新函数,当被调用时,返回使用转换后的参数调用 `func` 的结果。
- 转换后的参数为:
- 前 `start` 个参数作为单独的元素。
- 从索引 `start` 开始的剩余参数被分组到一个数组中。
## 示例
@ -24,7 +29,14 @@ function fn(a, b, c) {
return Array.from(arguments);
}
rest(fn)(1, 2, 3, 4); // [1, 2, [3, 4]]
rest(fn, 1)(1, 2, 3, 4); // [1, [2, 3, 4]]
rest(fn)(1); // [1, undefined, []]
// 使用默认起始索引 (func.length - 1即在此例中为 2)
const func1 = rest(fn);
console.log(func1(1, 2, 3, 4)); // [1, 2, [3, 4]]
// 使用起始索引 1
const func2 = rest(fn, 1);
console.log(func2(1, 2, 3, 4)); // [1, [2, 3, 4]]
// 参数少于起始索引
console.log(func1(1)); // [1, undefined, []]
```

View File

@ -1,20 +1,44 @@
import { rest as restToolkit } from '../../function/rest.ts';
/**
* Creates a function that invokes `func` with the this binding of the created function and arguments from start and beyond provided as an array.
* Creates a function that transforms the arguments of the provided function `func`.
* The transformed arguments are passed to `func` such that the arguments starting from a specified index
* are grouped into an array, while the previous arguments are passed as individual elements.
*
* @template F The type of the function.
* @param {Function} func The function to apply a rest parameter to.
* @param {number} start The start position of the rest parameter.
* @returns {Function} Returns the new function.
* @template F - The type of the function being transformed.
* @param {F} func - The function whose arguments are to be transformed.
* @param {number} [startIndex=func.length - 1] - The index from which to start grouping the remaining arguments into an array.
* Defaults to `func.length - 1`, grouping all arguments after the last parameter.
* @returns {(...args: any[]) => ReturnType<F>} A new function that, when called, returns the result of calling `func` with the transformed arguments.
*
* The transformed arguments are:
* - The first `start` arguments as individual elements.
* - The remaining arguments from index `start` onward grouped into an array.
* @example
* function fn(a, b, c) {
* return [a, b, c];
* }
*
* // Using default start index (func.length - 1, which is 2 in this case)
* const transformedFn = rest(fn);
* console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
*
* // Using start index 1
* const transformedFnWithStart = rest(fn, 1);
* console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
*
* // With fewer arguments than the start index
* console.log(transformedFn(1)); // [1, undefined, []]
*/
export function rest<F extends (...args: any[]) => any>(
func: F,
start = func.length - 1
): (...args: any[]) => ReturnType<F> {
start = Number.parseInt(start as any, 10);
if (Number.isNaN(start) || start < 0) {
start = func.length - 1;
}
return restToolkit(func, start);
}

View File

@ -1,20 +1,41 @@
/**
* Creates a function that invokes `func` with the this binding of the created function and arguments from start and beyond provided as an array.
* Creates a function that transforms the arguments of the provided function `func`.
* The transformed arguments are passed to `func` such that the arguments starting from a specified index
* are grouped into an array, while the previous arguments are passed as individual elements.
*
* @template F The type of the function.
* @param {Function} func The function to apply a rest parameter to.
* @param {number} start The start position of the rest parameter.
* @returns {Function} Returns the new function.
* @template F - The type of the function being transformed.
* @param {F} func - The function whose arguments are to be transformed.
* @param {number} [startIndex=func.length - 1] - The index from which to start grouping the remaining arguments into an array.
* Defaults to `func.length - 1`, grouping all arguments after the last parameter.
* @returns {(...args: any[]) => ReturnType<F>} A new function that, when called, returns the result of calling `func` with the transformed arguments.
*
* The transformed arguments are:
* - The first `start` arguments as individual elements.
* - The remaining arguments from index `start` onward grouped into an array.
* @example
* function fn(a, b, c) {
* return [a, b, c];
* }
*
* // Using default start index (func.length - 1, which is 2 in this case)
* const transformedFn = rest(fn);
* console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
*
* // Using start index 1
* const transformedFnWithStart = rest(fn, 1);
* console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
*
* // With fewer arguments than the start index
* console.log(transformedFn(1)); // [1, undefined, []]
*/
export function rest<F extends (...args: any[]) => any>(
func: F,
start = func.length - 1
startIndex = func.length - 1
): (...args: any[]) => ReturnType<F> {
return function (this: any, ...args: any[]) {
const rest = args.slice(start);
const params = args.slice(0, start);
while (params.length < start) {
const rest = args.slice(startIndex);
const params = args.slice(0, startIndex);
while (params.length < startIndex) {
params.push(undefined);
}
return func.apply(this, [...params, rest]);