Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ <h4 class="center-align">How To Use</h4>
<div class="col l4 s12 m12 push-l7">
<div id="result-area" class="row">
<h5 class="center-align">Result</h5>
<p class="error center-align">{{error}}</p>
<!--<p class="error center-align">{{error}}</p>-->
<div id="result" class="center-align">
{{result}}
</div>
Expand All @@ -144,20 +144,20 @@ <h5 class="center-align">Result</h5>
<div class="col l5 s12 m12 pull-l3">
<div class="row">
<div class="input-field col l12">
<input placeholder="Enter text to test" id="input" type="text" class="validate" ng-model="input">
<input placeholder="Enter text to test" id="input" type="text" class="validate" ng-model="input" ng-change="{{ selected !== '' ? getResult() : '' }}">
<label for="input">String Input</label>
</div>
<div class="input-field col l12">
<select ng-model="selected">
<select ng-model="selected" ng-selected="{{ input !== '' ? getResult() : ''}}">
<option value="" disabled selected>Choose your option</option>
<option ng-repeat="option in options" ng-model="selected" value="{{option}}">{{option}}</option>
</select>
<label>Methods</label>
</div>
</div>
<div class="row">
<!--<div class="row">
<button class="waves-effect waves-light green btn" ng-click="getResult(input, selected)">Get Result</button>
</div>
</div>-->
</div>
</div>
</div>
Expand Down
20 changes: 6 additions & 14 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}])
19 changes: 7 additions & 12 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
},

/**
Expand All @@ -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,');
},

/**
Expand Down
17 changes: 10 additions & 7 deletions test/string.test.js
Original file line number Diff line number Diff line change
@@ -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');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to resolve path to module 'chai' import/no-unresolved


const expect = chai.expect;
const assert = chai.assert;

require('../src/string');

Expand Down Expand Up @@ -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');
});
});

Expand Down