From 4e175775fdc3375f3e1c1b7fc360314c7f4685c2 Mon Sep 17 00:00:00 2001 From: victor nwaiwu Date: Thu, 19 Jan 2017 13:09:42 +0100 Subject: [PATCH 1/6] refactor(string): refactor string class based on feedback --- src/string.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/string.js b/src/string.js index 1dd5211..aa6b718 100644 --- a/src/string.js +++ b/src/string.js @@ -48,8 +48,8 @@ const stringClassExtensions = { * @returns {Boolean} true or false */ isQuestion() { - const regexType = /\?$/g; - return regexType.test(this); + const regexType = /^[\w]+([. \w]+)?\?$/; + return regexType.test(this.trim()); }, /** @@ -76,17 +76,12 @@ const stringClassExtensions = { * @returns {String} string of numbers */ toCurrency() { - if (!Number(this)) { - return 'This is not a Number'; + if (/[^\d.]/.test(this) || /\..*\./.test(this)) { + return 'Invalid Currency Format'; } - let [number, decimal] = this.split(/\./g); - if (decimal === undefined) { - decimal = '00'; - } else { - decimal = decimal.substring(0, 2); - } - number = number.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); - return `${number}.${decimal}`; + + const currencyValue = Number(this).toFixed(2); + return currencyValue.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); }, /** From 0c19a25e63075ecdff4d4b4bce7f2dc8b42fcba8 Mon Sep 17 00:00:00 2001 From: victor nwaiwu Date: Thu, 19 Jan 2017 13:10:43 +0100 Subject: [PATCH 2/6] refactor(test): refactor test files to pass after refactoring code --- test/string.test.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/string.test.js b/test/string.test.js index ccd8d45..2e19faa 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -1,5 +1,4 @@ /* eslint no-unused-expressions: 0 */ -const mocha = require('mocha'); const expect = require('chai').expect; require('../src/string'); @@ -76,18 +75,22 @@ describe('String Class', () => { it('returns a currency representation of the String', () => { expect('11111.11'.toCurrency()).to.equal('11,111.11'); expect('2535678'.toCurrency()).to.equal('2,535,678.00'); + expect('1234567.'.toCurrency()).to.equal('1,234,567.00'); }); it('returns an error message if the string is not of a "number"', () => { - expect('Mother'.toCurrency()).to.deep.equal('This is not a Number'); - expect('Andela'.toCurrency()).to.deep.equal('This is not a Number'); + expect('Mother'.toCurrency()).to.deep.equal('Invalid Currency Format'); + expect('Andela'.toCurrency()).to.deep.equal('Invalid Currency Format'); + }); + it(`returns an error message if the string is not in a + currency format`, () => { + expect('15248.15.45'.toCurrency()).to.deep.equal('Invalid Currency Format'); }); }); describe('fromCurrency', () => { it('returns a number representation of the currency string', () => { - expect('11,111.11'.fromCurrency()).to.equal('11111.11'); - expect('2,535,678.00'.fromCurrency()).to.equal('2535678.00'); expect('2,535,678'.fromCurrency()).to.equal('2535678'); + expect('2,535,678.11'.fromCurrency()).to.equal('2535678.11'); }); }); From c34be3fd428487a6ac9c7117910cf22f7d3c554d Mon Sep 17 00:00:00 2001 From: victor nwaiwu Date: Thu, 19 Jan 2017 13:20:04 +0100 Subject: [PATCH 3/6] style: fix trailing spaces issue with houndci --- src/string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string.js b/src/string.js index aa6b718..bfc56e0 100644 --- a/src/string.js +++ b/src/string.js @@ -76,7 +76,7 @@ const stringClassExtensions = { * @returns {String} string of numbers */ toCurrency() { - if (/[^\d.]/.test(this) || /\..*\./.test(this)) { + if (/[^\d.]/.test(this) || /\..*\./.test(this)) { return 'Invalid Currency Format'; } From d32c95e1a0e817ebdae9809a342afba5484104dc Mon Sep 17 00:00:00 2001 From: victor nwaiwu Date: Thu, 19 Jan 2017 16:47:19 +0100 Subject: [PATCH 4/6] refactor: throw error when the input is not a number for the toCurrency method --- src/string.js | 2 +- test/string.test.js | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/string.js b/src/string.js index bfc56e0..28212f0 100644 --- a/src/string.js +++ b/src/string.js @@ -77,7 +77,7 @@ const stringClassExtensions = { */ toCurrency() { if (/[^\d.]/.test(this) || /\..*\./.test(this)) { - return 'Invalid Currency Format'; + throw new TypeError('Invalid number'); } const currencyValue = Number(this).toFixed(2); diff --git a/test/string.test.js b/test/string.test.js index 2e19faa..e91754e 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -1,5 +1,8 @@ /* eslint no-unused-expressions: 0 */ -const expect = require('chai').expect; +const chai = require('chai'); + +const expect = chai.expect; +const assert = chai.assert; require('../src/string'); @@ -77,13 +80,9 @@ describe('String Class', () => { expect('2535678'.toCurrency()).to.equal('2,535,678.00'); expect('1234567.'.toCurrency()).to.equal('1,234,567.00'); }); - it('returns an error message if the string is not of a "number"', () => { - expect('Mother'.toCurrency()).to.deep.equal('Invalid Currency Format'); - expect('Andela'.toCurrency()).to.deep.equal('Invalid Currency Format'); - }); - it(`returns an error message if the string is not in a - currency format`, () => { - expect('15248.15.45'.toCurrency()).to.deep.equal('Invalid Currency Format'); + it('should throw an error for invalid numbers', () => { + assert.throws('Mother'.toCurrency, TypeError, 'Invalid number'); + assert.throws('15248.15.45'.toCurrency, TypeError, 'Invalid number'); }); }); From 3803114fd7eb219fe2f53d58cf22d6b559d1a885 Mon Sep 17 00:00:00 2001 From: victor nwaiwu Date: Thu, 19 Jan 2017 16:48:16 +0100 Subject: [PATCH 5/6] refactor: refactor front end to display error --- public/index.html | 10 +++++----- public/js/app.js | 20 ++++++-------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/public/index.html b/public/index.html index 995fcd4..92d6d52 100644 --- a/public/index.html +++ b/public/index.html @@ -135,7 +135,7 @@

How To Use

Result
-

{{error}}

+
{{result}}
@@ -144,20 +144,20 @@
Result
- +
-
-
+
diff --git a/public/js/app.js b/public/js/app.js index c816917..54821dd 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -18,21 +18,13 @@ angular.module('StringExtension', []) 'isDigit', 'doubleCheck' ]; - $scope.result = ''; - $scope.error = ''; - $scope.getResult = (input, selected) => { - if (input === '' || input === undefined) { - $scope.result = ''; - $scope.error = 'You did not enter any text'; - return; - } else if (selected === undefined) { - $scope.result = ''; - $scope.error = 'You did not select a method'; - return; + $scope.getResult = () => { + try { + $scope.input && $scope.selected ? ($scope.result = $scope.input[$scope.selected]()) : ''; + } catch (error) { + $scope.result = error.message; } - $scope.error = ''; - $scope.result = input[selected](); - return; + return $scope.result; } }]) \ No newline at end of file From f77b175610e3bd52073b32408802274cb21cbce2 Mon Sep 17 00:00:00 2001 From: victor nwaiwu Date: Thu, 19 Jan 2017 16:55:37 +0100 Subject: [PATCH 6/6] style: fix import/no unresolved module issues with hound --- test/string.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/string.test.js b/test/string.test.js index e91754e..8f1df5e 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -1,3 +1,4 @@ +/* eslint amd:true */ /* eslint no-unused-expressions: 0 */ const chai = require('chai');