Skip to content

Commit

Permalink
Merge pull request #405 from HowWeRollingham/master
Browse files Browse the repository at this point in the history
[fix] Don't throw in helpers.shuffle on empty array
  • Loading branch information
Marak authored Sep 26, 2016
2 parents 1dc26b1 + 7e888f7 commit 2ad5b70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ var Helpers = function (faker) {
* @param {array} o
*/
self.shuffle = function (o) {
if (o.length === 0) {
return [];
}
o = o || ["a", "b", "c"];
for (var j, x, i = o.length-1; i; j = faker.random.number(i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
Expand Down
5 changes: 5 additions & 0 deletions test/helpers.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ describe("helpers.js", function () {
assert.ok(faker.random.number.calledWith(1));
faker.random.number.restore();
});

it("empty array returns empty array", function () {
var shuffled = faker.helpers.shuffle([]);
assert.ok(shuffled.length === 0);
});
});

describe("slugify()", function () {
Expand Down

0 comments on commit 2ad5b70

Please sign in to comment.