From 950192a89623c1bcf2d336fcc9c5fa6d85849997 Mon Sep 17 00:00:00 2001 From: Jam <42326027+benjammin4dayz@users.noreply.github.com> Date: Thu, 18 Jul 2024 17:05:31 -0400 Subject: [PATCH] Refactor calculator, enable test; fix #1 * Implement _check function * Enable test to assert _check --- src/calculator.js | 34 ++++++++-------------------------- src/calculator.test.js | 2 +- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index b46080e5..924b58e7 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,47 +1,29 @@ -exports._check = () => { - // DRY up the codebase with this function - // First, move the duplicate error checking code here - // Then, invoke this function inside each of the others - // HINT: you can invoke this function with exports._check() -}; - -exports.add = (x, y) => { +exports._check = (x, y) => { if (typeof x !== 'number') { throw new TypeError(`${x} is not a number`); } if (typeof y !== 'number') { throw new TypeError(`${y} is not a number`); } +}; + +exports.add = (x, y) => { + exports._check(x, y); return x + y; }; exports.subtract = (x, y) => { - if (typeof x !== 'number') { - throw new TypeError(`${x} is not a number`); - } - if (typeof y !== 'number') { - throw new TypeError(`${y} is not a number`); - } + exports._check(x, y); return x - y; }; exports.multiply = (x, y) => { - if (typeof x !== 'number') { - throw new TypeError(`${x} is not a number`); - } - if (typeof y !== 'number') { - throw new TypeError(`${y} is not a number`); - } + exports._check(x, y); return x * y; }; exports.divide = (x, y) => { - if (typeof x !== 'number') { - throw new TypeError(`${x} is not a number`); - } - if (typeof y !== 'number') { - throw new TypeError(`${y} is not a number`); - } + exports._check(x, y); return x / y; }; diff --git a/src/calculator.test.js b/src/calculator.test.js index d0f85ed6..3d6b7f74 100644 --- a/src/calculator.test.js +++ b/src/calculator.test.js @@ -1,7 +1,7 @@ /* eslint-disable no-unused-expressions */ const calculator = require('./calculator'); -describe.skip('_check', () => { +describe('_check', () => { beforeEach(() => { sinon.spy(calculator, '_check'); });