|
1 |
| -import { helpers, Context } from "../deps.ts"; |
2 |
| -import { APIParams, APIcalculator, Results } from "../lib/interface.ts"; |
| 1 | +import { Context, helpers } from "../deps.ts"; |
| 2 | +import { APIcalculator, APIParams, Results } from "../lib/interface.ts"; |
3 | 3 | import * as utils from "../lib/utils.ts";
|
4 | 4 |
|
5 |
| -export function randomOperation (ctx: Context) { |
6 |
| - console.log("/random-operation"); |
7 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
8 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
9 |
| - const param = addOperation(chooseRandomOperation(), paramWithoutOperation); |
10 |
| - const results: Results = calculate(param); |
11 |
| - ctx.response.body = results; |
| 5 | +const operationList: Array<string> = [ |
| 6 | + "addition", |
| 7 | + "subtraction", |
| 8 | + "multiplication", |
| 9 | + "division", |
| 10 | + "remainder", |
| 11 | + "exponent", |
| 12 | +]; |
| 13 | + |
| 14 | +export function randomOperation(ctx: Context) { |
| 15 | + console.log("/random-operation"); |
| 16 | + const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
| 17 | + const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
| 18 | + const param = addOperation(chooseRandomOperation(), paramWithoutOperation); |
| 19 | + const results: Results = calculateFromAPIcalculator(param); |
| 20 | + ctx.response.body = results; |
12 | 21 | }
|
13 | 22 |
|
14 |
| -export function calc (ctx: Context) { |
15 |
| - console.log("/calc/:operation/"); |
16 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
17 |
| - const param: APIcalculator = sanitizeParams(params); |
18 |
| - const results: Results = calculate(param); |
19 |
| - ctx.response.body = results; |
| 23 | +export function calc(ctx: Context) { |
| 24 | + console.log("/calc/:operation/"); |
| 25 | + const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
| 26 | + const param: APIcalculator = sanitizeParams(params); |
| 27 | + const results: Results = calculateFromAPIcalculator(param); |
| 28 | + ctx.response.body = results; |
20 | 29 | }
|
21 | 30 |
|
22 |
| -export function addition (ctx: Context) { |
23 |
| - console.log("/addition/"); |
24 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
25 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
26 |
| - const param = addOperation("addition", paramWithoutOperation); |
27 |
| - const results: Results = calculate(param); |
28 |
| - ctx.response.body = results; |
29 |
| - } |
30 |
| - |
31 |
| - export function subtraction (ctx: Context) { |
32 |
| - console.log("/subtraction/"); |
33 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
34 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
35 |
| - const param = addOperation("subtraction", paramWithoutOperation); |
36 |
| - const results: Results = calculate(param); |
37 |
| - ctx.response.body = results; |
38 |
| - } |
| 31 | +export function addition(ctx: Context) { |
| 32 | + const results: Results = calculateFromContextWithOperation(ctx, "addition"); |
| 33 | + ctx.response.body = results; |
| 34 | +} |
39 | 35 |
|
40 |
| - export function multiplication (ctx: Context) { |
41 |
| - console.log("/multiplication/"); |
42 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
43 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
44 |
| - const param = addOperation("multiplication", paramWithoutOperation); |
45 |
| - const results: Results = calculate(param); |
46 |
| - ctx.response.body = results; |
47 |
| - } |
| 36 | +export function subtraction(ctx: Context) { |
| 37 | + const results: Results = calculateFromContextWithOperation( |
| 38 | + ctx, |
| 39 | + "subtraction", |
| 40 | + ); |
| 41 | + ctx.response.body = results; |
| 42 | +} |
48 | 43 |
|
49 |
| - export function division (ctx: Context) { |
50 |
| - console.log("/division/"); |
51 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
52 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
53 |
| - const param = addOperation("division", paramWithoutOperation); |
54 |
| - const results: Results = calculate(param); |
55 |
| - ctx.response.body = results; |
56 |
| - } |
| 44 | +export function multiplication(ctx: Context) { |
| 45 | + const results: Results = calculateFromContextWithOperation( |
| 46 | + ctx, |
| 47 | + "multiplication", |
| 48 | + ); |
| 49 | + ctx.response.body = results; |
| 50 | +} |
57 | 51 |
|
58 |
| - export function remainder (ctx: Context) { |
59 |
| - console.log("/remainder/"); |
60 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
61 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
62 |
| - const param = addOperation("remainder", paramWithoutOperation); |
63 |
| - const results: Results = calculate(param); |
64 |
| - ctx.response.body = results; |
65 |
| - } |
| 52 | +export function division(ctx: Context) { |
| 53 | + const results: Results = calculateFromContextWithOperation(ctx, "division"); |
| 54 | + ctx.response.body = results; |
| 55 | +} |
66 | 56 |
|
67 |
| - export function exponent (ctx: Context) { |
68 |
| - console.log("/exponent/"); |
69 |
| - const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
70 |
| - const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
71 |
| - const param = addOperation("exponent", paramWithoutOperation); |
72 |
| - const results: Results = calculate(param); |
73 |
| - ctx.response.body = results; |
74 |
| - } |
| 57 | +export function remainder(ctx: Context) { |
| 58 | + const results: Results = calculateFromContextWithOperation(ctx, "remainder"); |
| 59 | + ctx.response.body = results; |
| 60 | +} |
75 | 61 |
|
| 62 | +export function exponent(ctx: Context) { |
| 63 | + const results: Results = calculateFromContextWithOperation(ctx, "exponent"); |
| 64 | + ctx.response.body = results; |
| 65 | +} |
76 | 66 |
|
| 67 | +function addOperation(operation: string, param: APIcalculator): APIcalculator { |
| 68 | + const result: APIcalculator = { ...param }; |
| 69 | + result.operationPresent = checkOperationName(operation); |
| 70 | + result.operation = operation; |
| 71 | + return result; |
| 72 | +} |
77 | 73 |
|
| 74 | +function chooseRandomOperation(): string { |
| 75 | + const operation: string = utils.choose(...operationList); |
| 76 | + return operation; |
| 77 | +} |
78 | 78 |
|
79 |
| -function addOperation(operation:string, param: APIcalculator): APIcalculator { |
80 |
| - const result: APIcalculator = {...param}; |
81 |
| - result.operationPresent = true; |
82 |
| - result.operation = operation; |
83 |
| - return result; |
| 79 | +export function calculateFromContextWithOperation( |
| 80 | + ctx: Context, |
| 81 | + operation: string, |
| 82 | +): Results { |
| 83 | + console.log(`/${operation}`); |
| 84 | + const params: APIParams = helpers.getQuery(ctx, { mergeParams: true }); |
| 85 | + const paramWithoutOperation: APIcalculator = sanitizeParams(params); |
| 86 | + const param = addOperation(operation, paramWithoutOperation); |
| 87 | + const results: Results = calculateFromAPIcalculator(param); |
| 88 | + return results; |
84 | 89 | }
|
85 | 90 |
|
86 |
| -function chooseRandomOperation () : string { |
87 |
| - const operation:string = utils.choose( |
88 |
| - "addition", |
89 |
| - "subtraction", |
90 |
| - "multiplication", |
91 |
| - "division", |
92 |
| - "remainder", |
93 |
| - "exponent", |
94 |
| - ); |
95 |
| - return operation; |
| 91 | +function calculateFromAPIcalculator(param: APIcalculator): Results { |
| 92 | + const results: Results = { |
| 93 | + results: "", |
| 94 | + ok: false, |
| 95 | + operation: param.operation, |
| 96 | + a: param.a, |
| 97 | + b: param.b, |
| 98 | + }; |
| 99 | + |
| 100 | + if (param.aPresent && param.bPresent && param.operationPresent) { |
| 101 | + const result: number = utils.calc({ |
| 102 | + operation: param.operation, |
| 103 | + a: param.a, |
| 104 | + b: param.b, |
| 105 | + }); |
| 106 | + results.results = `${result}`; |
| 107 | + results.ok = true; |
96 | 108 | }
|
97 | 109 |
|
98 |
| - function calculate(param: APIcalculator): Results { |
99 |
| - const results: Results = { |
100 |
| - results: "", |
101 |
| - ok: false, |
102 |
| - operation: param.operation, |
103 |
| - a: param.a, |
104 |
| - b: param.b, |
105 |
| - }; |
106 |
| - |
107 |
| - if (param.aPresent && param.bPresent && param.operationPresent ) { |
108 |
| - const result: number = utils.calc({ operation: param.operation, a: param.a, b: param.b }); |
109 |
| - results.results = `${result}`; |
110 |
| - results.ok = true; |
111 |
| - } |
112 |
| - |
113 |
| - return results; |
| 110 | + return results; |
| 111 | +} |
| 112 | + |
| 113 | +function sanitizeParams(param: APIParams): APIcalculator { |
| 114 | + let operationPresent = utils.paramIsString(param.operation); |
| 115 | + const operation = utils.sanitizeParam(param.operation); |
| 116 | + operationPresent = checkOperationName(operation); |
| 117 | + const aPresent = utils.paramIsString(param.a); |
| 118 | + const a = utils.sanitizeParam(param.a); |
| 119 | + const bPresent = utils.paramIsString(param.b); |
| 120 | + const b = utils.sanitizeParam(param.b); |
| 121 | + const result: APIcalculator = { |
| 122 | + operationPresent, |
| 123 | + operation, |
| 124 | + aPresent, |
| 125 | + a, |
| 126 | + bPresent, |
| 127 | + b, |
| 128 | + }; |
| 129 | + return result; |
114 | 130 | }
|
115 | 131 |
|
116 |
| -function sanitizeParams(param: APIParams): APIcalculator{ |
117 |
| - // TODO: check operation name |
118 |
| - const operationPresent = utils.paramIsString(param.operation); |
119 |
| - const operation = utils.sanitizeParam(param.operation); |
120 |
| - const aPresent = utils.paramIsString(param.a); |
121 |
| - const a = utils.sanitizeParam(param.a); |
122 |
| - const bPresent = utils.paramIsString(param.b); |
123 |
| - const b = utils.sanitizeParam(param.b); |
124 |
| - const result: APIcalculator = { |
125 |
| - operationPresent, |
126 |
| - operation, |
127 |
| - aPresent, |
128 |
| - a, |
129 |
| - bPresent, |
130 |
| - b |
131 |
| - }; |
132 |
| - return result; |
| 132 | +function checkOperationName(operation: string): boolean { |
| 133 | + const result: boolean = operationList.includes(operation); |
| 134 | + return result; |
133 | 135 | }
|
0 commit comments