Compare commits

..

No commits in common. "master" and "simplify-data-structure" have entirely different histories.

28 changed files with 15686 additions and 2470 deletions

View File

@ -1,21 +0,0 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react/jsx-runtime',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
settings: { react: { version: '18.2' } },
plugins: ['react-refresh'],
rules: {
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}

2
.gitignore vendored
View File

@ -9,7 +9,7 @@
/coverage
# production
/dist
/build
# misc
.DS_Store

18
Jenkinsfile vendored
View File

@ -1,18 +0,0 @@
pipeline {
agent any
stages {
stage('build'){
steps{
sh 'npm install'
sh 'npm run build'
}
}
stage('deploy'){
steps{
sshPublisher(publishers: [sshPublisherDesc(configName: 'Demos', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'ssh-uploads/calculator', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'dist/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}

View File

@ -1,8 +1,70 @@
# Responsive Calculator App
# Getting Started with Create React App
I built this as coursework for a [Frontend certification](https://www.freecodecamp.org/certification/fcc60e22726-0a94-4738-8702-3e216d14fdb2/front-end-development-libraries). [Check out the live demo.](https://demos.ajstepien.xyz/calculator)
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## This app is..
- fully functional as a calculator.
- designed to look good on a range of devices, in a range of aspect ratios.
- built with an elegant command routing system (if I do say so myself).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
The page will reload when you make changes.\
You may also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Analyzing the Bundle Size
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `npm run build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

View File

@ -1,16 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
</body>
</html>

17332
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +1,44 @@
{
"name": "calculator-vite",
"homepage": "https://andrzejstepien.github.io/calculator/",
"name": "calculator",
"version": "0.1.0",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build --base=/calculator/",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy" : "npm run build",
"deploy" : "gh-pages -d build"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"vite": "^5.2.0"
"gh-pages": "^5.0.0"
}
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

45
public/index.html Normal file
View File

@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
</body>
</html>

BIN
public/logo192.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

BIN
public/logo512.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

25
public/manifest.json Normal file
View File

@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

3
public/robots.txt Normal file
View File

@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,193 +1,11 @@
*,
*::before,
*::after {
box-sizing: border-box;
}
@font-face {
font-family: calculator;
src: url("DS-DIGIB.TTF");
}
@media screen and (min-width:601px) {
html {
font-size: 16px;
}
}
@media screen and (max-width:600px) {
html {
font-size: 12px;
}
}
body {
padding: 0px;
margin: 0px;
}
#calculator {
background-color: white;
height: 50rem;
max-height: 100vh;
width: 35rem;
max-width: 100vw;
margin: auto;
background: rgb(68, 64, 64);
background: linear-gradient(351deg, rgba(68, 64, 64, 1) 0%, rgba(156, 150, 150, 1) 100%);
padding: 1rem;
border-radius: 2rem;
}
#calculator-content {
height: 100%;
max-width: 100%;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: 1fr 1.6fr repeat(5, 1fr);
grid-template-areas:
"header header header header"
"display display display display"
"functions functions functions operators"
"numbers numbers numbers operators"
"numbers numbers numbers operators"
"numbers numbers numbers operators"
"numbers numbers numbers operators"
;
gap: 1em;
}
#calculator-content>header {
grid-area: header;
align-self: center;
font-family: 'Roboto', sans-serif;
}
#calculator-content>header>h1 {
font-size: 3rem;
margin: 0.5rem auto;
color: aliceblue;
opacity: 0.8;
}
#display-container {
grid-area: display;
justify-self: end;
align-self: end;
height: 9em;
width: 100%;
display: grid;
grid-template-rows: 1fr 6fr 1fr;
grid-template-areas:
"memory"
"main"
"operator"
;
background: linear-gradient(351deg, rgba(15, 93, 53, 1) 0%, rgba(194, 228, 185, 1) 100%);
font-family: 'VT323', monospace;
font-size: 1rem;
border-radius: 0.5rem;
padding: 0.5rem;
box-shadow: 0.5rem 0.5rem 2rem inset;
}
.off {
filter: brightness(0.5);
}
.off>p {
display: none !important;
}
#display-container>p {
margin: auto 0;
text-align: right;
opacity: 0.8;
button{
width:100px;
height:100px;
background-color: gray;
margin: 10px;
font-size: 30px;
}
#display{
font-family: calculator;
font-size: 5rem;
grid-area: main;
align-self: center;
}
#memory {
grid-area: memory;
align-self: center;
}
#operator {
grid-area: operator;
align-self: center;
}
.num-pad {
grid-area: numbers;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, 1fr);
gap: 0.2rem;
}
.operator-pad {
grid-area: operators;
display: grid;
grid-template-rows: repeat(3, 1fr) 2fr;
gap: 0.2rem;
}
.function-pad {
grid-area: functions;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.2rem;
}
button {
background: linear-gradient(351deg, rgba(92, 92, 92, 0.9) 0%, rgba(224, 224, 224, 0.9) 100%);
background-blend-mode: multiply;
font-size: 3rem;
place-self: stretch;
border-radius: 1.5rem;
}
.num-pad>button {
background-color: lightblue;
}
.operator-pad>button {
font-size: 3.5em;
}
.function-pad>button {
font-size: 2em;
}
#off {
background-color: #FF9B9B;
}
#on {
background-color: #CBFFA9;
}
#clear {
background-color: #FFD6A5;
font-size: 26px;;
}

View File

@ -1,5 +1,5 @@
import "./App.css";
import Calculator from "./Components/Calculator";
import './App.css';
import Calculator from './Components/Calculator';
function App() {
return (

View File

@ -0,0 +1,107 @@
import { useState } from "react";
import KeyPad from "./KeyPad";
export default function Calculator(props){
const [main,setMain] = useState("0")
const [memory,setMemory] = useState("")
const [operation,setOperation] = useState("")
const map = {
conc:(numberString)=>{
if(main[0]==="0"){
setMain(prev=>prev.slice(1)+numberString)
}else{
setMain(prev=>prev+numberString)
}
},
zero:function(){this.conc("0")},
one:function(){this.conc("1")},
two:function(){this.conc("2")},
three:function(){this.conc("3")},
four:function(){this.conc("4")},
five:function(){this.conc("5")},
six:function(){this.conc("6")},
seven:function(){this.conc("7")},
eight:function(){this.conc("8")},
nine:function(){this.conc("9")},
decimal:function(){
if(!/\./g.test(main)){
this.conc(".")
}
},
add:function(){this.rcvOperator("add")},
subtract:function(){this.rcvOperator("subtract")},
multiply:function(){this.rcvOperator("multiply")},
divide:function(){this.rcvOperator("divide")},
rcvOperator:function(operator){
if(main==="-"){
setMain("")
}else if(main!==""){
if(memory===""){
setMemory(main)
}else{
setMemory(prev=>{return this.calculate(main,prev)})
}
setMain("")
}
if(main==="" && operator==="subtract"){
setMain(prev=>{
if(prev[0]!=="-"){
return "-"+prev
}
})
}else{
setOperation(operator)
}
},
operations:{
add:(a,b)=>{return a+b},
subtract:(a,b)=>{return b-a},
multiply:(a,b)=>{return a*b},
divide:(a,b)=>{return b/a},
},
clear:()=>{
setMain("0")
setMemory("")
setOperation("")
},
equals:function(){
if(operation!=="" && main !==""){
setMain(prev=>{
return this.calculate(prev,memory)
})
setMemory("")
setOperation("")
}
},
calculate:function(a,b){
return this.operations[operation](parseFloat(a),parseFloat(b)).toString()
}
}
function handleInput(content){
map[content]()
}
return(
<>
<div id="display-container">
<p id="memory">{"memory: "+memory}</p>
<p id="display">{main}</p>
<p id="operation">{"operation: "+operation}</p>
</div>
<KeyPad handleInput={handleInput}/>
</>
)
}

View File

@ -1,159 +0,0 @@
import { useState } from "react";
import KeyPad from "./KeyPad";
export default function Calculator(props) {
const [main, setMain] = useState("0");
const [memory, setMemory] = useState("");
const [operation, setOperation] = useState("");
const [on, setOn] = useState(true);
const map = {
conc: (numberString) => {
if (main.length <= 9) {
if (main[0] === "0") {
setMain((prev) => prev.slice(1) + numberString);
} else {
setMain((prev) => prev + numberString);
}
}
},
zero: function() {
this.conc("0");
},
one: function() {
this.conc("1");
},
two: function() {
this.conc("2");
},
three: function() {
this.conc("3");
},
four: function() {
this.conc("4");
},
five: function() {
this.conc("5");
},
six: function() {
this.conc("6");
},
seven: function() {
this.conc("7");
},
eight: function() {
this.conc("8");
},
nine: function() {
this.conc("9");
},
decimal: function() {
if (!/\./g.test(main)) {
this.conc(".");
}
},
add: function() {
this.rcvOperator("add");
},
subtract: function() {
this.rcvOperator("subtract");
},
multiply: function() {
this.rcvOperator("multiply");
},
divide: function() {
this.rcvOperator("divide");
},
rcvOperator: function(operator) {
if (main === "-") {
setMain("");
} else if (main !== "") {
if (memory === "") {
setMemory(main);
} else {
setMemory((prev) => {
return this.calculate(main, prev);
});
}
setMain("");
}
if (main === "" && operator === "subtract") {
setMain((prev) => {
if (prev[0] !== "-") {
return "-" + prev;
}
});
} else {
setOperation(operator);
}
},
operations: {
add: (a, b) => {
return a + b;
},
subtract: (a, b) => {
return b - a;
},
multiply: (a, b) => {
return a * b;
},
divide: (a, b) => {
return b / a;
},
},
clear: () => {
setMain("0");
setMemory("");
setOperation("");
},
equals: function() {
if (operation !== "" && main !== "") {
setMain((prev) => {
return this.calculate(prev, memory);
});
setMemory("");
setOperation("");
}
},
on: () => setOn(true),
off: () => {
setOn(false);
setMain("0");
setMemory("");
setOperation("");
},
calculate: function(a, b) {
const string = this.operations[operation](
parseFloat(a),
parseFloat(b),
).toString();
return string.length <= 9 ? string : string.slice(0, 10);
},
};
function handleInput(content) {
map[content]();
}
return (
<div id="calculator">
<div id="calculator-content">
<header>
<h1>CALCULATOR</h1>
</header>
<div id="display-container" className={on ? "on" : "off"}>
<p id="memory">{memory}</p>
<p id="display">{main}</p>
<p id="operator">{operation}</p>
</div>
<KeyPad handleInput={handleInput} />
</div>
</div>
);
}

View File

@ -1,36 +1,30 @@
import Button from "./Button"
export default function NumPad(props){
const numberMap = {
seven:7,
eight:8,
nine:9,
four:4,
five:5,
six:6,
zero:0,
one:1,
two:2,
three:3,
zero:0,
decimal:".",
equals:"="
four:4,
five:5,
six:6,
seven:7,
eight:8,
nine:9,
decimal:"."
}
const operatorMap = {
add:"+",
subtract:"-",
multiply:"x",
divide:"/",
add:"+",
}
const functionMap = {
on:"on",
off:"off",
clear:"C",
equals:"="
}
function buildButtons(map){
@ -49,7 +43,7 @@ export default function NumPad(props){
return(
<>
<div className="keypad">
<div className="num-pad">
{buildButtons(numberMap)}
</div>
@ -59,6 +53,6 @@ export default function NumPad(props){
<div className="function-pad">
{buildButtons(functionMap)}
</div>
</>
</div>
)
}

Binary file not shown.

Binary file not shown.

16
src/index.js Normal file
View File

@ -0,0 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

View File

@ -1,10 +0,0 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);

13
src/reportWebVitals.js Normal file
View File

@ -0,0 +1,13 @@
const reportWebVitals = onPerfEntry => {
if (onPerfEntry && onPerfEntry instanceof Function) {
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
getCLS(onPerfEntry);
getFID(onPerfEntry);
getFCP(onPerfEntry);
getLCP(onPerfEntry);
getTTFB(onPerfEntry);
});
}
};
export default reportWebVitals;

5
src/setupTests.js Normal file
View File

@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';

View File

@ -1,7 +0,0 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})