Skip to content

Commit

Permalink
feat: add StreamSpeed#getStreamSpeed()
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Apr 11, 2020
1 parent 868f2e9 commit 04603bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Removes stream from group.
### StreamSpeed#getSpeed()
Get current speed.

### StreamSpeed#getStreamSpeed(stream)
Get an individual's stream's current speed.

### StreamSpeed#getStreams()
Returns a list of all streams in the group.

Expand All @@ -60,7 +63,7 @@ Helper method to convert `bytes` to a human readable string.
StreamSpeed.toHuman(1500); // 1.46KB
StreamSpeed.toHuman(1024 * 1024); // 1MB
StreamSpeed.toHuman(1024 * 1024 * 20.5, { timeUnit: 's' }); // 20.5MB/s
StreamSpeed.toHuman(1024 * 1024 * 20.5, { precision: 3 }); // 20.50MB
StreamSpeed.toHuman(1024 * 1024 * 20.5, { precision: 3 }); // 20.50MB
```

### Event: 'speed'
Expand Down
14 changes: 14 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ module.exports = class StreamSpeed extends EventEmitter {
}


/**
* Get an individual stream's speed.
*
* @param {Readable} stream
*/
getStreamSpeed(stream) {
const meta = this._streams.find(m => m.stream === stream);
if (!meta) {
throw Error('Stream not found in group');
}
return meta.speedo.getSpeed();
}


/**
* Converts bytes to human readable unit.
* Thank you Amir from StackOverflow.
Expand Down
6 changes: 6 additions & 0 deletions test/group-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ describe('Create a group and write to it', () => {
assert.equal(spy.callCount, 2);
assert.deepEqual(spy.getCall(0).args, [500]);
assert.deepEqual(spy.getCall(1).args, [1000]);
assert.equal(group.getSpeed(), 1000);
assert.equal(group.getStreamSpeed(s1), 500);
assert.equal(group.getStreamSpeed(s2), 500);
done();
});
});
Expand All @@ -66,6 +69,9 @@ describe('Create a group and write to it', () => {
assert.equal(group.getStreams().length, 2);
s1.interval(100, 6, 200, { end: true });
s1.on('end', () => {
assert.throws(() => {
group.getStreamSpeed(s1);
}, /not found/);
assert.equal(group.getStreams().length, 1);
assert.equal(group.getSpeed(), 0);
done();
Expand Down

0 comments on commit 04603bb

Please sign in to comment.