-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better randomInt, and arrayShuffle
- Loading branch information
1 parent
fb7dc6a
commit 42529a9
Showing
25 changed files
with
120 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { randomBytes } from 'crypto'; | ||
|
||
/** | ||
* Crypto.randomInt() function is not implemented by polyfill 'crypto-browserify' | ||
* @see https://github.com/browserify/crypto-browserify/issues/224 | ||
*/ | ||
export const getRandomInt = (min: number, max: number) => { | ||
if (min >= max) { | ||
throw new RangeError( | ||
`The value of "max" is out of range. It must be greater than the value of "min" (${min}). Received ${max}`, | ||
); | ||
} | ||
|
||
const randomValue = parseInt(randomBytes(4).toString('hex'), 16); | ||
|
||
return min + (randomValue % (max - min)); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* @deprecated Consider using `getRandomInt` which is cryptographically secure. | ||
*/ | ||
export const getWeakRandomNumberInRange = (min: number, max: number) => | ||
Math.floor(Math.random() * (max - min + 1)) + min; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { randomInt } from 'crypto'; | ||
|
||
import { getRandomInt } from '../src'; | ||
|
||
describe(getRandomInt.name, () => { | ||
it('raises same error as randomInt from crypto when max <= min', () => { | ||
const EXPECTED_ERROR = new RangeError( | ||
'The value of "max" is out of range. It must be greater than the value of "min" (0). Received -1', | ||
); | ||
|
||
expect(() => randomInt(0, -1)).toThrowError(EXPECTED_ERROR); | ||
expect(() => getRandomInt(0, -1)).toThrowError(EXPECTED_ERROR); | ||
}); | ||
|
||
it('returns same value when range is trivial', () => { | ||
expect(randomInt(0, 1)).toEqual(0); | ||
expect(getRandomInt(0, 1)).toEqual(0); | ||
|
||
expect(randomInt(100, 101)).toEqual(100); | ||
expect(getRandomInt(100, 101)).toEqual(100); | ||
}); | ||
|
||
it('returns same value when range is trivial', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
const result = getRandomInt(0, 100); | ||
expect(result).toBeGreaterThanOrEqual(0); | ||
expect(result).toBeLessThan(100); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.