-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shuffle and random utility functions to ArrayUtils
- Loading branch information
Showing
7 changed files
with
99 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Returns a random element from the given array. | ||
* @param arr The array from which to select a random element. | ||
* @returns A random element from the array. | ||
*/ | ||
export function random<T>(arr: T[]): T { | ||
return arr[Math.floor(Math.random() * arr.length)]; | ||
} |
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,15 @@ | ||
/** | ||
* Shuffles the elements of an array in place. | ||
* | ||
* @param arr - The array to be shuffled. | ||
* @returns A new array with the elements shuffled. | ||
* @template T - The type of elements in the array. | ||
*/ | ||
export function shuffle<T>(arr: T[]): T[] { | ||
const result = [...arr]; | ||
for (let i = 0; i < result.length; i++) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[result[i], result[j]] = [result[j], result[i]]; | ||
} | ||
return result; | ||
} |
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,16 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { random } from "../../src/array/random"; | ||
|
||
describe("random", () => { | ||
it("should return a random element from the array", () => { | ||
const arr = [1, 2, 3, 4, 5]; | ||
const result = random(arr); | ||
expect(arr.includes(result)).toBe(true); | ||
}); | ||
|
||
it("should return undefined if the array is empty", () => { | ||
const arr: number[] = []; | ||
const result = random(arr); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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,29 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { shuffle } from "../../src/array/shuffle"; | ||
|
||
describe("shuffle", () => { | ||
it("should shuffle the elements in the array", () => { | ||
const arr = [1, 2, 3, 4, 5]; | ||
const result = shuffle(arr); | ||
expect(result).not.toEqual(arr); | ||
expect(result).toHaveLength(arr.length); | ||
expect(result).toContain(1); | ||
expect(result).toContain(2); | ||
expect(result).toContain(3); | ||
expect(result).toContain(4); | ||
expect(result).toContain(5); | ||
}); | ||
|
||
it("should return an empty array if the input array is empty", () => { | ||
const arr: number[] = []; | ||
const result = shuffle(arr); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should not modify the original array", () => { | ||
const arr = [1, 2, 3, 4, 5]; | ||
const originalArr = [...arr]; | ||
shuffle(arr); | ||
expect(arr).toEqual(originalArr); | ||
}); | ||
}); |