-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add utils/sample helper function * Use sample() helper in random response scripts * Update type documentation to be templated, document undefined return value * Add clamping around Math.random() to see if it addresses codeQL vuln
- Loading branch information
1 parent
89efedf
commit f49327a
Showing
8 changed files
with
51 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Returns a randomly-selected item from an array | ||
* @template T | ||
* @param {T[]} arr Array of values to be sampled | ||
* @param {Number} [randomValue] random value that can be injected for more deterministic behavior | ||
* @return {T=} an item from the array (or undefined if empty array) | ||
*/ | ||
function sample(arr, randomValue = Math.random()) { | ||
return arr[ | ||
Math.min(Math.max(0, Math.floor(randomValue * arr.length)), arr.length - 1) | ||
]; | ||
} | ||
|
||
module.exports = sample; |
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 @@ | ||
const sample = require("./sample"); | ||
|
||
describe("utils / sample", () => { | ||
const arr = ["a", "b", "c", "d", "e"]; | ||
|
||
it("selects an item from an array based on randomValue and clamps it to the array bounds", () => { | ||
expect(sample(arr, 0)).toEqual("a"); | ||
expect(sample(arr, 0.999)).toEqual("e"); | ||
expect(sample(arr, -1)).toEqual("a"); | ||
expect(sample(arr, 100)).toEqual("e"); | ||
}); | ||
|
||
it("still returns a random item when randomValue is not provided", () => { | ||
expect(arr).toContain(sample(arr)); | ||
}); | ||
|
||
it("returns undefined when an array is empty", () => { | ||
expect(sample([])).toBeUndefined(); | ||
}); | ||
}); |