Skip to content

Commit

Permalink
prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
philihp committed Apr 29, 2020
1 parent 10584e0 commit 2ff4ce2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
4 changes: 2 additions & 2 deletions babel.config.js
@@ -1,3 +1,3 @@
const presets = ["@babel/env"];
const presets = ['@babel/env']

module.exports = { presets };
module.exports = { presets }
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -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]
Expand All @@ -13,4 +13,4 @@ const shuffle = (deck, random = Math.random) => {
return shuffled
}

export default shuffle;
export default shuffle
46 changes: 18 additions & 28 deletions test/test.js
Expand Up @@ -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)
Expand All @@ -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', () => {
Expand All @@ -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())
})

})

0 comments on commit 2ff4ce2

Please sign in to comment.