Skip to content

Commit cab86fd

Browse files
kborucinskikrystianborucinski
authored andcommitted
Add array/zipN method (#38)
* Add zipN method * Update after code review * Update type * Update after code review * Add additional test case Co-authored-by: krystianborucinski <krystian.borucinski@sandstream.pl>
1 parent 09b68ee commit cab86fd

File tree

9 files changed

+192
-0
lines changed

9 files changed

+192
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,34 @@ Zips given arrays together into pairs.
569569
```
570570
<!-- prettier-ignore-end -->
571571

572+
#### zipN
573+
574+
Zips given arrays
575+
576+
##### Type signature
577+
578+
<!-- prettier-ignore-start -->
579+
```typescript
580+
(...xs: any[][]) => any[][]
581+
```
582+
<!-- prettier-ignore-end -->
583+
584+
##### Examples
585+
586+
<!-- prettier-ignore-start -->
587+
```javascript
588+
zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]
589+
```
590+
591+
```javascript
592+
zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
593+
```
594+
595+
```javascript
596+
zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]
597+
```
598+
<!-- prettier-ignore-end -->
599+
572600
#### zipWith
573601

574602
Zips given arrays together with the given function.

array/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,34 @@ Zips given arrays together into pairs.
552552
```
553553
<!-- prettier-ignore-end -->
554554

555+
# zipN
556+
557+
Zips given arrays
558+
559+
## Type signature
560+
561+
<!-- prettier-ignore-start -->
562+
```typescript
563+
(...xs: any[][]) => any[][]
564+
```
565+
<!-- prettier-ignore-end -->
566+
567+
## Examples
568+
569+
<!-- prettier-ignore-start -->
570+
```javascript
571+
zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]
572+
```
573+
574+
```javascript
575+
zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
576+
```
577+
578+
```javascript
579+
zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]
580+
```
581+
<!-- prettier-ignore-end -->
582+
555583
# zipWith
556584

557585
Zips given arrays together with the given function.

array/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import sum from "./sum.js";
3939
import take from "./take.js";
4040
import unique from "./unique.js";
4141
import zip from "./zip.js";
42+
import zipN from "./zipN.js";
4243
import zipWith from "./zipWith.js";
4344

4445
export {
@@ -83,6 +84,7 @@ export {
8384
take,
8485
unique,
8586
zip,
87+
zipN,
8688
zipWith
8789
};
8890

@@ -128,5 +130,6 @@ export default {
128130
take,
129131
unique,
130132
zip,
133+
zipN,
131134
zipWith
132135
};

array/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import sum from "./sum";
3939
import take from "./take";
4040
import unique from "./unique";
4141
import zip from "./zip";
42+
import zipN from "./zipN";
4243
import zipWith from "./zipWith";
4344

4445
export {
@@ -83,6 +84,7 @@ export {
8384
take,
8485
unique,
8586
zip,
87+
zipN,
8688
zipWith
8789
};
8890

@@ -128,5 +130,6 @@ export default {
128130
take,
129131
unique,
130132
zip,
133+
zipN,
131134
zipWith
132135
};

array/zipN.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default (...xs) => {
2+
const [head = [], ...tail] = xs;
3+
4+
return head.map((value, index) =>
5+
tail.reduce((x, xs) => [...x, xs[index]], [value])
6+
);
7+
};

array/zipN.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "zipN",
3+
"description": "Zips given arrays",
4+
"signature": "(...xs: any[][]) => any[][]",
5+
"examples": [
6+
{
7+
"language": "javascript",
8+
"content": "zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]"
9+
},
10+
{
11+
"language": "javascript",
12+
"content": "zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]"
13+
},
14+
{
15+
"language": "javascript",
16+
"content": "zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]"
17+
}
18+
],
19+
"questions": ["TODO: List questions that may this function answers."]
20+
}

array/zipN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# zipN
2+
3+
Zips given arrays
4+
5+
## Type signature
6+
7+
<!-- prettier-ignore-start -->
8+
```typescript
9+
(...xs: any[][]) => any[][]
10+
```
11+
<!-- prettier-ignore-end -->
12+
13+
## Examples
14+
15+
<!-- prettier-ignore-start -->
16+
```javascript
17+
zipN([1, 2, 3], [4, 5, 6]); // ⇒ [[1, 4], [2, 5], [3, 6]]
18+
```
19+
20+
```javascript
21+
zipN([1, 2, 3], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
22+
```
23+
24+
```javascript
25+
zipN([1, 2], [4, 5, 6], [7, 8, 9]); // ⇒ [[1, 4, 7],[2, 5, 8]]
26+
```
27+
<!-- prettier-ignore-end -->

array/zipN.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* eslint-env jest */
2+
// @ts-ignore ambiguous import
3+
import zipN from "./zipN.ts";
4+
5+
describe("zip", () => {
6+
it("should zip with pair constructor", () => {
7+
expect(zipN([1, 2, 3], [4, 5, 6])).toEqual([
8+
[1, 4],
9+
[2, 5],
10+
[3, 6]
11+
]);
12+
});
13+
14+
it("should zip up to the left arrays length", () => {
15+
expect(zipN([1, 2, 3, 4, 5, 7], [4, 5, 6])).toEqual([
16+
[1, 4],
17+
[2, 5],
18+
[3, 6],
19+
[4, undefined],
20+
[5, undefined],
21+
[7, undefined]
22+
]);
23+
24+
expect(zipN([1, 2, 3], [4, 5, 6, 4, 5, 7])).toEqual([
25+
[1, 4],
26+
[2, 5],
27+
[3, 6]
28+
]);
29+
});
30+
31+
it("should return the wrapped elements of the source array when only one array is given", () => {
32+
expect(zipN([1, 2])).toEqual([[1], [2]]);
33+
});
34+
35+
it("should return an empty array when the given array is empty", () => {
36+
expect(zipN([])).toEqual([]);
37+
});
38+
39+
it("should return an empty array when the given nothing", () => {
40+
expect(zipN()).toEqual([]);
41+
});
42+
43+
it("should zip three arrays with the same lengths", () => {
44+
expect(zipN([1, 2, 3], [4, 5, 6], [7, 8, 9])).toEqual([
45+
[1, 4, 7],
46+
[2, 5, 8],
47+
[3, 6, 9]
48+
]);
49+
});
50+
51+
it("should zip three arrays with different lengths", () => {
52+
expect(zipN([1, 2, 3], [4, 5, 6], [7, 8])).toEqual([
53+
[1, 4, 7],
54+
[2, 5, 8],
55+
[3, 6, undefined]
56+
]);
57+
58+
expect(zipN([1, 2, 3], [4, 5], [7, 8, 9])).toEqual([
59+
[1, 4, 7],
60+
[2, 5, 8],
61+
[3, undefined, 9]
62+
]);
63+
64+
expect(zipN([1, 2], [4, 5, 6], [7, 8, 9])).toEqual([
65+
[1, 4, 7],
66+
[2, 5, 8]
67+
]);
68+
});
69+
});

array/zipN.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default (...xs: any[][]) => {
2+
const [head = [], ...tail] = xs;
3+
4+
return head.map((value, index) =>
5+
tail.reduce((x, xs) => [...x, xs[index]], [value])
6+
);
7+
};

0 commit comments

Comments
 (0)