Skip to content

Commit 1255b2e

Browse files
authored
feat(): add new method random on num class (#25)
1 parent 3563b68 commit 1255b2e

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ What kind of change does this PR introduce?
2424

2525
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
2626

27-
Issue Number: N/A
27+
Issue Num: N/A
2828

2929
## What is the new behavior?
3030

lib/exceptions/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './str.exception';
2+
export * from './num.exception';

lib/exceptions/num.exception.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class NumException extends Error {
2+
name: 'NumException';
3+
4+
/**
5+
* @constructor
6+
* @param {string} message
7+
*/
8+
constructor(message: string) {
9+
super(message);
10+
}
11+
}

lib/num/num.interface.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface INumber {
2+
random(min: number, max: number): number;
3+
}

lib/num/num.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Num } from './num';
2+
import { NumException } from '../exceptions';
3+
4+
describe('Number', () => {
5+
let number: Num;
6+
7+
beforeEach(async () => {
8+
number = new Num();
9+
});
10+
11+
it('should be defined', () => {
12+
expect(number).toBeDefined();
13+
});
14+
15+
describe('Random method', () => {
16+
it('should have method random', () => {
17+
expect(number).toHaveProperty('random');
18+
});
19+
20+
it('Use random method', () => {
21+
expect(number.random(5)).not.toBeNull();
22+
expect(number.random(5)).not.toBeNaN();
23+
expect(number.random(5)).toBeGreaterThanOrEqual(0);
24+
expect(number.random(5)).toBeLessThanOrEqual(5);
25+
});
26+
27+
it('should have method random and return value', () => {
28+
number.random = jest.fn().mockReturnValue(5);
29+
30+
expect(number.random(5)).not.toBeNull();
31+
expect(number.random(5)).not.toBeNaN();
32+
expect(number.random(5)).toEqual(5);
33+
});
34+
});
35+
});

lib/num/num.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { INumber } from './num.interface';
2+
3+
export class Num implements INumber {
4+
/**
5+
* Random a number.
6+
* @param min
7+
* @param max
8+
*/
9+
random(min: number, max?: number): number {
10+
if (max) {
11+
return Math.floor(Math.random() * (max - min)) + min;
12+
}
13+
14+
return Math.floor(Math.random() * (min + 1));
15+
}
16+
}

0 commit comments

Comments
 (0)