aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarak <[email protected]>2016-09-25 21:43:01 -0400
committerGitHub <[email protected]>2016-09-25 21:43:01 -0400
commit2ad5b70d4134e4b68a64d32cc3c9a9322ef6af9a (patch)
treefff9711787c897cf696d8d90a30a1d1d86791f2e
parent1dc26b11873a5e0479047c2ce57bfa48b8a19fce (diff)
parent7e888f758192592a969c068943823526555dc34a (diff)
downloadfaker-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.js3
-rw-r--r--test/helpers.unit.js5
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 () {