From aa242f7a4bf0cfd15ebcb7f5dcd799d27de6792b Mon Sep 17 00:00:00 2001 From: Sherman Mui Date: Thu, 4 Jun 2015 11:43:49 -0700 Subject: Fix issue Marak/faker.js#214 with shuffle() Add a dubious spec. --- lib/helpers.js | 2 +- test/helpers.unit.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/helpers.js b/lib/helpers.js index 99a02e27..b4ef2248 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -39,7 +39,7 @@ exports.replaceSymbolWithNumber = function (string, symbol) { // takes an array and returns it randomized exports.shuffle = function (o) { o = o || ["a", "b", "c"]; - for (var j, x, i = o.length; i; j = faker.random.number(i), x = o[--i], o[i] = o[j], o[j] = x); + 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 425167e2..e58e04e7 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -21,6 +21,16 @@ describe("helpers.js", function () { }); }); + describe("shuffle()", function () { + it("the output is the same length as the input", function () { + sinon.spy(faker.random, 'number'); + var shuffled = faker.helpers.shuffle(["a", "b"]); + assert.ok(shuffled.length === 2); + assert.ok(faker.random.number.calledWith(1)); + faker.random.number.restore(); + }); + }); + describe("slugify()", function () { it("removes unwanted characters from URI string", function () { assert.equal(faker.helpers.slugify("Aiden.HarÂȘann"), "Aiden.Harann"); -- cgit v1.2.3