Skip to content

Commit b7c0b0d

Browse files
author
Rodrigo Bermudez Schettino
committed
Add helper toPercentage
Returns strings with percentage sign appended.
1 parent 6802153 commit b7c0b0d

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

changelog.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
## [Unreleased]
1818

19+
## [1.1.0] - 2019-04-24
20+
21+
### Added
22+
23+
- Implement helper `toPercentage` to return strings with percentage sign appended.
24+
1925
## [1.0.0] - 2019-04-24
2026

2127
### Added
2228

2329
- Calculate percentage difference between two numbers.
2430

25-
[unreleased]: https://github.com/rodrigobdz/percentage-diff/compare/v1.0.0...HEAD
31+
[unreleased]: https://github.com/rodrigobdz/percentage-diff/compare/v1.1.0...HEAD
32+
[1.1.0]: https://github.com/rodrigobdz/percentage-diff/compare/v1.0.0...v1.1.0
2633
[1.0.0]: https://github.com/rodrigobdz/percentage-diff/compare/839407f777811173c2bbb62af850e3fc6ee07ebf...v1.0.0

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict';
22

3-
module.exports = (firstNr, secondNr) => {
3+
function toPercentage(val) {
4+
return `${val}%`;
5+
}
6+
7+
function percentageDiff(firstNr, secondNr) {
48
if (typeof firstNr !== 'number') {
59
throw new TypeError(`Expected a number, got ${typeof firstNr}`);
610
}
@@ -12,4 +16,7 @@ module.exports = (firstNr, secondNr) => {
1216
const percentageDiff = (secondNr - firstNr) / firstNr * 100;
1317

1418
return Number(percentageDiff.toFixed(2));
15-
};
19+
}
20+
21+
module.exports = percentageDiff;
22+
module.exports.toPercentage = toPercentage;

readme.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ $ npm install percentage-diff
1414
const percentageDiff = require('percentage-diff');
1515

1616
percentageDiff(50, 75);
17+
//=> 50
18+
percentageDiff.toPercentage(50);
1719
//=> 50%
1820
percentageDiff(45,35);
19-
//=> -22.22%
21+
//=> -22.22
2022
```
2123

2224
## API
@@ -31,6 +33,12 @@ Type: `number`
3133

3234
Type: `number`
3335

36+
### percentageDiff.toPercentage(value)
37+
38+
#### value
39+
40+
Type: `number`
41+
3442
## Credits
3543

3644
* [generator-lnm](https://github.com/rodrigobdz/generator-lnm) - Awesome node module generator

test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ test('Handles zero division', t => {
3434
t.is(percentageDiff(0, 0), NaN);
3535
t.is(percentageDiff(0, 2), Infinity);
3636
});
37+
38+
test('Format as percentage', t => {
39+
t.is(percentageDiff.toPercentage(50), '50%');
40+
});

0 commit comments

Comments
 (0)