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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,34 @@ Zips given arrays together into pairs.
```
<!-- prettier-ignore-end -->

#### zipN

Zips given arrays

##### Type signature

<!-- prettier-ignore-start -->
```typescript
(...xs: any[][]) => any[][]
```
<!-- prettier-ignore-end -->

##### Examples

<!-- prettier-ignore-start -->
```javascript
zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]
```

```javascript
zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
```

```javascript
zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]
```
<!-- prettier-ignore-end -->

#### zipWith

Zips given arrays together with the given function.
Expand Down
28 changes: 28 additions & 0 deletions array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,34 @@ Zips given arrays together into pairs.
```
<!-- prettier-ignore-end -->

# zipN

Zips given arrays

## Type signature

<!-- prettier-ignore-start -->
```typescript
(...xs: any[][]) => any[][]
```
<!-- prettier-ignore-end -->

## Examples

<!-- prettier-ignore-start -->
```javascript
zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]
```

```javascript
zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
```

```javascript
zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]
```
<!-- prettier-ignore-end -->

# zipWith

Zips given arrays together with the given function.
Expand Down
3 changes: 3 additions & 0 deletions array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import sum from "./sum.js";
import take from "./take.js";
import unique from "./unique.js";
import zip from "./zip.js";
import zipN from "./zipN.js";
import zipWith from "./zipWith.js";

export {
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
take,
unique,
zip,
zipN,
zipWith
};

Expand Down Expand Up @@ -122,5 +124,6 @@ export default {
take,
unique,
zip,
zipN,
zipWith
};
3 changes: 3 additions & 0 deletions array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import sum from "./sum";
import take from "./take";
import unique from "./unique";
import zip from "./zip";
import zipN from "./zipN";
import zipWith from "./zipWith";

export {
Expand Down Expand Up @@ -79,6 +80,7 @@ export {
take,
unique,
zip,
zipN,
zipWith
};

Expand Down Expand Up @@ -122,5 +124,6 @@ export default {
take,
unique,
zip,
zipN,
zipWith
};
7 changes: 7 additions & 0 deletions array/zipN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (...xs) => {
const [head = [], ...tail] = xs;

return head.map((value, index) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail for the case zipN() - please guard against it and cover it with tests.

tail.reduce((x, xs) => [...x, xs[index]], [value])
);
};
20 changes: 20 additions & 0 deletions array/zipN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "zipN",
"description": "Zips given arrays",
"signature": "(...xs: any[][]) => any[][]",
"examples": [
{
"language": "javascript",
"content": "zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]"
},
{
"language": "javascript",
"content": "zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]"
},
{
"language": "javascript",
"content": "zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]"
}
],
"questions": ["TODO: List questions that may this function answers."]
}
27 changes: 27 additions & 0 deletions array/zipN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# zipN

Zips given arrays

## Type signature

<!-- prettier-ignore-start -->
```typescript
(...xs: any[][]) => any[][]
```
<!-- prettier-ignore-end -->

## Examples

<!-- prettier-ignore-start -->
```javascript
zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]
```

```javascript
zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
```

```javascript
zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]
```
<!-- prettier-ignore-end -->
69 changes: 69 additions & 0 deletions array/zipN.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-env jest */
// @ts-ignore ambiguous import
import zipN from "./zipN.ts";

describe("zip", () => {
it("should zip with pair constructor", () => {
expect(zipN([1, 2, 3], [4, 5, 6])).toEqual([
[1, 4],
[2, 5],
[3, 6]
]);
});

it("should zip up to the left arrays length", () => {
expect(zipN([1, 2, 3, 4, 5, 7], [4, 5, 6])).toEqual([
[1, 4],
[2, 5],
[3, 6],
[4, undefined],
[5, undefined],
[7, undefined]
]);

expect(zipN([1, 2, 3], [4, 5, 6, 4, 5, 7])).toEqual([
[1, 4],
[2, 5],
[3, 6]
]);
});

it("should return the wrapped elements of the source array when only one array is given", () => {
expect(zipN([1, 2])).toEqual([[1], [2]]);
});

it("should return an empty array when the given array is empty", () => {
expect(zipN([])).toEqual([]);
});

it("should return an empty array when the given nothing", () => {
expect(zipN()).toEqual([]);
});

it("should zip three arrays with the same lengths", () => {
expect(zipN([1, 2, 3], [4, 5, 6], [7, 8, 9])).toEqual([
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]);
});

it("should zip three arrays with different lengths", () => {
expect(zipN([1, 2, 3], [4, 5, 6], [7, 8])).toEqual([
[1, 4, 7],
[2, 5, 8],
[3, 6, undefined]
]);

expect(zipN([1, 2, 3], [4, 5], [7, 8, 9])).toEqual([
[1, 4, 7],
[2, 5, 8],
[3, undefined, 9]
]);

expect(zipN([1, 2], [4, 5, 6], [7, 8, 9])).toEqual([
[1, 4, 7],
[2, 5, 8]
]);
});
});
7 changes: 7 additions & 0 deletions array/zipN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (...xs: any[][]) => {
const [head = [], ...tail] = xs;

return head.map((value, index) =>
tail.reduce((x, xs) => [...x, xs[index]], [value])
);
};