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 @@
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
diff --git a/src/string.js b/src/string.js
index 1dd5211..28212f0 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)) {
+ throw new TypeError('Invalid number');
}
- 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,');
},
/**
diff --git a/test/string.test.js b/test/string.test.js
index ccd8d45..8f1df5e 100644
--- a/test/string.test.js
+++ b/test/string.test.js
@@ -1,6 +1,9 @@
+/* eslint amd:true */
/* eslint no-unused-expressions: 0 */
-const mocha = require('mocha');
-const expect = require('chai').expect;
+const chai = require('chai');
+
+const expect = chai.expect;
+const assert = chai.assert;
require('../src/string');
@@ -76,18 +79,18 @@ 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');
+ it('should throw an error for invalid numbers', () => {
+ assert.throws('Mother'.toCurrency, TypeError, 'Invalid number');
+ assert.throws('15248.15.45'.toCurrency, TypeError, 'Invalid number');
});
});
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');
});
});