-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- process business logic: command evalutaion - unit test command evaluation - setup repo interface
- Loading branch information
Showing
5 changed files
with
227 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import CommandService from './command-service'; | ||
import { pino } from 'pino'; | ||
|
||
const mockRepository = { | ||
saveCommand: jest.fn(), | ||
getLatest: jest.fn(), | ||
}; | ||
|
||
describe('evaluate', () => { | ||
describe.each([ | ||
{ command: '', hasError: true }, | ||
{ command: ' ', hasError: true }, | ||
{ command: '1', expected: '1' }, | ||
{ command: '1+', hasError: true }, | ||
{ command: '2 + + 1', hasError: true }, | ||
|
||
{ command: '1 + 1', expected: '2' }, | ||
{ command: '123 * 23 + 45 - 6', expected: '2868' }, | ||
{ command: '10 * 5 / 2', expected: '25' }, | ||
{ command: '10.5 * 5 / 2.5', expected: '21' }, | ||
|
||
{ command: '1a2b3c', hasError: true }, | ||
{ command: '1 + 2 = 3', hasError: true }, | ||
{ command: '(123 * 23) + 45 - 6', hasError: true }, | ||
|
||
{ command: '1 + 1', expected: '2' }, | ||
{ command: '123 * 23 + 45 - 6', expected: '2868' }, | ||
{ command: '10 * 5 / 2', expected: '25' }, | ||
{ command: '10.54 * 7 / 3', expected: '24.593333333333334' }, | ||
{ command: '5 * 7 / 3 * 3', expected: '35' }, | ||
|
||
])('.evaluate($command)', ({ command, hasError, expected }) => { | ||
test(`returns ${hasError}`, () => { | ||
const clientId = 'whatever'; | ||
const commandService = new CommandService(pino(), mockRepository); | ||
if (hasError) { | ||
expect(() => commandService.evaluateAndSave(clientId, command)).toThrow('Invalid command'); | ||
} else { | ||
expect(commandService.evaluateAndSave(clientId, command)).toBe(expected); | ||
expect(mockRepository.saveCommand).toHaveBeenCalledWith(clientId, command, expected); | ||
} | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Logger } from "pino"; | ||
import { evaluate as evaluateMathjs } from 'mathjs'; | ||
import { CommandAndResult } from "./entities/command-result.entity"; | ||
|
||
interface IRepository { | ||
saveCommand(clientId: string, command: string, result: string): void; | ||
getLatest(clientId: string, count: number): CommandAndResult[]; | ||
} | ||
|
||
const historyCount = 10; | ||
|
||
class CommandService { | ||
private operators = new Set(['+', '-', '*', '/']); | ||
private repository: IRepository; | ||
private logger: Logger; | ||
constructor(logger: Logger, repository: IRepository) { | ||
this.logger = logger; | ||
this.repository = repository; | ||
} | ||
|
||
// evaluateAndSave evaluates a mathematical expression and saves the result to the repository | ||
public evaluateAndSave(clientId: string, expression: string): string { | ||
const result = this.evaluate(expression); | ||
this.repository.saveCommand(clientId, expression, result); | ||
return result; | ||
} | ||
|
||
// getHistory returns the latest 10 commands for a client | ||
public getHistory(clientId: string): CommandAndResult[] { | ||
return this.repository.getLatest(clientId, historyCount); | ||
} | ||
|
||
// evaluate evaluate a mathematical expression | ||
private evaluate(expression: string): string { | ||
if (!this.isValidCommand(expression)) { | ||
throw new Error('Invalid command'); | ||
} | ||
return evaluateMathjs(expression).toString(); | ||
} | ||
|
||
// isValidCommand checks if the command is valid | ||
private isValidCommand(s: string): boolean { | ||
s = s.replace(/ /g,''); // remove all spaces | ||
if (s.length === 0) { | ||
return false; | ||
} | ||
// allowed characters: 0-9, +, -, *, /, . | ||
const regex = /^[\d+\-*/.]+$/; | ||
if (!regex.test(s)) { | ||
return false; | ||
} | ||
// operators cannot be adjacent to each other | ||
for (let i = 0; i < s.length - 1; i++) { | ||
if (this.operators.has(s[i]) && this.operators.has(s[i + 1])) { | ||
return false; | ||
} | ||
} | ||
|
||
// operators cannot be at the beginning or end of the string | ||
if (this.operators.has(s[0]) || this.operators.has(s[s.length - 1])) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
export default CommandService; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// CommandAndResult is a round trip of a command and its result | ||
export type CommandAndResult = { | ||
expression: string; | ||
result: string; | ||
} | ||
|
||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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