Skip to content

Commit

Permalink
JS: Calculate percentage difference
Browse files Browse the repository at this point in the history
Add feature to changelog and description to readme.
  • Loading branch information
rodrigobdz committed Apr 23, 2019
1 parent 839407f commit 9b881c7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
10 changes: 8 additions & 2 deletions changelog.md
Expand Up @@ -16,5 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

[unreleased]: https://github.com/rodrigobdz/percentage-diff/compare
<!-- Example: https://github.com/rodrigobdz/percentage-diff/compare/v1.0.0...HEAD -->
## [1.0.0] - 2019-04-24

### Added

- Calculate percentage difference between two numbers.

[unreleased]: https://github.com/rodrigobdz/percentage-diff/compare/v1.0.0...HEAD
[1.0.0]: https://github.com/rodrigobdz/percentage-diff/compare/839407f777811173c2bbb62af850e3fc6ee07ebf...v1.0.0
14 changes: 10 additions & 4 deletions index.js
@@ -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));
};
25 changes: 9 additions & 16 deletions readme.md
Expand Up @@ -13,30 +13,23 @@ $ npm install percentage-diff
```js
const percentageDiff = require('percentage-diff');

percentageDiff('unicorns');
//=> 'unicorns & rainbows'
percentageDiff(50, 75);
//=> 50%
percentageDiff(45,35);
//=> -22.22%
```

## API

### percentageDiff(input, [options])
### percentageDiff(firstNr, secondNr)

#### input
#### firstNr

Type: `string`
Type: `number`

Lorem ipsum.
#### secondNr

#### options

Type: `Object`

##### foo

Type: `boolean`<br>
Default: `false`

Lorem ipsum.
Type: `number`

## Credits

Expand Down
35 changes: 30 additions & 5 deletions test.js
@@ -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);
});

0 comments on commit 9b881c7

Please sign in to comment.