Skip to content

kmin 계산기 제출입니다. #2

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ typings/

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
Expand Down
5 changes: 5 additions & 0 deletions .idea/.gitignore

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

12 changes: 12 additions & 0 deletions .idea/javascript-calculator.iml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

7 changes: 7 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"baseUrl": "http://localhost:5050",
"env": {
"TOTAL": "#total",
"MODIFIER": ".modifier"
}
Comment on lines +3 to +6

Choose a reason for hiding this comment

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

Cypress에서도 환경변수를 넘겨줄 수 있었네요! 하나 배워갑니다.

}
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
43 changes: 43 additions & 0 deletions cypress/integration/sample_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
describe("Reg form", () => {
const clickButtons = (values: string): void => {
const charFromValues = values.split("");
charFromValues.forEach((char) => {
cy.get("button").contains(char).click();
})
};

const calculate = (expression: string, expectedResult: string): void => {
clickButtons(expression);
cy.get(Cypress.env("TOTAL")).should('have.text', expectedResult);
cy.get(Cypress.env("MODIFIER")).click();
}
it("input numbers must be in range 0 ~ 999", () => {
cy.visit("/");
clickButtons("123111");
cy.get(Cypress.env("TOTAL")).should("have.text", "123");
})
it("clear board", () => {
cy.get(Cypress.env("MODIFIER")).click();
cy.get(Cypress.env("TOTAL")).should("have.text", "0");
})
it("add numbers", () => {
calculate("123+123=","246");
calculate("123+123+","246");
calculate("123+123+123=","369");
})
it("multiply numbers", () => {
calculate("123X123=","15129");
calculate("123X123X","15129");
calculate("123X123X123=","1860867");
})
it("divide numbers", () => {
calculate("123/123=","1");
calculate("123/123/","1");
calculate("123/123/123=","0");
})
it("subtract numbers", () => {
calculate("123-123=","0");
calculate("123-123-","0");
calculate("123-123-123=","-123");
})
})
22 changes: 22 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.tsfile can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
25 changes: 25 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.tsfile shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
20 changes: 20 additions & 0 deletions cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.tsfile is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.tsfile using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
Binary file added cypress/videos/sample_spec.ts.mp4
Binary file not shown.
2 changes: 2 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Calculator from "./tsfile/component/calculator.js";
new Calculator(document.querySelector('.calculator'));
23 changes: 23 additions & 0 deletions dist/tsfile/component/board.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isClear, isOperator } from "../../utils/utils.js";
class BoardImpl {
constructor({ $element, onClick }) {
this.addEvent = () => {
this.$element.addEventListener('click', ({ target }) => {
if (!(target instanceof HTMLButtonElement))
return;
const value = target.textContent;
if (isOperator(value)) {
return this.onClick({ type: 'Operator', value });
}
if (isClear(value)) {
return this.onClick({ type: 'Clear', value });
}
return this.onClick({ type: 'Number', value });
});
};
this.$element = $element;
this.onClick = onClick;
this.addEvent();
}
}
export default BoardImpl;
65 changes: 65 additions & 0 deletions dist/tsfile/component/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import ScreenImpl from "./screen.js";
import BoardImpl from "./board.js";
import { calc } from "../../utils/utils.js";
class Calculator {
constructor($element) {
this.$element = $element;
this.clear = () => {
this.value = '';
this.oper = '';
this.prev = '';
};
this.setState = (value) => {
this.value = value;
this.screen.setState(value);
};
this.onClick = ({ type, value }) => {
switch (type) {
case 'Operator':
if (this.operatorPressed)
return;
this.operatorPressed = true;
this.prev = calc(this.prev, this.oper, this.value);
this.oper = value;
this.screen.setState(this.prev);
if (value === '=') {
this.operatorPressed = false;
}
this.value = '0';
break;
case 'Clear':
this.clear();
this.setState('0');
break;
case 'Number':
if (this.oper === '=')
this.clear();
if (this.value.length === 3)
break;
let newValue = this.value === '0' ? value : this.value + value;
if (!this.operatorPressed) {
this.setState(newValue);
break;
}
this.operatorPressed = false;
this.setState(value);
break;
default:
throw new Error('Operation fail');
}
};
this.value = '0';
this.prev = '';
this.oper = '';
this.operatorPressed = false;
this.screen = new ScreenImpl({
$element: this.$element.querySelector('#total'),
value: this.value
});
new BoardImpl({
$element: this.$element,
onClick: this.onClick
});
}
}
export default Calculator;
14 changes: 14 additions & 0 deletions dist/tsfile/component/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ScreenImpl {
constructor({ $element, value }) {
this.setState = (value) => {
this.value = value;
this.render();
};
this.render = () => {
this.$element.textContent = `${this.value}`;
};
this.$element = $element;
this.value = value;
}
}
export default ScreenImpl;
1 change: 1 addition & 0 deletions dist/types/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
30 changes: 30 additions & 0 deletions dist/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const isOperator = (value) => {
const allowedKey = ['+', '-', '/', 'X', '='];
return allowedKey.indexOf(value) !== -1;
};
const isClear = (value) => {
const allowedKey = 'AC';
return allowedKey === value;
};
const operate = (prev, oper, value) => {
const num_prev = +prev;
const num_value = +value;
switch (oper) {
case '+':
return String(num_prev + num_value);
case '-':
return String(num_prev - num_value);
case 'X':
return String(num_prev * num_value);
case '/':
return String(Math.floor(num_prev / num_value));
default:
return prev;
}
};
const calc = (prev, oper, value) => {
if (prev === '' || oper === '')
return value;
return operate(prev, oper, value);
};
export { isOperator, isClear, calc };
71 changes: 36 additions & 35 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="src/css/index.css" />
</head>
<body>
<div id="app">
<div class="calculator">
<h1 id="total">0</h1>
<div class="digits flex">
<button class="digit">9</button>
<button class="digit">8</button>
<button class="digit">7</button>
<button class="digit">6</button>
<button class="digit">5</button>
<button class="digit">4</button>
<button class="digit">3</button>
<button class="digit">2</button>
<button class="digit">1</button>
<button class="digit">0</button>
</div>
<div class="modifiers subgrid">
<button class="modifier">AC</button>
</div>
<div class="operations subgrid">
<button class="operation">/</button>
<button class="operation">X</button>
<button class="operation">-</button>
<button class="operation">+</button>
<button class="operation">=</button>
</div>
</div>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculator</title>
<link rel="stylesheet" type="text/css" href="src/css/index.css" />
</head>
<body>
<div id="app">
<div class="calculator">
<h1 id="total">0</h1>
<div class="digits flex">
<button class="digit">9</button>
<button class="digit">8</button>
<button class="digit">7</button>
<button class="digit">6</button>
<button class="digit">5</button>
<button class="digit">4</button>
<button class="digit">3</button>
<button class="digit">2</button>
<button class="digit">1</button>
<button class="digit">0</button>
</div>
</body>
</html>
<div class="modifiers subgrid">
<button class="modifier">AC</button>
</div>
<div class="operations subgrid">
<button class="operation">/</button>
<button class="operation">X</button>
<button class="operation">-</button>
<button class="operation">+</button>
<button class="operation">=</button>
</div>
</div>
</div>
<script type="module" src="./dist/index.js"></script>
</body>
</html>
Loading