Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add homework to git, all worked, tests written #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,29 @@
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
"extends": [
"plugin:react/recommended",
"standard"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint"
],
"rules": {
}
}
19 changes: 18 additions & 1 deletion README.md
@@ -1 +1,18 @@
# react-otus-homework
# react-otus-homework

Для домашнего задания использован репозиторий
https://github.com/nickovchinnikov/react-js-tutorial/pull/3
код из папки src/lesson2


Добавлена поддержка математических операторов:

возведение в квадрат **
возведение в степень ^
факториал !

Добавлена обработка скобок, в том числе вложенных
пример: (2 + 2) * 2 = 8

Добавлена обработка текста без пробелов, формулы можно вводить прямым текстом
Добавлены тесты
18 changes: 18 additions & 0 deletions babel.config.json
@@ -0,0 +1,18 @@
{
"presets": [
[
"@babel/env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1"
},
"useBuiltIns": "usage",
"corejs": "3.6.5"
}
],
"@babel/preset-typescript"
]
}
4 changes: 2 additions & 2 deletions index.js
@@ -1,2 +1,2 @@
"use strict";
//# sourceMappingURL=index.js.map
'use strict'
// # sourceMappingURL=index.js.map
10 changes: 5 additions & 5 deletions jest.config.js
Expand Up @@ -3,9 +3,9 @@

