Skip to content

Commit

Permalink
Merge branch 'main' into feat/forEachRight
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 authored Jul 3, 2024
2 parents 0aae40c + 834d50c commit b4978b4
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 3 deletions.
27 changes: 27 additions & 0 deletions benchmarks/unzipWith.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { unzipWith as unzipWithToolkit } from 'es-toolkit';
import { unzipWith as unzipWithLodash } from 'lodash';
import { bench, describe } from 'vitest';

describe('unzipWith', () => {
bench('es-toolkit/unzipWith', () => {
unzipWithToolkit(
[
[1000, 2000],
[3000, 4000],
[50000, 60000],
],
(a, b, c) => a + b + c
);
});

bench('lodash/unzipWith', () => {
unzipWithLodash(
[
[1000, 2000],
[3000, 4000],
[50000, 60000],
],
(a, b, c) => a + b + c
);
});
});
3 changes: 3 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function sidebar(): DefaultTheme.Sidebar {
text: 'Array Utilities',
items: [
{ text: 'chunk', link: '/reference/array/chunk' },
{ text: 'countBy', link: '/reference/array/countBy' },
{ text: 'compact', link: '/reference/array/compact' },
{ text: 'difference', link: '/reference/array/difference' },
{ text: 'differenceBy', link: '/reference/array/differenceBy' },
Expand Down Expand Up @@ -79,6 +80,8 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'uniq', link: '/reference/array/uniq' },
{ text: 'uniqBy', link: '/reference/array/uniqBy' },
{ text: 'uniqWith', link: '/reference/array/uniqWith' },
{ text: 'unzipWith', link: '/reference/array/unzipWith' },
{ text: 'without', link: '/reference/array/without' },
{ text: 'xor', link: '/reference/array/xor' },
{ text: 'xorBy', link: '/reference/array/xorBy' },
{ text: 'xorWith', link: '/reference/array/xorWith' },
Expand Down
3 changes: 3 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function sidebar(): DefaultTheme.Sidebar {
text: '배열',
items: [
{ text: 'chunk', link: '/ko/reference/array/chunk' },
{ text: 'countBy', link: '/ko/reference/array/countBy' },
{ text: 'compact', link: '/ko/reference/array/compact' },
{ text: 'difference', link: '/ko/reference/array/difference' },
{ text: 'differenceBy', link: '/ko/reference/array/differenceBy' },
Expand Down Expand Up @@ -75,9 +76,11 @@ function sidebar(): DefaultTheme.Sidebar {
{ text: 'union', link: '/ko/reference/array/union' },
{ text: 'unionBy', link: '/ko/reference/array/unionBy' },
{ text: 'unionWith', link: '/ko/reference/array/unionWith' },
{ text: 'unzipWith', link: '/reference/array/unzipWith' },
{ text: 'uniq', link: '/ko/reference/array/uniq' },
{ text: 'uniqBy', link: '/ko/reference/array/uniqBy' },
{ text: 'uniqWith', link: '/ko/reference/array/uniqWith' },
{ text: 'without', link: '/ko/reference/array/without' },
{ text: 'xor', link: '/ko/reference/array/xor' },
{ text: 'xorBy', link: '/ko/reference/array/xorBy' },
{ text: 'xorWith', link: '/ko/reference/array/xorWith' },
Expand Down
30 changes: 30 additions & 0 deletions docs/ko/reference/array/unzipWith.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# unzipWith

결합되어 있는 2차원 배열을 1차원 배열로 풀고, `iteratee` 함수로 값을 변환해서 새로운 배열을 반환해요.

## 인터페이스

```typescript
function unzipWith<T, R>(target: readonly T[][], iteratee: (...args: T[]) => R): R[];
```

### 파라미터

- `target` (`T[][]`): 결합을 풀고 변환할 배열이에요.
- `iteratee` (`(...args: T[]) => R`): 결합이 풀린 배열을 변환할 함수예요.

### 반환 값

(`R[]`): 결합을 풀고 변환된 값들로 만들어진 새로운 배열이에요.

## 예시

```typescript
const nestedArray = [
[1, 2],
[3, 4],
[5, 6],
];
const result = unzipWith(nestedArray, (item, item2, item3) => item + item2 + item3);
// [9, 12]
```
30 changes: 30 additions & 0 deletions docs/reference/array/unzipWith.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# unzipWith

Unzips an array of arrays, applying an `iteratee` function to regrouped elements.

## Signature

```typescript
function unzipWith<T, R>(target: T[][], iteratee: (...args: T[]) => R): R[];
```

### Parameters

- `target`(`T[][]`): The nested array to unzip. This is an array of arrays, where each inner array contains elements to be unzipped.
- `iteratee` (`(...args: T[]) => R`): A function to transform the unzipped elements.

### Returns

(`R[]`): A new array of unzipped and transformed elements.

## Examples

```typescript
const nestedArray = [
[1, 2],
[3, 4],
[5, 6],
];
const result = unzipWith(nestedArray, (item, item2, item3) => item + item2 + item3);
// [9, 12]
```
7 changes: 4 additions & 3 deletions src/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { drop } from './drop.ts';
export { dropRight } from './dropRight.ts';
export { dropRightWhile } from './dropRightWhile.ts';
export { dropWhile } from './dropWhile.ts';
export { fill } from './fill.ts';
export { forEachRight } from './forEachRight.ts'
export { groupBy } from './groupBy.ts';
export { intersection } from './intersection.ts';
Expand All @@ -30,11 +31,11 @@ export { unionWith } from './unionWith.ts';
export { uniq } from './uniq.ts';
export { uniqBy } from './uniqBy.ts';
export { uniqWith } from './uniqWith.ts';
export { without } from './without.ts';
export { unzipWith } from './unzipWith.ts';
export { xor } from './xor.ts';
export { xorBy } from './xorBy.ts';
export { xorWith } from './xorWith.ts';
export { zip } from './zip.ts';
export { zipWith } from './zipWith.ts';
export { zipObject } from './zipObject.ts';
export { fill } from './fill.ts';
export { zipWith } from './zipWith.ts';
export { without } from './without.ts';
19 changes: 19 additions & 0 deletions src/array/unzipWith.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import { unzipWith } from './unzipWith';
import { zip } from './zip';

describe('unzipWith', () => {
it('should unzip arrays correctly with an iteratee', () => {
const zipped = zip([10, 20, 30], [40, 50, 60], [70, 80, 90]);
const result = unzipWith(zipped, (item, item2, item3) => item + item2 + item3);

expect(result).toEqual([60, 150, 240]);
});

it('should handle arrays of different lengths', () => {
const zipped = zip([1, 20, 300, 4000], [100, 200, 300]);
const result = unzipWith(zipped, (item, item2, item3, item4) => item + item2 + item3 + item4);

expect(result).toEqual([4321, NaN]);
});
});
30 changes: 30 additions & 0 deletions src/array/unzipWith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Unzips an array of arrays, applying an `iteratee` function to regrouped elements.
*
* @template T, R
* @param {T[][]} target - The nested array to unzip. This is an array of arrays,
* where each inner array contains elements to be unzipped.
* @param {(...args: T[]) => R} iteratee - A function to transform the unzipped elements.
* @returns {R[]} A new array of unzipped and transformed elements.
*
* @example
* const nestedArray = [[1, 2], [3, 4], [5, 6]];
* const result = unzipWith(nestedArray, (item, item2, item3) => item + item2 + item3);
* // result will be [9, 12]
*/
export function unzipWith<T, R>(target: readonly T[][], iteratee: (...args: T[]) => R): R[] {
const maxLength = Math.max(...target.map(innerArray => innerArray.length));
const result: R[] = new Array(maxLength);

for (let i = 0; i < maxLength; i++) {
const group = new Array(target.length);

for (let j = 0; j < target.length; j++) {
group[j] = target[j][i];
}

result[i] = iteratee(...group);
}

return result;
}

0 comments on commit b4978b4

Please sign in to comment.