-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
978 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,135 @@ | ||
import { helpers, Context } from "../deps.ts"; | ||
import { APIParams, APIcalculator, Results } from "../lib/interface.ts"; | ||
import { Context, helpers } from "../deps.ts"; | ||
import { APIcalculator, APIParams, Results } from "../lib/interface.ts"; | ||
import * as utils from "../lib/utils.ts"; | ||
|
||
export function randomOperation (ctx: Context) { | ||
console.log("/random-operation"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation(chooseRandomOperation(), paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
const operationList: Array<string> = [ | ||
"addition", | ||
"subtraction", | ||
"multiplication", | ||
"division", | ||
"remainder", | ||
"exponent", | ||
]; | ||
|
||
export function randomOperation(ctx: Context) { | ||
console.log("/random-operation"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation(chooseRandomOperation(), paramWithoutOperation); | ||
const results: Results = calculateFromAPIcalculator(param); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function calc (ctx: Context) { | ||
console.log("/calc/:operation/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const param: APIcalculator = sanitizeParams(params); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
export function calc(ctx: Context) { | ||
console.log("/calc/:operation/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const param: APIcalculator = sanitizeParams(params); | ||
const results: Results = calculateFromAPIcalculator(param); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function addition (ctx: Context) { | ||
console.log("/addition/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation("addition", paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function subtraction (ctx: Context) { | ||
console.log("/subtraction/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation("subtraction", paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
} | ||
export function addition(ctx: Context) { | ||
const results: Results = calculateFromContextWithOperation(ctx, "addition"); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function multiplication (ctx: Context) { | ||
console.log("/multiplication/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation("multiplication", paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
} | ||
export function subtraction(ctx: Context) { | ||
const results: Results = calculateFromContextWithOperation( | ||
ctx, | ||
"subtraction", | ||
); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function division (ctx: Context) { | ||
console.log("/division/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation("division", paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
} | ||
export function multiplication(ctx: Context) { | ||
const results: Results = calculateFromContextWithOperation( | ||
ctx, | ||
"multiplication", | ||
); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function remainder (ctx: Context) { | ||
console.log("/remainder/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation("remainder", paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
} | ||
export function division(ctx: Context) { | ||
const results: Results = calculateFromContextWithOperation(ctx, "division"); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function exponent (ctx: Context) { | ||
console.log("/exponent/"); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation("exponent", paramWithoutOperation); | ||
const results: Results = calculate(param); | ||
ctx.response.body = results; | ||
} | ||
export function remainder(ctx: Context) { | ||
const results: Results = calculateFromContextWithOperation(ctx, "remainder"); | ||
ctx.response.body = results; | ||
} | ||
|
||
export function exponent(ctx: Context) { | ||
const results: Results = calculateFromContextWithOperation(ctx, "exponent"); | ||
ctx.response.body = results; | ||
} | ||
|
||
function addOperation(operation: string, param: APIcalculator): APIcalculator { | ||
const result: APIcalculator = { ...param }; | ||
result.operationPresent = checkOperationName(operation); | ||
result.operation = operation; | ||
return result; | ||
} | ||
|
||
function chooseRandomOperation(): string { | ||
const operation: string = utils.choose(...operationList); | ||
return operation; | ||
} | ||
|
||
function addOperation(operation:string, param: APIcalculator): APIcalculator { | ||
const result: APIcalculator = {...param}; | ||
result.operationPresent = true; | ||
result.operation = operation; | ||
return result; | ||
export function calculateFromContextWithOperation( | ||
ctx: Context, | ||
operation: string, | ||
): Results { | ||
console.log(`/${operation}`); | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const paramWithoutOperation: APIcalculator = sanitizeParams(params); | ||
const param = addOperation(operation, paramWithoutOperation); | ||
const results: Results = calculateFromAPIcalculator(param); | ||
return results; | ||
} | ||
|
||
function chooseRandomOperation () : string { | ||
const operation:string = utils.choose( | ||
"addition", | ||
"subtraction", | ||
"multiplication", | ||
"division", | ||
"remainder", | ||
"exponent", | ||
); | ||
return operation; | ||
function calculateFromAPIcalculator(param: APIcalculator): Results { | ||
const results: Results = { | ||
results: "", | ||
ok: false, | ||
operation: param.operation, | ||
a: param.a, | ||
b: param.b, | ||
}; | ||
|
||
if (param.aPresent && param.bPresent && param.operationPresent) { | ||
const result: number = utils.calc({ | ||
operation: param.operation, | ||
a: param.a, | ||
b: param.b, | ||
}); | ||
results.results = `${result}`; | ||
results.ok = true; | ||
} | ||
|
||
function calculate(param: APIcalculator): Results { | ||
const results: Results = { | ||
results: "", | ||
ok: false, | ||
operation: param.operation, | ||
a: param.a, | ||
b: param.b, | ||
}; | ||
|
||
if (param.aPresent && param.bPresent && param.operationPresent ) { | ||
const result: number = utils.calc({ operation: param.operation, a: param.a, b: param.b }); | ||
results.results = `${result}`; | ||
results.ok = true; | ||
} | ||
|
||
return results; | ||
return results; | ||
} | ||
|
||
function sanitizeParams(param: APIParams): APIcalculator { | ||
let operationPresent = utils.paramIsString(param.operation); | ||
const operation = utils.sanitizeParam(param.operation); | ||
operationPresent = checkOperationName(operation); | ||
const aPresent = utils.paramIsString(param.a); | ||
const a = utils.sanitizeParam(param.a); | ||
const bPresent = utils.paramIsString(param.b); | ||
const b = utils.sanitizeParam(param.b); | ||
const result: APIcalculator = { | ||
operationPresent, | ||
operation, | ||
aPresent, | ||
a, | ||
bPresent, | ||
b, | ||
}; | ||
return result; | ||
} | ||
|
||
function sanitizeParams(param: APIParams): APIcalculator{ | ||
// TODO: check operation name | ||
const operationPresent = utils.paramIsString(param.operation); | ||
const operation = utils.sanitizeParam(param.operation); | ||
const aPresent = utils.paramIsString(param.a); | ||
const a = utils.sanitizeParam(param.a); | ||
const bPresent = utils.paramIsString(param.b); | ||
const b = utils.sanitizeParam(param.b); | ||
const result: APIcalculator = { | ||
operationPresent, | ||
operation, | ||
aPresent, | ||
a, | ||
bPresent, | ||
b | ||
}; | ||
return result; | ||
function checkOperationName(operation: string): boolean { | ||
const result: boolean = operationList.includes(operation); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,37 @@ | ||
import { helpers, Context } from "../deps.ts"; | ||
import { Context, helpers } from "../deps.ts"; | ||
import { APIParams, Results } from "../lib/interface.ts"; | ||
import { choose } from "../lib/utils.ts"; | ||
|
||
export function message(ctx: Context) { | ||
const results: Results = { results: "", ok: false, operation: "", a: "", b: "" }; | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
const results: Results = { | ||
results: "", | ||
ok: false, | ||
operation: "", | ||
a: "", | ||
b: "", | ||
}; | ||
const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); | ||
|
||
if (params?.first && params?.second) { | ||
console.log(`/${params.first} ${params.second}`); | ||
results.results = `${params.first} ${params.second}`; | ||
results.ok = true; | ||
} else { | ||
const message: string = randomMessage(); | ||
console.log(`/${message}`); | ||
results.results = `${message}`; | ||
results.ok = true; | ||
} | ||
ctx.response.body = results; | ||
if (params?.first && params?.second) { | ||
console.log(`/${params.first} ${params.second}`); | ||
results.results = `${params.first} ${params.second}`; | ||
results.ok = true; | ||
} else { | ||
const message: string = randomMessage(); | ||
console.log(`/${message}`); | ||
results.results = `${message}`; | ||
results.ok = true; | ||
} | ||
ctx.response.body = results; | ||
} | ||
|
||
function randomMessage() : string { | ||
const message: string = choose( | ||
"Hello world", | ||
"Hola món", | ||
"Hallo Welt", | ||
"Bonjour le monde", | ||
"Ciao Mondo" | ||
); | ||
return message; | ||
} | ||
function randomMessage(): string { | ||
const message: string = choose( | ||
"Hello world", | ||
"Hola món", | ||
"Hallo Welt", | ||
"Bonjour le monde", | ||
"Ciao Mondo", | ||
); | ||
return message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
export { Application, Context, helpers, Router } from "https://deno.land/x/oak/mod.ts"; | ||
export { | ||
Application, | ||
Context, | ||
helpers, | ||
Router, | ||
} from "https://deno.land/x/oak/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
export interface APIParams { | ||
operation?: string, | ||
a?: string, | ||
b?: string, | ||
first?: string, | ||
second?: string, | ||
operation?: string; | ||
a?: string; | ||
b?: string; | ||
first?: string; | ||
second?: string; | ||
} | ||
|
||
export interface APIcalculator { | ||
operationPresent: boolean, | ||
operation: string, | ||
aPresent: boolean, | ||
a: string, | ||
bPresent: boolean, | ||
b: string, | ||
operationPresent: boolean; | ||
operation: string; | ||
aPresent: boolean; | ||
a: string; | ||
bPresent: boolean; | ||
b: string; | ||
} | ||
|
||
export interface Results { | ||
results: string, | ||
ok: boolean, | ||
operation: string, | ||
a: string, | ||
b: string | ||
} | ||
results: string; | ||
ok: boolean; | ||
operation: string; | ||
a: string; | ||
b: string; | ||
} |
Oops, something went wrong.