diff --git a/README.md b/README.md index d52401c..88a30b4 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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' diff --git a/lib/index.js b/lib/index.js index 5036827..1780fd8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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. diff --git a/test/group-test.js b/test/group-test.js index d3a1eda..c80762a 100644 --- a/test/group-test.js +++ b/test/group-test.js @@ -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(); }); }); @@ -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();