Skip to content

Commit

Permalink
Add helper toPercentage
Browse files Browse the repository at this point in the history
Returns strings with percentage sign appended.
  • Loading branch information
rodrigobdz committed Apr 23, 2019
1 parent 6802153 commit b7c0b0d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2019-04-24

### Added

- Implement helper `toPercentage` to return strings with percentage sign appended.

## [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
[unreleased]: https://github.com/rodrigobdz/percentage-diff/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/rodrigobdz/percentage-diff/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/rodrigobdz/percentage-diff/compare/839407f777811173c2bbb62af850e3fc6ee07ebf...v1.0.0
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

module.exports = (firstNr, secondNr) => {
function toPercentage(val) {
return `${val}%`;
}

function percentageDiff(firstNr, secondNr) {
if (typeof firstNr !== 'number') {
throw new TypeError(`Expected a number, got ${typeof firstNr}`);
}
Expand All @@ -12,4 +16,7 @@ module.exports = (firstNr, secondNr) => {
const percentageDiff = (secondNr - firstNr) / firstNr * 100;

return Number(percentageDiff.toFixed(2));
};
}

module.exports = percentageDiff;
module.exports.toPercentage = toPercentage;
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ $ npm install percentage-diff
const percentageDiff = require('percentage-diff');

percentageDiff(50, 75);
//=> 50
percentageDiff.toPercentage(50);
//=> 50%
percentageDiff(45,35);
//=> -22.22%
//=> -22.22
```

## API
Expand All @@ -31,6 +33,12 @@ Type: `number`

Type: `number`

### percentageDiff.toPercentage(value)

#### value

Type: `number`

## Credits

* [generator-lnm](https://github.com/rodrigobdz/generator-lnm) - Awesome node module generator
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ test('Handles zero division', t => {
t.is(percentageDiff(0, 0), NaN);
t.is(percentageDiff(0, 2), Infinity);
});

test('Format as percentage', t => {
t.is(percentageDiff.toPercentage(50), '50%');
});

0 comments on commit b7c0b0d

Please sign in to comment.