Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ Basic configuration for the project on the master branch. What configurations yo
* Eslint
* Jest
* Precommit hooks / husky

## Lesson 2:
Math calculator with basic arithmetic operations without eval

```npm run calc```

Then you can calc simple math operations

Examples:

```
> 10 + 10
Result: 20
> 10 + 10 * 20 - 30
Result: 180
> 19 + -10
Result: 9
```
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"main": "index.js",
"scripts": {
"build": "npx webpack --mode production",
"calc": "npx ts-node src/lesson2",
"lint": "npx eslint --ext .js,.jsx,.ts,.tsx --fix ./",
"test": "npx jest"
"test": "npx jest",
"check": "npm test && npm run lint"
},
"husky": {
"hooks": {
"pre-commit": "npm test && npm run lint",
"pre-push": "npm test && npm run lint"
"pre-commit": "npm run check",
"pre-push": "npm run check"
}
},
"repository": {
Expand Down Expand Up @@ -40,6 +42,7 @@
"husky": "^4.2.3",
"jest": "^25.2.4",
"prettier": "^2.0.2",
"ts-node": "^8.8.2",
"typescript": "^3.8.3",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11",
Expand Down
47 changes: 47 additions & 0 deletions src/lesson2/engine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { firstPrioritiesCalc, secondPrioritiesCalc } from "./engine";

describe("firstPrioritiesCalc simple cases", () => {
it("[1, * 32]", () => {
expect(firstPrioritiesCalc([1, "*", 32])).toEqual([32]);
});

it("[32, /, 32]", () => {
expect(firstPrioritiesCalc([32, "/", 32])).toEqual([1]);
});

it("[32, + 32]", () => {
expect(firstPrioritiesCalc([32, "+", 32])).toEqual([32, "+", 32]);
});
});

describe("firstPrioritiesCalc mixed with second priorities cases", () => {
it("[32, /, 32, +, 10, *, 10]", () => {
expect(firstPrioritiesCalc([32, "/", 32, "+", 10, "*", 10])).toEqual([
1,
"+",
100,
]);
});
});

describe("secondPrioritiesCalc invalid cases", () => {
it("[32, / 32]", () => {
expect(() => secondPrioritiesCalc([32, "/", 32])).toThrow(
TypeError("Unexpected stack!")
);
});
});

describe("secondPrioritiesCalc simple cases", () => {
it("[32, + 32]", () => {
expect(secondPrioritiesCalc([32, "+", 32])).toEqual(64);
});

it("[32, - 32]", () => {
expect(secondPrioritiesCalc([32, "-", 32])).toEqual(0);
});

it("[32, - 32, +, 10]", () => {
expect(secondPrioritiesCalc([32, "-", 32, "+", 10])).toEqual(10);
});
});
42 changes: 42 additions & 0 deletions src/lesson2/engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ParsedLineType } from "./parser";
import { isNumber } from "./helpers";
import {
mathOperators,
mathPriorities,
mathOperatorsPriorities,
} from "./mathOperators";

const [FIRST, SECOND] = mathPriorities;

export const firstPrioritiesCalc = (stack: ParsedLineType): ParsedLineType =>
stack.reduce<ParsedLineType>((result, nextItem) => {
const prevItem = result[result.length - 2];
const item = result[result.length - 1];

if (!isNumber(String(item)) && mathOperatorsPriorities[item] === FIRST) {
if (!mathOperators[item]) {
throw new TypeError("Unexpected stack!");
}
result = [
...result.slice(0, -2),
mathOperators[item](Number(prevItem), Number(nextItem)),
];
} else {
result.push(nextItem);
}
return result;
}, []);

export const secondPrioritiesCalc = (stack: ParsedLineType): number =>
stack.reduce<number>((result, nextItem, key) => {
const item = stack[key - 1];

if (mathOperatorsPriorities[item] === FIRST) {
throw new TypeError("Unexpected stack!");
}

if (!isNumber(String(item)) && mathOperatorsPriorities[item] === SECOND) {
result = mathOperators[item](Number(result), Number(nextItem));
}
return result;
}, Number(stack[0]));
1 change: 1 addition & 0 deletions src/lesson2/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const isNumber = (item: string): boolean => !isNaN(Number(item));
29 changes: 29 additions & 0 deletions src/lesson2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createInterface } from "readline";