module.exports = {
clearMocks: true,
coverageDirectory: "coverage",
testEnvironment: "node",
coverageDirectory: 'coverage',
testEnvironment: 'node',
transform: {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
},
};
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest'
}
}
21 changes: 4 additions & 17 deletions package.json
@@ -1,31 +1,19 @@
{
"name": "react-js-tutorial",
"name": "react-otus-homework",
"version": "1.0.0",
"description": "",
"main": "lesson2/index.js",
"scripts": {
"build": "npx webpack --mode production",
"calc": "npx ts-node src/lesson2",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm run calc выдает ошибку Emit skipped TypeStrong/ts-node#693 (comment)
Немного смутило сначала, тесты проходят, а калькулятор не запускается! xD

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

подправил, должно запускаться

"lint": "npx eslint --ext .js,.jsx,.ts,.tsx --fix ./",
"test": "npx jest",
"check": "npm test && npm run lint"
},
"husky": {
"hooks": {
"pre-commit": "npm run check",
"pre-push": "npm run check"
}
"test": "npx jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nickovchinnikov/react-js-tutorial.git"
"url": "https://github.com/Vladimir2308/react-otus-homework.git"
},
"author": "Nick Ovchinnikov",
"author": "Vladimir Sushchen",
"license": "ISC",
"bugs": {
"url": "https://github.com/nickovchinnikov/react-js-tutorial/issues"
},
"homepage": "https://github.com/nickovchinnikov/react-js-tutorial#readme",
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
Expand All @@ -44,7 +32,6 @@
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-react": "^7.26.1",
"html-webpack-plugin": "^4.0.3",
"husky": "^4.2.3",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а почему вы не хотите прекоммит хук с линтером? это же очень удобно, мне понравилось, когда узнал что так можно

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пока не успел разобрать, не всё сразу

"jest": "^25.2.4",
"prettier": "^2.0.2",
"ts-node": "^8.8.2",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
@@ -1,2 +1,2 @@
"use strict";
//# sourceMappingURL=index.js.map
'use strict'
// # sourceMappingURL=index.js.map
114 changes: 114 additions & 0 deletions src/lesson2/engine.js
@@ -0,0 +1,114 @@
'use strict'
const __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length
for (var r = Array(s), k = 0, i = 0; i < il; i++) {
for (let a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) { r[k] = a[j] }
}
return r
}
Object.defineProperty(exports, '__esModule', { value: true })
exports.thirdPrioritiesCalc = exports.secondPrioritiesCalc = exports.firstPrioritiesCalc = exports.calcWithPriorities = exports.bracketExprCalc = void 0
const helpers_1 = require('./helpers')
const mathOperators_1 = require('./mathOperators')
const FIRST = mathOperators_1.mathPriorities[0]; const SECOND = mathOperators_1.mathPriorities[1]; const THIRD = mathOperators_1.mathPriorities[2]
exports.bracketExprCalc = function (stack) {
const result = []
const listInnerBrackets = []
const openBracketStack = []
let temp
for (let i = 0; i < stack.length; i++) {
if (stack[i] === '(') {
openBracketStack.push(i)
temp = []
listInnerBrackets.push(temp)
} else if (stack[i] === ')') {
const positionLastOpenBracket = openBracketStack.pop()
if (positionLastOpenBracket == null) {
throw new TypeError('Unexpected stack! Unexpected Bracket!')
}
const exprInBracket = listInnerBrackets.pop()
if (exprInBracket !== undefined) {
const valueInBracket = exports.calcWithPriorities(exprInBracket)
if (listInnerBrackets.length === 0) {
result.push(valueInBracket)
} else {
var outerBracketExpr = listInnerBrackets[listInnerBrackets.length - 1]
if (outerBracketExpr !== undefined) {
outerBracketExpr.push(valueInBracket)
}
}
}
} else if (openBracketStack.length > 0) {
var outerBracketExpr = listInnerBrackets[listInnerBrackets.length - 1]
if (outerBracketExpr !== undefined) {
outerBracketExpr.push(stack[i])
}
} else {
result.push(stack[i])
}
}
return result
}
exports.calcWithPriorities = function (bracketExprRes) {
const firstPrioritiesRes = exports.firstPrioritiesCalc(bracketExprRes)
if (firstPrioritiesRes.length === 1) {
return Number(firstPrioritiesRes[0])
}
const secondPrioritiesRes = exports.secondPrioritiesCalc(firstPrioritiesRes)
if (secondPrioritiesRes.length === 1) {
return Number(secondPrioritiesRes[0])
}
return exports.thirdPrioritiesCalc(secondPrioritiesRes)
}
exports.firstPrioritiesCalc = function (stack) {
return stack.reduce(function (result, nextItem) {
const prevItem = result[result.length - 2]
const item = result[result.length - 1]
if ((!helpers_1.isNumber(String(item)) && mathOperators_1.mathOperatorsPriorities[item] === FIRST) ||
(helpers_1.isNumber(String(item)) && (mathOperators_1.mathOperatorsPriorities[nextItem] === FIRST) &&
(nextItem === '**' || nextItem === '!'))) {
if (nextItem === '**' || nextItem === '!') {
result = __spreadArrays(result.slice(0, -1), [
mathOperators_1.mathOperators[nextItem](Number(item), Number(item))
])
} else {
result = __spreadArrays(result.slice(0, -2), [
mathOperators_1.mathOperators[item](Number(prevItem), Number(nextItem))
])
}
} else {
result.push(nextItem)
}
return result
}, [])
}
exports.secondPrioritiesCalc = function (stack) {
return stack.reduce(function (result, nextItem) {
const prevItem = result[result.length - 2]
const item = result[result.length - 1]
if (!helpers_1.isNumber(String(item)) && mathOperators_1.mathOperatorsPriorities[item] === SECOND) {
if (!mathOperators_1.mathOperators[item]) {
throw new TypeError('Unexpected stack!')
}
result = __spreadArrays(result.slice(0, -2), [
mathOperators_1.mathOperators[item](Number(prevItem), Number(nextItem))
])
} else {
result.push(nextItem)
}
return result
}, [])
}
exports.thirdPrioritiesCalc = function (stack) {
return stack.reduce(function (result, nextItem, key) {
const item = stack[key - 1]
if (mathOperators_1.mathOperatorsPriorities[item] === FIRST || mathOperators_1.mathOperatorsPriorities[item] === SECOND) {
throw new TypeError('Unexpected stack!')
}
if (!helpers_1.isNumber(String(item)) && mathOperators_1.mathOperatorsPriorities[item] === THIRD) {
result = mathOperators_1.mathOperators[item](Number(result), Number(nextItem))
}
return result
}, Number(stack[0]))
}
// # sourceMappingURL=engine.js.map
1 change: 1 addition & 0 deletions src/lesson2/engine.js.map

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

