Skip to content

Commit

Permalink
[fix] Bail early for negative howMany values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Keslin committed Jun 28, 2018
1 parent fe168f5 commit 4f6ef3d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @public
*/
const randomFromCollection = module.exports = function randomFromCollection(collection, howMany) {
if (howMany === 0)
if (howMany < 1)
return [];

const collectionSize = typeof collection.size === 'number' ? collection.size : collection.length;
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('random-from-collection', function () {
assume(result).has.length(0);
});

it('returns an empty array when howMany is -1', function () {
const result = randomFromCollection(fixtureSet, -1);
assume(result).is.an('array');
assume(result).has.length(0);
});

it('returns an empty array when collection is empty', function () {
const result = randomFromCollection(new Set([]), 5);
assume(result).is.an('array');
Expand Down

0 comments on commit 4f6ef3d

Please sign in to comment.