import { runner } from "./runner";

const rl = createInterface({
input: process.stdin,
output: process.stdout,
});

const question = (): Promise<null> =>
new Promise((resolve) => {
rl.question("> ", (answer: string) => {
const result = runner(answer);

if (result) {
console.log(`Result: ${result}`);
}

resolve();
});
});

async function app(): Promise<null> {
while (true) {
await question();
}
}

app();
27 changes: 27 additions & 0 deletions src/lesson2/mathOperators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mul, div, add, minus } from "./mathOperators";

describe("mathOperators test cases", () => {
it("mul 1 * 2 to equal 2", () => {
expect(mul(1, 2)).toBe(2);
});

it("mul 2 * 2 to equal 4", () => {
expect(mul(2, 2)).toBe(4);
});

it("div 2 / 2 to equal 1", () => {
expect(div(2, 2)).toBe(1);
});

it("div 4 / 2 to equal 2", () => {
expect(div(4, 2)).toBe(2);
});

it("add 4 + 2 to equal 6", () => {
expect(add(4, 2)).toBe(6);
});

it("minus 4 - 2 to equal 2", () => {
expect(minus(4, 2)).toBe(2);
});
});
39 changes: 39 additions & 0 deletions src/lesson2/mathOperators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export type ScalarOperationType = (first: number, second: number) => number;

export const mul: ScalarOperationType = (
first: number,
second: number
): number => first * second;

export const div: ScalarOperationType = (
first: number,
second: number
): number => first / second;

export const add: ScalarOperationType = (
first: number,
second: number
): number => first + second;

export const minus: ScalarOperationType = (
first: number,
second: number
): number => first - second;

export const mathOperators: { [key: string]: ScalarOperationType } = {
"*": mul,
"/": div,
"+": add,
"-": minus,
};

export const mathPriorities: number[] = [1, 2];

const [FIRST, SECOND] = mathPriorities;

export const mathOperatorsPriorities: { [key: string]: number } = {
"*": FIRST,
"/": FIRST,
"+": SECOND,
"-": SECOND,
};
27 changes: 27 additions & 0 deletions src/lesson2/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { parser } from "./parser";

describe("Parser correct cases", () => {
it("1 + 32", () => {
expect(parser("1 + 32")).toEqual([1, "+", 32]);
});

it("11 + 3 * 22", () => {
expect(parser("11 + 3 * 22")).toEqual([11, "+", 3, "*", 22]);
});

it("1 + 32 - 2 + 2", () => {
expect(parser("1 + 32 - 2 + 2")).toEqual([1, "+", 32, "-", 2, "+", 2]);
});
});

describe("Parser invalid cases", () => {
it("1 + + 33 - 2", () => {
expect(() => parser("1 + + 33 - 2")).toThrow(
TypeError("Unexpected string")
);
});

it("1 ! 33 - 2", () => {
expect(() => parser("1 ! 33 - 2")).toThrow(TypeError("Unexpected string"));
});
});
27 changes: 27 additions & 0 deletions src/lesson2/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { isNumber } from "./helpers";
import { mathOperators } from "./mathOperators";

export type ParsedLineType = (number | string)[];

export const parser = (line: string): ParsedLineType | null => {
const stack = line.split(" ");

return stack.reduce<ParsedLineType>((result, item, key) => {
const prevItem = stack[key - 1];

const isValidNumberPush = !isNumber(prevItem) && isNumber(item);
const isValidOperatorPush =
isNumber(prevItem) &&
!isNumber(item) &&
mathOperators.hasOwnProperty(item);

if (isValidNumberPush) {
result.push(Number(item));
} else if (isValidOperatorPush) {
result.push(item);
} else {
throw new TypeError("Unexpected string");
}
return result;
}, []);
};
Loading