Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to changelog and description to readme.
- Loading branch information
1 parent
839407f
commit 9b881c7
Showing
4 changed files
with
57 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = (input, options = {}) => { | ||
if (typeof input !== 'string') { | ||
throw new TypeError(`Expected a string, got ${typeof input}`); | ||
module.exports = (firstNr, secondNr) => { | ||
if (typeof firstNr !== 'number') { | ||
throw new TypeError(`Expected a number, got ${typeof firstNr}`); | ||
} | ||
|
||
return input + ' & ' + (options.postfix || 'rainbows'); | ||
if (typeof secondNr !== 'number') { | ||
throw new TypeError(`Expected a number, got ${typeof secondNr}`); | ||
} | ||
|
||
const percentageDiff = (secondNr - firstNr) / firstNr * 100; | ||
|
||
return Number(percentageDiff.toFixed(2)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,36 @@ | ||
import test from 'ava'; | ||
import percentageDiff from '.'; | ||
|
||
test('title', t => { | ||
const err = t.throws(() => { | ||
percentageDiff(123); | ||
test('Validate argument types', t => { | ||
const err1 = t.throws(() => { | ||
percentageDiff('123', 12); | ||
}, TypeError); | ||
t.is(err.message, 'Expected a string, got number'); | ||
t.is(err1.message, 'Expected a number, got string'); | ||
const err2 = t.throws(() => { | ||
percentageDiff(123, '12'); | ||
}, TypeError); | ||
t.is(err2.message, 'Expected a number, got string'); | ||
}); | ||
|
||
test('First number is larger than second one', t => { | ||
t.is(percentageDiff(3595210, 738583), -79.46); | ||
t.is(percentageDiff(75, 50), -33.33); | ||
t.is(percentageDiff(45, 35), -22.22); | ||
}); | ||
|
||
test('Second number is larger than first one', t => { | ||
t.is(percentageDiff(12, 32), 166.67); | ||
t.is(percentageDiff(27, 96), 255.56); | ||
t.is(percentageDiff(50, 75), 50); | ||
t.is(percentageDiff(35, 45), 28.57); | ||
}); | ||
|
||
test('Both numbers are equal', t => { | ||
t.is(percentageDiff(12, 12), 0); | ||
t.is(percentageDiff(-5, -5.0), 0); | ||
}); | ||
|
||
t.is(percentageDiff('unicorns'), 'unicorns & rainbows'); | ||
test('Handles zero division', t => { | ||
t.is(percentageDiff(0, 0), NaN); | ||
t.is(percentageDiff(0, 2), Infinity); | ||
}); |