Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
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)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Trailing spaces not allowed no-trailing-spaces

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

fixed

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,');
},

/**
Expand Down
13 changes: 8 additions & 5 deletions test/string.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint no-unused-expressions: 0 */
const mocha = require('mocha');
const expect = require('chai').expect;

require('../src/string');
Expand Down Expand Up @@ -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');
});
});

Expand Down