Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): add new method random on num class #25

Merged
merged 1 commit into from
Jan 26, 2022
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
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ What kind of change does this PR introduce?

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

Issue Number: N/A
Issue Num: N/A

## What is the new behavior?

Expand Down
1 change: 1 addition & 0 deletions lib/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './str.exception';
export * from './num.exception';
11 changes: 11 additions & 0 deletions lib/exceptions/num.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class NumException extends Error {
name: 'NumException';

/**
* @constructor
* @param {string} message
*/
constructor(message: string) {
super(message);
}
}
3 changes: 3 additions & 0 deletions lib/num/num.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface INumber {
random(min: number, max: number): number;
}
35 changes: 35 additions & 0 deletions lib/num/num.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Num } from './num';
import { NumException } from '../exceptions';

describe('Number', () => {
let number: Num;

beforeEach(async () => {
number = new Num();
});

it('should be defined', () => {
expect(number).toBeDefined();
});

describe('Random method', () => {
it('should have method random', () => {
expect(number).toHaveProperty('random');
});

it('Use random method', () => {
expect(number.random(5)).not.toBeNull();
expect(number.random(5)).not.toBeNaN();
expect(number.random(5)).toBeGreaterThanOrEqual(0);
expect(number.random(5)).toBeLessThanOrEqual(5);
});

it('should have method random and return value', () => {
number.random = jest.fn().mockReturnValue(5);

expect(number.random(5)).not.toBeNull();
expect(number.random(5)).not.toBeNaN();
expect(number.random(5)).toEqual(5);
});
});
});
16 changes: 16 additions & 0 deletions lib/num/num.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { INumber } from './num.interface';

export class Num implements INumber {
/**
* Random a number.
* @param min
* @param max
*/
random(min: number, max?: number): number {
if (max) {
return Math.floor(Math.random() * (max - min)) + min;
}

return Math.floor(Math.random() * (min + 1));
}
}