Skip to content

Instantly share code, notes, and snippets.

View mazeeblanke's full-sized avatar
🎯
Focusing

Ewomazino Ukah mazeeblanke

🎯
Focusing
View GitHub Profile
@mazeeblanke
mazeeblanke / calculate.test.js
Last active September 26, 2018 08:47
Unit test the utility function calculate.js
import calculate from '../utils/calculate.js'
describe('Calculate', () => {
let expression;
it ('evaluates the expression correctly', () => {
expression = '2+3+4-4*3'
expect(calculate(expression)).toBe(-3);
expression = '0+3+4'
expect(calculate(expression)).toBe(7);
@mazeeblanke
mazeeblanke / calculate.js
Last active September 26, 2018 08:46
Calculate.js utility file
export default (expression) => {
const matched = (new RegExp('([\\d]+\\.?[\\d]*)?([-+/*][\\d]+\\.?[\\d]*)*')).exec(expression)
if (!matched) {
return 0;
}
if (/^[*+\/]/.test(expression)){
return () => {
@mazeeblanke
mazeeblanke / index.js
Last active September 24, 2018 19:04
The redux store
import { createStore, combineReducers } from 'redux';
import calculateReducer from './reducers/calculateReducer'
const rootReducer = combineReducers({
calculator: calculateReducer
})
export default createStore(
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
@mazeeblanke
mazeeblanke / calculateReducer.js
Last active September 26, 2018 10:01
The calculator reducer
import * as types from '../types';
import calculate from '../../utils/calculate'
let initialState = {
expression: '',
total: 0
}
function setExpression({ expression, total}, action) {
@mazeeblanke
mazeeblanke / types.js
Last active September 25, 2018 11:14
export const SET_EXPRESSION = 'SET_EXPRESSION';
export const CLEAR_EXPRESSION = 'CLEAR_EXPRESSION';
export const DELETE_LAST_EXPRESSION_ENTRY = 'DELETE_LAST_EXPRESSION_ENTRY';
export const EVALUATE_EXPRESSION = 'EVALUATE_EXPRESSION';
import * as types from '../types';
export const calculate = (key) => {
return {
type: types.SET_EXPRESSION,
payload: key
}
}
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import store from './store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
@mazeeblanke
mazeeblanke / App.js
Last active September 25, 2018 06:57
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { calculate, deleteLastEntry, clear, evaluateExpression } from './store/actions/calculate'
import Calculator from './components/calculator'
import * as fromCalculator from './store'
import './App.css'
export class App extends Component {
componentDidMount () {
import Screen from './Screen'
import Keypad from './Keypad'
import Button from './Button'
export default {
Screen,
Keypad,
Button
}
import React from 'react';
import { Textfit } from 'react-textfit';
export default (props) => {
return (
<div className="screen--container">
<Textfit
max={40}
throttle={60}
mode="single"
className="screen-top"