diff options
| author | Marak <[email protected]> | 2016-09-25 21:43:01 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2016-09-25 21:43:01 -0400 |
| commit | 2ad5b70d4134e4b68a64d32cc3c9a9322ef6af9a (patch) | |
| tree | fff9711787c897cf696d8d90a30a1d1d86791f2e | |
| parent | 1dc26b11873a5e0479047c2ce57bfa48b8a19fce (diff) | |
| parent | 7e888f758192592a969c068943823526555dc34a (diff) | |
| download | faker-2ad5b70d4134e4b68a64d32cc3c9a9322ef6af9a.tar.xz faker-2ad5b70d4134e4b68a64d32cc3c9a9322ef6af9a.zip | |
Merge pull request #405 from HowWeRollingham/master
[fix] Don't throw in helpers.shuffle on empty array
| -rw-r--r-- | lib/helpers.js | 3 | ||||
| -rw-r--r-- | test/helpers.unit.js | 5 |
2 files changed, 8 insertions, 0 deletions
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 () { |