80 changes: 80 additions & 0 deletions src/lesson2/engine.test.js
@@ -0,0 +1,80 @@
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
const engine_1 = require('./engine')
describe('firstPrioritiesCalc simple cases', function () {
it('[2, "!"]', function () {
expect(engine_1.firstPrioritiesCalc([2, '**'])).toEqual([4])
})
it('[5, "!"]', function () {
expect(engine_1.firstPrioritiesCalc([5, '**'])).toEqual([25])
})
it('[1, "!"]', function () {
expect(engine_1.firstPrioritiesCalc([1, '!'])).toEqual([1])
})
it('[0, "!"]', function () {
expect(engine_1.firstPrioritiesCalc([0, '!'])).toEqual([1])
})
it('[5, "!"]', function () {
expect(engine_1.firstPrioritiesCalc([5, '!'])).toEqual([120])
})
it('[5, "^",3]', function () {
expect(engine_1.firstPrioritiesCalc([5, '^', 3])).toEqual([125])
})
it('[6, "^",6]', function () {
expect(engine_1.firstPrioritiesCalc([6, '^', 6])).toEqual([46656])
})
})
describe('secondPrioritiesCalc simple cases', function () {
it('[1, * 32]', function () {
expect(engine_1.secondPrioritiesCalc([1, '*', 32])).toEqual([32])
})
it('[32, /, 32]', function () {
expect(engine_1.secondPrioritiesCalc([32, '/', 32])).toEqual([1])
})
it('[32, + 32]', function () {
expect(engine_1.secondPrioritiesCalc([32, '+', 32])).toEqual([32, '+', 32])
})
})
describe('secondPrioritiesCalc mixed with second priorities cases', function () {
it('[32, /, 32, +, 10, *, 10]', function () {
expect(engine_1.secondPrioritiesCalc([32, '/', 32, '+', 10, '*', 10])).toEqual([
1,
'+',
100
])
})
})
describe('thirdPrioritiesCalc invalid cases', function () {
it('[32, / 32]', function () {
expect(function () { return engine_1.thirdPrioritiesCalc([32, '/', 32]) }).toThrow(TypeError('Unexpected stack!'))
})
})
describe('thirdPrioritiesCalc simple cases', function () {
it('[32, + 32]', function () {
expect(engine_1.thirdPrioritiesCalc([32, '+', 32])).toEqual(64)
})
it('[32, - 32]', function () {
expect(engine_1.thirdPrioritiesCalc([32, '-', 32])).toEqual(0)
})
it('[32, - 32, +, 10]', function () {
expect(engine_1.thirdPrioritiesCalc([32, '-', 32, '+', 10])).toEqual(10)
})
it('[32, - 32, +, 10]', function () {
expect(engine_1.thirdPrioritiesCalc([32, '-', 32, '+', 10])).toEqual(10)
})
it('[32, - 32, +, 10]', function () {
expect(engine_1.thirdPrioritiesCalc([32, '-', 32, '+', 10])).toEqual(10)
})
})
describe('bracketExprCalc invalid cases', function () {
it(') (', function () {
expect(function () { return engine_1.bracketExprCalc([')', '(']) }).toThrow(TypeError('Unexpected stack! Unexpected Bracket!'))
})
it('( ) (', function () {
expect(function () { return engine_1.bracketExprCalc(['(', ')', ')']) }).toThrow(TypeError('Unexpected stack! Unexpected Bracket!'))
})
it('( ) ( ) )', function () {
expect(function () { return engine_1.bracketExprCalc(['(', ')', ')', ')', ')']) }).toThrow(TypeError('Unexpected stack! Unexpected Bracket!'))
})
})
// # sourceMappingURL=engine.test.js.map
1 change: 1 addition & 0 deletions src/lesson2/engine.test.js.map

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