Skip to content

Commit 27917b7

Browse files
committed
feat: add precision option to toHuman()
BREAKING CHANGES: `toHumman(timeUnit)` -> `toHuman({ timeUnit })`
1 parent 0a60d7e commit 27917b7

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ Curent speed.
5555
### StreamSpeed#avg
5656
Current average speed.
5757

58-
### StreamSpeed.toHuman(bytes, timeUnit)
58+
### StreamSpeed.toHuman(bytes, options)
5959
Convenient method to convert `bytes` to a human readable string.
6060

6161
```js
62-
StreamSpeed.toHuman(1500); // 1.46KB
63-
StreamSpeed.toHuman(1024 * 1024); // 1MB
64-
StreamSpeed.toHuman(1024 * 1024 * 20.5, 's'); // 20.5MB/s
62+
StreamSpeed.toHuman(1500); // 1.46KB
63+
StreamSpeed.toHuman(1024 * 1024); // 1MB
64+
StreamSpeed.toHuman(1024 * 1024 * 20.5, { timeUnit: 's' }); // 20.5MB/s
65+
StreamSpeed.toHuman(1024 * 1024 * 20.5, { precision: 3 }); // 20.50MB
6566
```
6667

6768
### Event: 'speed'

lib/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = class StreamSpeed extends EventEmitter {
99
*
1010
* @constructor
1111
* @extends {EventEmitter}
12-
* @param {number} per The time unit speed will be measured in.
12+
* @param {number?} per The time unit speed will be measured in.
1313
*/
1414
constructor(per) {
1515
super();
@@ -121,14 +121,18 @@ module.exports = class StreamSpeed extends EventEmitter {
121121
* Thank you Amir from StackOverflow.
122122
*
123123
* @param {number} bytes
124+
* @param {Object} options
125+
* {string} timeUnit
126+
* {number?} precision
124127
* @return {string}
125128
*/
126-
static toHuman(bytes, timeUnit) {
129+
static toHuman(bytes, options = {}) {
127130
const units = ' KMGTPEZYXWVU';
128131
if (bytes <= 0) { return '0'; }
129-
timeUnit = timeUnit ? '/' + timeUnit : '';
132+
options.timeUnit = options.timeUnit ? '/' + options.timeUnit : '';
130133
const t2 = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), 12);
131-
return (Math.round(bytes * 100 / Math.pow(1024, t2)) / 100) +
132-
units.charAt(t2).replace(' ', '') + 'B' + timeUnit;
134+
let rate = Math.round(bytes * 100 / Math.pow(1024, t2)) / 100;
135+
rate = options.precision ? rate.toPrecision(options.precision) : rate;
136+
return rate + units.charAt(t2).replace(' ', '') + 'B' + options.timeUnit;
133137
}
134138
};

test/util-test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,29 @@ describe('StreamSpeed.toHuman()', () => {
88
});
99

1010
it('Turns bytes into human readable size', () => {
11-
assert.equal(StreamSpeed.toHuman(1024, 's'), '1KB/s');
12-
assert.equal(StreamSpeed.toHuman(1024 * 1024 * 1.5, 's'), '1.5MB/s');
13-
});
14-
15-
it('Turns bytes into human readable size (no time unit)', () => {
1611
assert.equal(StreamSpeed.toHuman(1024), '1KB');
1712
assert.equal(StreamSpeed.toHuman(1024 * 1024 * 1.5), '1.5MB');
1813
});
14+
15+
describe('With `timeUnit`', () => {
16+
it('Turns bytes into human readable size', () => {
17+
assert.equal(StreamSpeed.toHuman(1024, {
18+
timeUnit: 's'
19+
}), '1KB/s');
20+
assert.equal(StreamSpeed.toHuman(1024 * 1024 * 1.5, {
21+
timeUnit: 's'
22+
}), '1.5MB/s');
23+
});
24+
});
25+
26+
describe('With `precision`', () => {
27+
it('Formats speed with requested number of precision', () => {
28+
assert.equal(StreamSpeed.toHuman(1024 * 1024 * 1.5, {
29+
precision: 3
30+
}), '1.50MB');
31+
assert.equal(StreamSpeed.toHuman(1024 * 1024 * 1.1234567, {
32+
precision: 2
33+
}), '1.1MB');
34+
});
35+
});
1936
});

0 commit comments

Comments
 (0)