From 7e888f758192592a969c068943823526555dc34a Mon Sep 17 00:00:00 2001 From: Ben Douglas Date: Tue, 30 Aug 2016 13:08:31 +0100 Subject: Prevented a crash on running helpers.shuffle on an empty array --- lib/helpers.js | 3 +++ test/helpers.unit.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/helpers.js b/lib/helpers.js index ac3398d7..cec043bc 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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; diff --git a/test/helpers.unit.js b/test/helpers.unit.js index fa2f4a90..707575ff 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -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 () { -- cgit v1.2.3