diff --git a/docs/ja/reference/compat/array/castArray.md b/docs/ja/reference/compat/array/castArray.md index c8c7eaba..6c66ae73 100644 --- a/docs/ja/reference/compat/array/castArray.md +++ b/docs/ja/reference/compat/array/castArray.md @@ -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: [] -``` \ No newline at end of file +``` diff --git a/docs/ja/reference/compat/function/rearg.md b/docs/ja/reference/compat/function/rearg.md new file mode 100644 index 00000000..61523ee3 --- /dev/null +++ b/docs/ja/reference/compat/function/rearg.md @@ -0,0 +1,31 @@ +# rearg + +`func` 関数に与えられる引数の順序を変更する新しい関数を生成します。 + +`indices` 配列が示すインデックスに従って引数の順序が変わります。例えば、`indices` が `[2, 0, 1]` の場合、新しい関数の最初の引数は第三の引数として、第二の引数は第一の引数として、第三の引数は第二の引数として与えられます。 + +## インターフェース + +```typescript +function rearg any>( + func: F, + ...indices: Array +): (...args: any[]) => ReturnType; +``` + +### パラメータ + +- `func` (`F`): 引数を並べ替える関数です。 +- `indices` (`Array`): 並べ替えられた引数のインデックスです。 + +### 戻り値 + +(`(...args: any[]) => ReturnType`): 新しい関数を返します。 + +## 例 + +```typescript +const greet = (greeting: string, name: string) => `${greeting}, ${name}!`; +const rearrangedGreet = rearg(greet, 1, 0); +console.log(rearrangedGreet('World', 'Hello')); // Output: "Hello, World!" +``` diff --git a/docs/ko/reference/compat/function/rearg.md b/docs/ko/reference/compat/function/rearg.md new file mode 100644 index 00000000..2d9bdfac --- /dev/null +++ b/docs/ko/reference/compat/function/rearg.md @@ -0,0 +1,31 @@ +# rearg + +`func` 함수에게 주어지는 인자의 순서를 바꾸는 새로운 함수를 생성해요. + +`indices` 배열이 나타내는 인덱스에 따라서 인자의 순서가 바뀌어요. 예를 들어서, `indices`가 `[2, 0, 1]` 이라면, 새 함수의 첫 번째 인자는 세 번째 인자로, 두 번째 인자는 첫 번째 인자로, 세 번째 인자는 두 번째 인자로 주어져요. + +## 인터페이스 + +```typescript +function rearg any>( + func: F, + ...indices: Array +): (...args: any[]) => ReturnType; +``` + +### 파라미터 + +- `func` (`F`): 인수를 재배열할 함수예요. +- `indices` (`Array`): 배열된 인수 인덱스예요. + +### 반환 값 + +(`(...args: any[]) => ReturnType`): 새로운 함수를 반환해요. + +## 예시 + +```typescript +const greet = (greeting: string, name: string) => `${greeting}, ${name}!`; +const rearrangedGreet = rearg(greet, 1, 0); +console.log(rearrangedGreet('World', 'Hello')); // Output: "Hello, World!" +``` diff --git a/docs/reference/function/rearg.md b/docs/reference/compat/function/rearg.md similarity index 100% rename from docs/reference/function/rearg.md rename to docs/reference/compat/function/rearg.md diff --git a/docs/zh_hans/reference/compat/array/castArray.md b/docs/zh_hans/reference/compat/array/castArray.md index 313570ce..1a556cf6 100644 --- a/docs/zh_hans/reference/compat/array/castArray.md +++ b/docs/zh_hans/reference/compat/array/castArray.md @@ -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: [] -``` \ No newline at end of file +``` diff --git a/docs/zh_hans/reference/function/rearg.md b/docs/zh_hans/reference/compat/function/rearg.md similarity index 100% rename from docs/zh_hans/reference/function/rearg.md rename to docs/zh_hans/reference/compat/function/rearg.md diff --git a/src/function/rearg.spec.ts b/src/compat/function/rearg.spec.ts similarity index 100% rename from src/function/rearg.spec.ts rename to src/compat/function/rearg.spec.ts diff --git a/src/function/rearg.ts b/src/compat/function/rearg.ts similarity index 63% rename from src/function/rearg.ts rename to src/compat/function/rearg.ts index 1e2f810d..82e2ecf6 100644 --- a/src/function/rearg.ts +++ b/src/compat/function/rearg.ts @@ -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} indexes The arranged argument indexes. + * @param {Array} indices The arranged argument indices. * @returns {(...args: any[]) => ReturnType} 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 any>( func: F, - ...indexes: Array + ...indices: Array ): (...args: any[]) => ReturnType { - 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]); diff --git a/src/compat/index.ts b/src/compat/index.ts index 6e6a4583..e5c51234 100644 --- a/src/compat/index.ts +++ b/src/compat/index.ts @@ -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'; diff --git a/src/function/index.ts b/src/function/index.ts index 7720eaed..a4f3f914 100644 --- a/src/function/index.ts +++ b/src/function/index.ts @@ -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';