docs(rearg): Add rearg in compat and add docs
Some checks are pending
CI / codecov (push) Waiting to run
Release / release (push) Waiting to run

This commit is contained in:
Sojin Park 2024-09-08 23:48:58 +09:00
parent ff595628cd
commit 3c3bba011f
10 changed files with 79 additions and 12 deletions

View File

@ -31,10 +31,10 @@ const arr1 = castArray(1);
const arr2 = castArray([1]);
// Returns: [1]
const arr3 = castArray({'a': 1});
const arr3 = castArray({ a: 1 });
// Returns: [{'a': 1}]
const arr4 = castArray(null);
const arr4 = castArray(null);
// Returns: [null]
const arr5 = castArray(undefined);
@ -42,4 +42,4 @@ const arr5 = castArray(undefined);
const arr6 = castArray();
// Returns: []
```
```

View File

@ -0,0 +1,31 @@
# rearg
`func` 関数に与えられる引数の順序を変更する新しい関数を生成します。
`indices` 配列が示すインデックスに従って引数の順序が変わります。例えば、`indices` が `[2, 0, 1]` の場合、新しい関数の最初の引数は第三の引数として、第二の引数は第一の引数として、第三の引数は第二の引数として与えられます。
## インターフェース
```typescript
function rearg<F extends (...args: any[]) => any>(
func: F,
...indices: Array<number | number[]>
): (...args: any[]) => ReturnType<F>;
```
### パラメータ
- `func` (`F`): 引数を並べ替える関数です。
- `indices` (`Array<number | number[]>`): 並べ替えられた引数のインデックスです。
### 戻り値
(`(...args: any[]) => ReturnType<F>`): 新しい関数を返します。
## 例
```typescript
const greet = (greeting: string, name: string) => `${greeting}, ${name}!`;
const rearrangedGreet = rearg(greet, 1, 0);
console.log(rearrangedGreet('World', 'Hello')); // Output: "Hello, World!"
```

View File

@ -0,0 +1,31 @@
# rearg
`func` 함수에게 주어지는 인자의 순서를 바꾸는 새로운 함수를 생성해요.
`indices` 배열이 나타내는 인덱스에 따라서 인자의 순서가 바뀌어요. 예를 들어서, `indices``[2, 0, 1]` 이라면, 새 함수의 첫 번째 인자는 세 번째 인자로, 두 번째 인자는 첫 번째 인자로, 세 번째 인자는 두 번째 인자로 주어져요.
## 인터페이스
```typescript
function rearg<F extends (...args: any[]) => any>(
func: F,
...indices: Array<number | number[]>
): (...args: any[]) => ReturnType<F>;
```
### 파라미터
- `func` (`F`): 인수를 재배열할 함수예요.
- `indices` (`Array<number | number[]>`): 배열된 인수 인덱스예요.
### 반환 값
(`(...args: any[]) => ReturnType<F>`): 새로운 함수를 반환해요.
## 예시
```typescript
const greet = (greeting: string, name: string) => `${greeting}, ${name}!`;
const rearrangedGreet = rearg(greet, 1, 0);
console.log(rearrangedGreet('World', 'Hello')); // Output: "Hello, World!"
```

View File

@ -31,10 +31,10 @@ const arr1 = castArray(1);
const arr2 = castArray([1]);
// Returns: [1]
const arr3 = castArray({'a': 1});
const arr3 = castArray({ a: 1 });
// Returns: [{'a': 1}]
const arr4 = castArray(null);
const arr4 = castArray(null);
// Returns: [null]
const arr5 = castArray(undefined);
@ -42,4 +42,4 @@ const arr5 = castArray(undefined);
const arr6 = castArray();
// Returns: []
```
```

View File

@ -1,23 +1,28 @@
import { flatten } from '../array/flatten.ts';
/**
* Creates a function that invokes `func` with arguments arranged according to the specified `indexes`
* Creates a function that invokes `func` with arguments arranged according to the specified `indices`
* where the argument value at the first index is provided as the first argument,
* the argument value at the second index is provided as the second argument, and so on.
*
* @template F The type of the function to re-arrange.
* @param {F} func The function to rearrange arguments for.
* @param {Array<number | number[]>} indexes The arranged argument indexes.
* @param {Array<number | number[]>} indices The arranged argument indices.
* @returns {(...args: any[]) => ReturnType<F>} Returns the new function.
*
* @example
* const greet = (greeting: string, name: string) => `${greeting}, ${name}!`;
* const rearrangedGreet = rearg(greet, 1, 0);
* console.log(rearrangedGreet('World', 'Hello')); // Output: "Hello, World!"
*/
export function rearg<F extends (...args: any[]) => any>(
func: F,
...indexes: Array<number | number[]>
...indices: Array<number | number[]>
): (...args: any[]) => ReturnType<F> {
const flattenIndexes = flatten(indexes);
const flattenIndices = flatten(indices);
return function (this: any, ...args: any[]) {
const reorderedArgs: any[] = flattenIndexes.map(i => args[i]).slice(0, args.length);
const reorderedArgs: any[] = flattenIndices.map(i => args[i]).slice(0, args.length);
for (let i = reorderedArgs.length; i < args.length; i++) {
reorderedArgs.push(args[i]);

View File

@ -48,6 +48,7 @@ export { bindKey } from './function/bindKey.ts';
export { rest } from './function/rest.ts';
export { spread } from './function/spread.ts';
export { attempt } from './function/attempt.ts';
export { rearg } from './function/rearg.ts';
export { get } from './object/get.ts';
export { set } from './object/set.ts';

View File

@ -11,5 +11,4 @@ export { unary } from './unary.ts';
export { partial } from './partial.ts';
export { partialRight } from './partialRight.ts';
export { rest } from './rest.ts';
export { rearg } from './rearg.ts';
export { spread } from './spread.ts';