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 / 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';
@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 / 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 / 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 / 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);