Skip to content

Commit 62be70e

Browse files
MarcusTXKAnonymxtrix
authored andcommitted
Add tests for Repeat bundle
1 parent 114fe18 commit 62be70e

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/bundles/repeat/__tests__/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Repeat from '../index';
2+
3+
// Destructure functions exported from bundle
4+
const { repeat, twice, thrice } = Repeat();
5+
6+
// Test functions
7+
test('repeat works correctly and repeats function n times', () => {
8+
expect(repeat((x: number) => x + 1, 5)(1)).toBe(6);
9+
});
10+
11+
test('twice works correctly and repeats function twice', () => {
12+
expect(twice((x: number) => x + 1)(1)).toBe(3);
13+
});
14+
15+
test('thrice works correctly and repeats function thrice', () => {
16+
expect(thrice((x: number) => x + 1)(1)).toBe(4);
17+
});

src/bundles/repeat/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,38 @@
44
* @author Tang Xin Kye, Marcus
55
*/
66

7-
// eslint-disable-next-line no-underscore-dangle
8-
function repeat(f: Function, n: any) {
7+
/**
8+
* Repeats function n times
9+
*
10+
* @param f - function to be repeated
11+
* @param n - number of times to be repeated
12+
* @return a function repeated n times
13+
*/
14+
function repeat(f: Function, n: any): Function {
915
return n === 0 ? (x: any) => x : (x: any) => f(repeat(f, n - 1)(x));
1016
}
1117

12-
function twice(f: any) {
18+
/**
19+
* Repeats function twice
20+
*
21+
* @param f - function to be repeated
22+
* @return a function repeated twice
23+
*/
24+
function twice(f: Function): Function {
1325
return repeat(f, 2);
1426
}
1527

16-
function thrice(f: any) {
28+
/**
29+
* Repeats function thrice
30+
*
31+
* @param f - function to be repeated
32+
* @return a function repeated thrice
33+
*/
34+
function thrice(f: Function): Function {
1735
return repeat(f, 3);
1836
}
1937

38+
// exported functions from the bundle
2039
export default () => ({
2140
repeat,
2241
twice,

0 commit comments

Comments
 (0)