Skip to content

Commit 1962442

Browse files
Add array/length (#68)
* Add length method for arrays * Add tests for length method * Update questions and add additional question * Separate some test cases Co-authored-by: Krystian Boruciński <krystian.borucinski@sandstream.pl>
1 parent 2962e11 commit 1962442

File tree

9 files changed

+160
-0
lines changed

9 files changed

+160
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,44 @@ last([]);
553553

554554
- How to get the last element of an array?
555555

556+
#### length
557+
558+
Returns the number of elements in the given array.
559+
560+
##### Type signature
561+
562+
<!-- prettier-ignore-start -->
563+
```typescript
564+
(xs: any[]) => number
565+
```
566+
<!-- prettier-ignore-end -->
567+
568+
##### Examples
569+
570+
<!-- prettier-ignore-start -->
571+
```javascript
572+
length([true, 1]);
573+
// ⇒ 2
574+
```
575+
576+
```javascript
577+
length([1, 2, 3]);
578+
// ⇒ 3
579+
```
580+
581+
```javascript
582+
length([]);
583+
// ⇒ 0
584+
```
585+
<!-- prettier-ignore-end -->
586+
587+
##### Questions
588+
589+
- How to check an array's length?
590+
- How to compute an array's length?
591+
- How to check the size of an array?
592+
- How to check the number of elements in an array?
593+
556594
#### lengthDiffers
557595

558596
Checks if lengths of the given arrays differ.

array/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,44 @@ last([]);
536536

537537
- How to get the last element of an array?
538538

539+
# length
540+
541+
Returns the number of elements in the given array.
542+
543+
## Type signature
544+
545+
<!-- prettier-ignore-start -->
546+
```typescript
547+
(xs: any[]) => number
548+
```
549+
<!-- prettier-ignore-end -->
550+
551+
## Examples
552+
553+
<!-- prettier-ignore-start -->
554+
```javascript
555+
length([true, 1]);
556+
// ⇒ 2
557+
```
558+
559+
```javascript
560+
length([1, 2, 3]);
561+
// ⇒ 3
562+
```
563+
564+
```javascript
565+
length([]);
566+
// ⇒ 0
567+
```
568+
<!-- prettier-ignore-end -->
569+
570+
## Questions
571+
572+
- How to check an array's length?
573+
- How to compute an array's length?
574+
- How to check the size of an array?
575+
- How to check the number of elements in an array?
576+
539577
# lengthDiffers
540578

541579
Checks if lengths of the given arrays differ.

array/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import insert from "./insert.js";
1616
import intersection from "./intersection.js";
1717
import is from "./is.js";
1818
import last from "./last.js";
19+
import length from "./length.js";
1920
import lengthDiffers from "./lengthDiffers.js";
2021
import map from "./map.js";
2122
import midpoint from "./midpoint.js";
@@ -65,6 +66,7 @@ export {
6566
intersection,
6667
is,
6768
last,
69+
length,
6870
lengthDiffers,
6971
map,
7072
midpoint,
@@ -115,6 +117,7 @@ export default {
115117
intersection,
116118
is,
117119
last,
120+
length,
118121
lengthDiffers,
119122
map,
120123
midpoint,

array/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import insert from "./insert";
1616
import intersection from "./intersection";
1717
import is from "./is";
1818
import last from "./last";
19+
import length from "./length";
1920
import lengthDiffers from "./lengthDiffers";
2021
import map from "./map";
2122
import midpoint from "./midpoint";
@@ -65,6 +66,7 @@ export {
6566
intersection,
6667
is,
6768
last,
69+
length,
6870
lengthDiffers,
6971
map,
7072
midpoint,
@@ -115,6 +117,7 @@ export default {
115117
intersection,
116118
is,
117119
last,
120+
length,
118121
lengthDiffers,
119122
map,
120123
midpoint,

array/length.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default xs => xs.length;

array/length.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "length",
3+
"description": "Returns the number of elements in the given array.",
4+
"signature": "(xs: any[]) => number",
5+
"examples": [
6+
{
7+
"language": "javascript",
8+
"content": "length([true, 1]);\n// ⇒ 2"
9+
},
10+
{
11+
"language": "javascript",
12+
"content": "length([1, 2, 3]);\n// ⇒ 3"
13+
},
14+
{
15+
"language": "javascript",
16+
"content": "length([]);\n// ⇒ 0"
17+
}
18+
],
19+
"questions": [
20+
"How to check an array's length?",
21+
"How to compute an array's length?",
22+
"How to check the size of an array?",
23+
"How to check the number of elements in an array?"
24+
]
25+
}

array/length.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# length
2+
3+
Returns the number of elements in the given array.
4+
5+
## Type signature
6+
7+
<!-- prettier-ignore-start -->
8+
```typescript
9+
(xs: any[]) => number
10+
```
11+
<!-- prettier-ignore-end -->
12+
13+
## Examples
14+
15+
<!-- prettier-ignore-start -->
16+
```javascript
17+
length([true, 1]);
18+
// ⇒ 2
19+
```
20+
21+
```javascript
22+
length([1, 2, 3]);
23+
// ⇒ 3
24+
```
25+
26+
```javascript
27+
length([]);
28+
// ⇒ 0
29+
```
30+
<!-- prettier-ignore-end -->
31+
32+
## Questions
33+
34+
- How to check an array's length?
35+
- How to compute an array's length?
36+
- How to check the size of an array?
37+
- How to check the number of elements in an array?

array/length.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-env jest */
2+
// @ts-ignore ambiguous import
3+
import length from "./length";
4+
5+
describe("length", () => {
6+
it("returns the length of the given array", () => {
7+
expect(length([1, 2, 3])).toEqual(3);
8+
expect(length([6, 7])).toEqual(2);
9+
});
10+
11+
it("returns zero when the given array is empty", () => {
12+
expect(length([])).toEqual(0);
13+
});
14+
});

array/length.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default (xs: any[]): number => xs.length;

0 commit comments

Comments
 (0)