Skip to content

Commit 77f6aa1

Browse files
committed
refactor: restructure fn and use better naming
1 parent 11cf527 commit 77f6aa1

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

Diff for: backend/src/command-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Logger } from "pino";
22
import { CommandAndResult } from "./entities/command-result.entity";
33
import { IRepository } from "./repositories";
4-
import { isValidCommand, evaluate } from "./math/evaluate";
4+
import { evaluate } from "./math/evaluate";
55

66
export interface ICommandService {
77
evaluateAndSave(clientId: string, expression: string): Promise<string>;

Diff for: backend/src/math/evaluate.ts

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
import { Summand } from './types';
22
const operators = new Set(['+', '-', '*', '/']);
33

4-
// isValidCommand checks if the command is a allowed mathematical expression
5-
function isValidCommand(s: string): boolean {
6-
s = s.replace(/ /g,''); // remove all spaces
7-
if (s.length === 0) {
4+
// isValidOperation checks if the operation is a allowed mathematical expression
5+
function isValidOperation(operation: string): boolean {
6+
const cleanedOperation = operation.replace(/ /g,''); // remove all spaces
7+
if (cleanedOperation.length === 0) {
88
return false;
99
}
10-
// allowed characters: 0-9, +, -, *, /, .
11-
const regex = /^[\d+\-*/.]+$/;
12-
if (!regex.test(s)) {
10+
if (!allowedChars(cleanedOperation)) {
1311
return false;
1412
}
15-
// operators cannot be adjacent to each other
16-
for (let i = 0; i < s.length - 1; i++) {
17-
if (operators.has(s[i]) && operators.has(s[i + 1])) {
18-
return false;
19-
}
13+
if (!noAdjacentOperators(cleanedOperation)) {
14+
return false;
2015
}
21-
// operators cannot be at the end of the string
22-
if (operators.has(s[s.length - 1])) {
16+
if (noOperatorsAtTheEnd(cleanedOperation)) {
2317
return false;
2418
}
2519
return true;
2620
}
2721

22+
function allowedChars(operation: string): boolean {
23+
return /^[\d+\-*/.]+$/.test(operation);
24+
}
25+
26+
function noAdjacentOperators(operation: string): boolean {
27+
for (let i = 0; i < operation.length - 1; i++) {
28+
if (operators.has(operation[i]) && operators.has(operation[i + 1])) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
}
34+
35+
function noOperatorsAtTheEnd(operation: string): boolean {
36+
return operators.has(operation[operation.length - 1]);
37+
}
38+
2839
// evaluate evaluate a mathematical expression
2940
function evaluate(s: string): string {
30-
if (!isValidCommand(s)) {
41+
if (!isValidOperation(s)) {
3142
throw new Error('Invalid command');
3243
}
3344
s = s.replace(/ /g,''); // remove all spaces
@@ -72,4 +83,4 @@ const parseExpression = (s: string): Summand[] => {
7283
return summands;
7384
}
7485

75-
export { isValidCommand, evaluate };
86+
export { evaluate };

Diff for: backend/src/math/types.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class Fraction {
7979
}
8080

8181
public add(f: Fraction): Fraction {
82-
const commonDenominator = getLcm(this.denominator, f.denominator);
82+
const commonDenominator = getLowestCommonMultiple(this.denominator, f.denominator);
8383
const multiplyThis = commonDenominator.div(this.denominator);
8484
const multiplyF = commonDenominator.div(f.denominator);
8585
this.numerator = this.numerator.mul(multiplyThis);
@@ -95,16 +95,16 @@ export class Fraction {
9595
}
9696
}
9797

98-
function getLcm(a: Decimal, b: Decimal): Decimal {
99-
return a.mul(b).div(getGcd(a, b));
98+
function getLowestCommonMultiple(a: Decimal, b: Decimal): Decimal {
99+
return a.mul(b).div(getGreatestCommonDivisor(a, b));
100100
}
101101

102-
function getGcd(a: Decimal, b: Decimal): Decimal {
102+
function getGreatestCommonDivisor(a: Decimal, b: Decimal): Decimal {
103103
const max = Decimal.max(a, b);
104104
const min = Decimal.min(a, b);
105105
if (max.mod(min).eq(0)) {
106106
return min;
107107
} else {
108-
return getGcd(max.mod(min), min);
108+
return getGreatestCommonDivisor(max.mod(min), min);
109109
}
110110
}

0 commit comments

Comments
 (0)