generated from sapphiredev/sapphire-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utilities): add pickRandom (#486)
Co-authored-by: Jeroen Claassens <support@favware.tech>
- Loading branch information
1 parent
a4acb69
commit 20823c9
Showing
4 changed files
with
47 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Picks a random element from an array | ||
* @param array The array to pick a random element from | ||
* @param amount Amount of values to obtain randomly (default: 1) | ||
*/ | ||
export function pickRandom<T>(array: readonly T[], amount?: 1): T; | ||
export function pickRandom<T>(array: readonly T[], amount: number): T[]; | ||
export function pickRandom<T>(array: readonly T[], amount = 1): T | T[] { | ||
const arr = [...array]; | ||
|
||
if (typeof amount === 'undefined' || amount === 1) { | ||
return arr[Math.floor(Math.random() * arr.length)]; | ||
} | ||
|
||
if (!arr.length || !amount) { | ||
return []; | ||
} | ||
|
||
return Array.from({ length: Math.min(amount, arr.length) }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]); | ||
} |
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,21 @@ | ||
import { pickRandom } from '../src'; | ||
|
||
describe('pickRandom', () => { | ||
test('GIVEN array THEN picks one random element', () => { | ||
const array = ['a', 'b', 'c']; | ||
expect(array).toContain(pickRandom(array)); | ||
}); | ||
|
||
test('GIVEN count of one THEN picks one random element', () => { | ||
const array = ['a', 'b', 'c']; | ||
expect(array).toContain(pickRandom(array, 1)); | ||
}); | ||
|
||
test('GIVEN count of two THEN picks two random elements', () => { | ||
const array = ['a', 'b', 'c']; | ||
const picked = pickRandom(array, 2); | ||
|
||
expect(picked).toHaveLength(2); | ||
expect(array).toEqual(expect.arrayContaining(picked)); | ||
}); | ||
}); |