Skip to content

Commit

Permalink
Merge pull request #30 from karlthomas3/main
Browse files Browse the repository at this point in the history
Fixed implement input validation for division endpoint
  • Loading branch information
Unisergius authored Mar 13, 2024
2 parents 1ccb8bd + 2740d4f commit 4dae393
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
8 changes: 6 additions & 2 deletions __test__/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/** @format */

import {
addTwoNumbers,
divideTwoNumbers,
powerTwoNumbers,
multiplyTwoNumbers,
subtractTwoNumbers
subtractTwoNumbers,
} from '../math.handler';

/*
Expand All @@ -22,7 +24,8 @@ describe('test math handler', (): void => {
// number 3 using this function will result in the number 32. This is a
// very crude test to test our function as it could just be always giving
// out the number 32!
let a = 29, b = 3;
let a = 29,
b = 3;
let result = addTwoNumbers(a, b);
// The expect function will return a Jest object with the result we gave it
// to actually test if the result we gave it is what we want, we need to use
Expand All @@ -38,6 +41,7 @@ describe('test math handler', (): void => {
// The following tests have been collapsed for brevety
test('divide two numbers', (): void => {
expect(divideTwoNumbers(10, 2)).toBe(5);
expect(divideTwoNumbers(1, 0)).toBe(NaN);
});

test('sub two numbers', (): void => {
Expand Down
51 changes: 31 additions & 20 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/** @format */

import express, { Express, Request, Response } from 'express';
// Import our library
import {
addTwoNumbers,
divideTwoNumbers,
subtractTwoNumbers,
powerTwoNumbers,
multiplyTwoNumbers,
subtractTwoNumbers
} from './math.handler';

/*
Expand Down Expand Up @@ -64,7 +66,7 @@ app.get('/', (_req: Request, res: Response) => {
});
});

// This GET route is very flexible, it will answer any request going to
// This GET route is very flexible, it will answer any request going to
// `/sum/*/*` and assign the wildcards into the parameters with the key given
app.get('/sum/:a/:b', (req: Request, res: Response) => {
// Extract the request parameters
Expand All @@ -78,21 +80,30 @@ app.get('/sum/:a/:b', (req: Request, res: Response) => {
operation: 'success',
a,
b,
sum
sum,
});
});

// Handler for the division route.
app.get('/div/:a/:b', (req: Request, res: Response) => {
const { a, b } = { a: req.params.a, b: req.params.b };
const division = divideTwoNumbers(Number(a), Number(b));
res.json({
message: 'Div Operation',
operation: 'success',
a,
b,
c: division
});
if (b == '0') {
res.json({
message: 'You cannot divide by zero',
operation: 'failure',
a,
b,
});
} else {
const division = divideTwoNumbers(Number(a), Number(b));
res.json({
message: 'Div Operation',
operation: 'success',
a,
b,
c: division,
});
}
});

// Handler for the subtraction route
Expand All @@ -111,14 +122,14 @@ subtraction
// Handler for the exponencial route
app.get('/power/:a/:b', (req: Request, res: Response) => {
const { a, b } = { a: Number(req.params.a), b: Number(req.params.b) };
const exponencial = powerTwoNumbers(Number(a), Number(b));
res.json({
message: 'Power Operation',
operation: 'success',
a,
b,
exponencial
});
const exponencial = powerTwoNumbers(Number(a), Number(b));
res.json({
message: 'Power Operation',
operation: 'success',
a,
b,
exponencial,
});
});
// Handler for the multiply endpoint
app.get('/multiply/:a/:b', (req: Request, res: Response) => {
Expand All @@ -129,7 +140,7 @@ app.get('/multiply/:a/:b', (req: Request, res: Response) => {
operation: 'success',
a,
b,
c: multiplication
c: multiplication,
});
});

Expand Down
26 changes: 15 additions & 11 deletions math.handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/*
This file contains our wonderful maths library
@see Math documentation: https://en.wikipedia.org/wiki/Mathematics
*/
/**
* This file contains our wonderful maths library
*
* @format
* @see Math documentation: https://en.wikipedia.org/wiki/Mathematics
*/

const addTwoNumbers = (a: number, b: number): number => a + b;

const divideTwoNumbers = (a: number, b: number): number => a / b;
const divideTwoNumbers = (a: number, b: number): number => {
if (b == 0) return NaN;
return a / b;
};

const subtractTwoNumbers = (a: number, b: number): number => a - b;

Expand All @@ -15,9 +19,9 @@ const powerTwoNumbers = (a: number, b: number): number => a ** b;
const multiplyTwoNumbers = (a: number, b: number): number => a * b;

export {
addTwoNumbers,
divideTwoNumbers,
powerTwoNumbers,
multiplyTwoNumbers,
subtractTwoNumbers
addTwoNumbers,
divideTwoNumbers,
powerTwoNumbers,
multiplyTwoNumbers,
subtractTwoNumbers,
};

0 comments on commit 4dae393

Please sign in to comment.