diff options
| author | Matt R. Wilson <[email protected]> | 2018-03-10 21:13:22 -0700 |
|---|---|---|
| committer | Matt R. Wilson <[email protected]> | 2018-10-15 07:44:31 -0600 |
| commit | 56d7ccbce18164706447c2c67c2e87b3d0016462 (patch) | |
| tree | 5cc331c9523deb116327985fc87230f9057c1ba1 /test | |
| parent | 220e5cbff3ac8bd437619c5df712bbfb8622895d (diff) | |
| download | faker-56d7ccbce18164706447c2c67c2e87b3d0016462.tar.xz faker-56d7ccbce18164706447c2c67c2e87b3d0016462.zip | |
Bugfix random.shuffle last element.
There was a bug in the implementation of the modern Fisher–Yates
algorithm in that `i` was being decremented prematurely in each loop.
This caused the last element of the array to never be swapped
and therefore remain the last element.
Updated doc to denote the provided array is shuffled in place.
Diffstat (limited to 'test')
| -rw-r--r-- | test/helpers.unit.js | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/test/helpers.unit.js b/test/helpers.unit.js index b9d559dd..058b8de0 100644 --- a/test/helpers.unit.js +++ b/test/helpers.unit.js @@ -43,6 +43,19 @@ describe("helpers.js", function () { var shuffled = faker.helpers.shuffle([]); assert.ok(shuffled.length === 0); }); + + it("mutates the input array in place", function () { + var input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; + var shuffled = faker.helpers.shuffle(input); + assert.deepEqual(shuffled, input); + }); + + it("all items shuffled as expected when seeded", function () { + var input = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; + faker.seed(100); + var shuffled = faker.helpers.shuffle(input); + assert.deepEqual(shuffled, ["b", "e", "a", "d", "j", "i", "h", "c", "g", "f"]); + }); }); describe("slugify()", function () { |
