Skip to content
Merged
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
2 changes: 1 addition & 1 deletion docs/ja/reference/array/partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const [truthy, falsy] = partition([], x => x > 0);
#### パラメータ

- `arr` (`T[]`): 2つのグループに分ける配列です。
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => boolean`): 各要素が最初の配列(truthy)に含まれるか、2番目の配列(falsy)に含まれるかを決定する条件関数です。値、インデックス、配列全体と共に呼び出されます。
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => unknown`): 各要素が最初の配列(truthy)に含まれるか、2番目の配列(falsy)に含まれるかを決定する条件関数です。値、インデックス、配列全体と共に呼び出されます。

#### 戻り値

Expand Down
2 changes: 1 addition & 1 deletion docs/ko/reference/array/partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const [truthy, falsy] = partition([], x => x > 0);
#### 파라미터

- `arr` (`T[]`): 두 그룹으로 나눌 배열이에요.
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => boolean`): 각 요소가 첫 번째 배열(truthy)에 포함될지, 두 번째 배열(falsy)에 포함될지를 결정하는 조건 함수예요. 값, 인덱스, 전체 배열과 함께 호출돼요.
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => unknown`): 각 요소가 첫 번째 배열(truthy)에 포함될지, 두 번째 배열(falsy)에 포함될지를 결정하는 조건 함수예요. 값, 인덱스, 전체 배열과 함께 호출돼요.

#### 반환 값

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/array/partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const [truthy, falsy] = partition([], x => x > 0);
#### Parameters

- `arr` (`T[]`): The array to split into two groups.
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => boolean`): A condition function that determines whether each element should be included in the first array (truthy) or the second array (falsy). The function is called with the value, its index, and the entire array.
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => unknown`): A condition function that determines whether each element should be included in the first array (truthy) or the second array (falsy). The function is called with the value, its index, and the entire array.

#### Returns

Expand Down
2 changes: 1 addition & 1 deletion docs/zh_hans/reference/array/partition.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const [truthy, falsy] = partition([], x => x > 0);
#### 参数

- `arr` (`T[]`): 要分为两组的数组。
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => boolean`): 决定每个元素应包含在第一个数组(truthy)还是第二个数组(falsy)中的条件函数。该函数接收值、索引和整个数组作为参数。
- `isInTruthy` (`(value: T, index: number, array: readonly T[]) => unknown`): 决定每个元素应包含在第一个数组(truthy)还是第二个数组(falsy)中的条件函数。该函数接收值、索引和整个数组作为参数。

#### 返回值

Expand Down
17 changes: 17 additions & 0 deletions src/array/partition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@ describe('partition', () => {
expect(array).toBe(arr);
});
});

it('should accept a predicate that returns any truthy or falsy value, like Array.prototype.filter', () => {
interface Value {
name: string;
good?: boolean;
}

const values: Value[] = [{ name: 'a', good: true }, { name: 'b', good: false }, { name: 'c' }];

const [good, bad] = partition(values, i => i.good);

expect(good).toEqual([{ name: 'a', good: true }]);
expect(bad).toEqual([{ name: 'b', good: false }, { name: 'c' }]);

expectTypeOf(good).toEqualTypeOf<Value[]>();
expectTypeOf(bad).toEqualTypeOf<Value[]>();
});
});
11 changes: 6 additions & 5 deletions src/array/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export function partition<T, U extends T>(
* @template T - The type of elements in the array.
* @param arr - The array to partition.
* @param isInTruthy - A predicate function that determines
* whether an element should be placed in the truthy array. The function is called with each
* whether an element should be placed in the truthy array. An element is placed in the truthy
* array when the function returns a truthy value. The function is called with each
* element of the array and its index.
* @returns A tuple containing two arrays: the first array contains elements for
* which the predicate returned true, and the second array contains elements for which the
* predicate returned false.
* which the predicate returned a truthy value, and the second array contains elements for which the
* predicate returned a falsy value.
*
* @example
* const array = [1, 2, 3, 4, 5];
Expand All @@ -50,11 +51,11 @@ export function partition<T, U extends T>(
*/
export function partition<T>(
arr: readonly T[],
isInTruthy: (value: T, index: number, array: readonly T[]) => boolean
isInTruthy: (value: T, index: number, array: readonly T[]) => unknown
): [truthy: T[], falsy: T[]];
export function partition<T>(
arr: readonly T[],
isInTruthy: (value: T, index: number, array: readonly T[]) => boolean
isInTruthy: (value: T, index: number, array: readonly T[]) => unknown
): [truthy: T[], falsy: T[]] {
const truthy: T[] = [];
const falsy: T[] = [];
Expand Down