Skip to content

Commit

Permalink
docs(intersectionWith): Update docs (#833)
Browse files Browse the repository at this point in the history
* refactor(intersectionWith): Update type parameter name for consistency

* docs(intersectionWith): add example for handling different item types

* docs(intersectionWith): update docs
  • Loading branch information
mass2527 authored Nov 20, 2024
1 parent e7bb35f commit a46d8c4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
16 changes: 13 additions & 3 deletions docs/ja/reference/array/intersectionWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
## インターフェース

```typescript
function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[];
function intersectionWith<T, U>(firstArr: T[], secondArr: U[], areItemsEqual: (x: T, y: U) => boolean): T[];
```

### パラメータ

- `firstArr` (`T[]`): 比較する最初の配列。
- `secondArr` (`T[]`): 比較する2番目の配列。
- `areItemsEqual` (`(x: T, y: T) => boolean`): 2つの要素が一致するかどうかを判断する一致関数です。2つの要素が一致する場合は `true` を、一致しない場合は `false` を返すようにしてください。
- `secondArr` (`U[]`): 比較する2番目の配列。
- `areItemsEqual` (`(x: T, y: U) => boolean`): 2つの要素が一致するかどうかを判断する一致関数です。2つの要素が一致する場合は `true` を、一致しない場合は `false` を返すようにしてください。

### 戻り値

Expand All @@ -30,4 +30,14 @@ const array2 = [{ id: 2 }, { id: 4 }];
const areItemsEqual = (a, b) => a.id === b.id;
const result = intersectionWith(array1, array2, areItemsEqual);
// `areItemsEqual` 基準でarray1とarray2の両方に含まれている要素からなる [{ id: 2 }] が返されます。

const array1 = [
{ id: 1, name: 'jane' },
{ id: 2, name: 'amy' },
{ id: 3, name: 'michael' },
];
const array2 = [2, 4];
const areItemsEqual = (a, b) => a.id === b;
const result = intersectionWith(array1, array2, areItemsEqual);
// 結果は [{ id: 2, name: 'amy' }] になります。この要素は、2番目の配列の要素と一致するidを持っているためです。
```
16 changes: 13 additions & 3 deletions docs/ko/reference/array/intersectionWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
## 인터페이스

```typescript
function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[];
function intersectionWith<T, U>(firstArr: T[], secondArr: U[], areItemsEqual: (x: T, y: U) => boolean): T[];
```

### 파라미터

- `firstArr` (`T[]`): 비교할 첫 번째 배열.
- `secondArr` (`T[]`): 비교할 두 번째 배열.
- `areItemsEqual` (`(x: T, y: T) => boolean`): 두 요소가 일치하는지 판단하는 일치 함수예요. 두 요소가 일치한다면 `true`를, 일치하지 않는다면 `false`를 반환하게 해주세요.
- `secondArr` (`U[]`): 비교할 두 번째 배열.
- `areItemsEqual` (`(x: T, y: U) => boolean`): 두 요소가 일치하는지 판단하는 일치 함수예요. 두 요소가 일치한다면 `true`를, 일치하지 않는다면 `false`를 반환하게 해주세요.

### 반환 값

Expand All @@ -30,4 +30,14 @@ const array2 = [{ id: 2 }, { id: 4 }];
const areItemsEqual = (a, b) => a.id === b.id;
const result = intersectionWith(array1, array2, areItemsEqual);
// `areItemsEqual` 기준으로 array1과 array2에 모두 포함되어 있는 요소들로 이루어진 [{ id: 2 }] 이 반환돼요.

const array1 = [
{ id: 1, name: 'jane' },
{ id: 2, name: 'amy' },
{ id: 3, name: 'michael' },
];
const array2 = [2, 4];
const areItemsEqual = (a, b) => a.id === b;
const result = intersectionWith(array1, array2, areItemsEqual);
// `areItemsEqual` 기준으로 array1의 `id`와 array2의 요소가 일치하는 [{ id: 2, name: 'amy' }] 이 반환돼요.
```
16 changes: 13 additions & 3 deletions docs/reference/array/intersectionWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ do not have corresponding matches in the second array according to the equality
## Signature

```typescript
function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[];
function intersectionWith<T, U>(firstArr: T[], secondArr: U[], areItemsEqual: (x: T, y: U) => boolean): T[];
```

### Parameters

- `firstArr` (`T[]`): The first array to compare.
- `secondArr` (`T[]`): The second array to compare.
- `areItemsEqual` (`(x: T, y: T) => boolean`): A custom function to determine if two elements are equal. This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
- `secondArr` (`U[]`): The second array to compare.
- `areItemsEqual` (`(x: T, y: U) => boolean`): A custom function to determine if two elements are equal. This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.

### Returns

Expand All @@ -31,4 +31,14 @@ const array2 = [{ id: 2 }, { id: 4 }];
const areItemsEqual = (a, b) => a.id === b.id;
const result = intersectionWith(array1, array2, areItemsEqual);
// result will be [{ id: 2 }] since this element has a matching id in both arrays.

const array1 = [
{ id: 1, name: 'jane' },
{ id: 2, name: 'amy' },
{ id: 3, name: 'michael' },
];
const array2 = [2, 4];
const areItemsEqual = (a, b) => a.id === b;
const result = intersectionWith(array1, array2, areItemsEqual);
// result will be [{ id: 2, name: 'amy' }] since this element has a matching id that is equal to seconds array's element.
```
16 changes: 13 additions & 3 deletions docs/zh_hans/reference/array/intersectionWith.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
## 签名

```typescript
function intersectionWith<T>(firstArr: T[], secondArr: T[], areItemsEqual: (x: T, y: T) => boolean): T[];
function intersectionWith<T, U>(firstArr: T[], secondArr: U[], areItemsEqual: (x: T, y: U) => boolean): T[];
```

### 参数

- `firstArr` (`T[]`): 要比较的第一个数组。
- `secondArr` (`T[]`): 要比较的第二个数组。
- `areItemsEqual` (`(x: T, y: T) => boolean`): 一个自定义函数,用于确定两个元素是否相等。该函数接受两个参数,分别来自每个数组,如果这两个元素被认为相等,则返回 `true`,否则返回 `false`
- `secondArr` (`U[]`): 要比较的第二个数组。
- `areItemsEqual` (`(x: T, y: U) => boolean`): 一个自定义函数,用于确定两个元素是否相等。该函数接受两个参数,分别来自每个数组,如果这两个元素被认为相等,则返回 `true`,否则返回 `false`

### 返回值

Expand All @@ -30,4 +30,14 @@ const array2 = [{ id: 2 }, { id: 4 }];
const areItemsEqual = (a, b) => a.id === b.id;
const result = intersectionWith(array1, array2, areItemsEqual);
// 结果将是 [{ id: 2 }] 因为这个元素在两个数组中具有匹配的 id。

const array1 = [
{ id: 1, name: 'jane' },
{ id: 2, name: 'amy' },
{ id: 3, name: 'michael' },
];
const array2 = [2, 4];
const areItemsEqual = (a, b) => a.id === b;
const result = intersectionWith(array1, array2, areItemsEqual);
// 结果将是 [{ id: 2, name: 'amy' }] 因为该元素的 id 与第二个数组中的元素匹配。
```
33 changes: 22 additions & 11 deletions src/array/intersectionWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,37 @@
* by the custom equality function. It effectively filters out any elements from the first array that
* do not have corresponding matches in the second array according to the equality function.
*
* @template F - The type of elements in the first array.
* @template S - The type of elements in the second array.
* @param {F[]} firstArr - The first array to compare.
* @param {S[]} secondArr - The second array to compare.
* @param {(x: F, y: S) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
* @template T - The type of elements in the first array.
* @template U - The type of elements in the second array.
* @param {T[]} firstArr - The first array to compare.
* @param {U[]} secondArr - The second array to compare.
* @param {(x: T, y: U) => boolean} areItemsEqual - A custom function to determine if two elements are equal.
* This function takes two arguments, one from each array, and returns `true` if the elements are considered equal, and `false` otherwise.
* @returns {F[]} A new array containing the elements from the first array that have corresponding matches in the second array according to the custom equality function.
* @returns {T[]} A new array containing the elements from the first array that have corresponding matches in the second array according to the custom equality function.
*
* @example
* const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
* const array2 = [{ id: 2 }, { id: 4 }];
* const areItemsEqual = (a, b) => a.id === b.id;
* const result = intersectionWith(array1, array2, areItemsEqual);
* // result will be [{ id: 2 }] since this element has a matching id in both arrays.
*
* @example
* const array1 = [
* { id: 1, name: 'jane' },
* { id: 2, name: 'amy' },
* { id: 3, name: 'michael' },
* ];
* const array2 = [2, 4];
* const areItemsEqual = (a, b) => a.id === b;
* const result = intersectionWith(array1, array2, areItemsEqual);
* // result will be [{ id: 2, name: 'amy' }] since this element has a matching id that is equal to seconds array's element.
*/
export function intersectionWith<F, S>(
firstArr: readonly F[],
secondArr: readonly S[],
areItemsEqual: (x: F, y: S) => boolean
): F[] {
export function intersectionWith<T, U>(
firstArr: readonly T[],
secondArr: readonly U[],
areItemsEqual: (x: T, y: U) => boolean
): T[] {
return firstArr.filter(firstItem => {
return secondArr.some(secondItem => {
return areItemsEqual(firstItem, secondItem);
Expand Down

0 comments on commit a46d8c4

Please sign in to comment.