From 2ff4ce2c2cb720e3037b13f7e91b459678cff036 Mon Sep 17 00:00:00 2001 From: Philihp Busby Date: Wed, 29 Apr 2020 01:46:13 +0000 Subject: [PATCH] prettier formatting --- babel.config.js | 4 ++-- src/index.js | 4 ++-- test/test.js | 46 ++++++++++++++++++---------------------------- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/babel.config.js b/babel.config.js index 9c5fba5..05d10e8 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,3 +1,3 @@ -const presets = ["@babel/env"]; +const presets = ['@babel/env'] -module.exports = { presets }; +module.exports = { presets } diff --git a/src/index.js b/src/index.js index bd4cf41..37428b6 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,7 @@ const shuffle = (deck, random = Math.random) => { let dstIndex = 0 let shuffled = new Array(srcIndex) - while(srcIndex) { + while (srcIndex) { let randIndex = (srcIndex * random()) | 0 shuffled[dstIndex++] = clone[randIndex] clone[randIndex] = clone[--srcIndex] @@ -13,4 +13,4 @@ const shuffle = (deck, random = Math.random) => { return shuffled } -export default shuffle; +export default shuffle diff --git a/test/test.js b/test/test.js index cfc1ec8..3863f2b 100644 --- a/test/test.js +++ b/test/test.js @@ -2,36 +2,28 @@ import shuffle from '../src/index' describe('shuffle', () => { it('shuffles the array', () => { - const d1 = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ] - let pseudoRandomNumbers = [ - 0.19, 0.84, 0.02, 0.29, - 0.19, 0.85, 0.11, 0.02, - ] - const d2 = shuffle(d1, () => pseudoRandomNumbers.pop()); + const d1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] + let pseudoRandomNumbers = [0.19, 0.84, 0.02, 0.29, 0.19, 0.85, 0.11, 0.02] + const d2 = shuffle(d1, () => pseudoRandomNumbers.pop()) expect(d2).toEqual(expect.arrayContaining(d1)) expect(d2).toHaveLength(d1.length) }) it('does not mutate the source', () => { - const d1 = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ] - let pseudoRandomNumbers = [ - 0.19, 0.84, 0.02, 0.29, - 0.19, 0.85, 0.11, 0.02, - ] - shuffle(d1, () => pseudoRandomNumbers.pop()); - expect(d1).toMatchObject([ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ]) + const d1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] + let pseudoRandomNumbers = [0.19, 0.84, 0.02, 0.29, 0.19, 0.85, 0.11, 0.02] + shuffle(d1, () => pseudoRandomNumbers.pop()) + expect(d1).toMatchObject(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']) }) it('does a shallow clone', () => { const d1 = [ { name: 'Alice', money: 10 }, { name: 'Betty', money: 20 }, - { name: 'Cindy', money: 15 } - ] - let pseudoRandomNumbers = [ - 0.19, 0.84, 0.02, 0.29, + { name: 'Cindy', money: 15 }, ] - const d2 = shuffle(d1, () => pseudoRandomNumbers.pop()); + let pseudoRandomNumbers = [0.19, 0.84, 0.02, 0.29] + const d2 = shuffle(d1, () => pseudoRandomNumbers.pop()) // given the numbers above, should be alice, cindy, betty expect(d2[0]['name']).toEqual('Alice') expect(d2[0]['money']).toEqual(10) @@ -41,14 +33,11 @@ describe('shuffle', () => { }) it('can be sorted back into the source array', () => { - const d1 = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' ].sort() - let pseudoRandomNumbers = [ - 0.19, 0.84, 0.02, 0.29, - 0.19, 0.85, 0.11, 0.02, - ] - const d2 = shuffle(d1, () => pseudoRandomNumbers.pop()); - const d3 = d2.sort(); - expect(d3).toMatchObject(d1); + const d1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'].sort() + let pseudoRandomNumbers = [0.19, 0.84, 0.02, 0.29, 0.19, 0.85, 0.11, 0.02] + const d2 = shuffle(d1, () => pseudoRandomNumbers.pop()) + const d3 = d2.sort() + expect(d3).toMatchObject(d1) }) it('scales calls to random linearly', () => { @@ -59,9 +48,10 @@ describe('shuffle', () => { }) it('works with massive noise', () => { - const d1 = Array(500000).fill().map((e,i) => i) + const d1 = Array(500000) + .fill() + .map((e, i) => i) const d2 = shuffle(d1) expect(d1.sort()).toMatchObject(d2.sort()) }) - })