-
-
Notifications
You must be signed in to change notification settings - Fork 0
Dev #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Dev #190
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1907de8
chore(deps): updated dependencies
webdeveric e5b0c35
fix(counter): updated return type
webdeveric 10f7f41
test(counter): updated tests
webdeveric 70ccba0
feat: added `randomInt()`
webdeveric 75c139d
feat: added `getRandomItem()`
webdeveric 600cc93
chore: updated exports
webdeveric b8dcaf7
chore: updated test description
webdeveric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 { randomInt } from './randomInt.js'; | ||
|
|
||
| export function getRandomItem(input: []): undefined; | ||
|
|
||
| export function getRandomItem<T>(input: [T, ...T[]]): T; | ||
|
|
||
| export function getRandomItem<T>(input: T[]): T | undefined { | ||
| switch (input.length) { | ||
| case 0: | ||
| return; | ||
| case 1: | ||
| return input[0]; | ||
| default: | ||
| return input[randomInt(0, input.length)]; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,37 @@ | ||
| import { assertIsInteger } from './assertion/assertIsInteger.js'; | ||
| import { isObject } from './predicate/isObject.js'; | ||
|
|
||
| import type { RequireAtLeastOne } from './types/records.js'; | ||
| import type { Pretty } from './types/utils.js'; | ||
|
|
||
| export type RandomIntOptions = Pretty< | ||
| RequireAtLeastOne<{ | ||
| // Inclusive | ||
| min: number; | ||
| // Exclusive | ||
| max: number; | ||
| }> | ||
| >; | ||
|
|
||
| export function randomInt(options?: RandomIntOptions): number; | ||
|
|
||
| export function randomInt(min: number, max?: number): number; | ||
|
|
||
| export function randomInt(arg1?: number | RandomIntOptions, arg2?: number): number { | ||
| const { min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER } = isObject(arg1) | ||
| ? arg1 | ||
| : { min: arg1, max: arg2 }; | ||
|
|
||
| assertIsInteger(min, 'min must be an integer'); | ||
| assertIsInteger(max, 'max must be an integer'); | ||
|
|
||
| if (min === max) { | ||
| return min; | ||
| } | ||
|
|
||
| if (min > max) { | ||
| throw new RangeError('min must be less than max'); | ||
| } | ||
|
|
||
| return Math.floor(Math.random() * (max - min)) + min; | ||
| } |
This file contains hidden or 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 hidden or 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 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { getRandomItem } from '../src/getRandomItem.js'; | ||
|
|
||
| describe('getRandomItem()', () => { | ||
| it('Returns a random item from an array', () => { | ||
| expect(getRandomItem([1])).toEqual(1); | ||
|
|
||
| expect(getRandomItem([1, 2, 3])).toBeTypeOf('number'); | ||
| }); | ||
|
|
||
| it('Returns undefined when input is empty', () => { | ||
| expect(getRandomItem([])).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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,42 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { randomInt } from '../src/randomInt.js'; | ||
|
|
||
| describe('randomInt()', () => { | ||
| it('Returns early when min and max are the same', () => { | ||
| expect(randomInt(0, 0)).toEqual(0); | ||
| }); | ||
|
|
||
| it('Returns a random integer', () => { | ||
| const value = randomInt(100, 200); | ||
|
|
||
| expect(value).greaterThanOrEqual(100); | ||
| expect(value).lessThan(200); | ||
| }); | ||
|
|
||
| it('The returns value is inclusive of min and exclusive of max', () => { | ||
| expect(randomInt(0, 1)).toEqual(0); | ||
| expect(randomInt(-2, -1)).toEqual(-2); | ||
| expect(randomInt(10, 11)).toEqual(10); | ||
| }); | ||
|
|
||
| it('Takes an options object', () => { | ||
| const value = randomInt({ max: 1000 }); | ||
|
|
||
| expect(value).greaterThanOrEqual(Number.MIN_SAFE_INTEGER); | ||
| expect(value).lessThan(1000); | ||
| }); | ||
|
|
||
| it('Defaults to SAFE_INTEGER range', () => { | ||
| const value = randomInt(); | ||
|
|
||
| expect(value).greaterThanOrEqual(Number.MIN_SAFE_INTEGER); | ||
| expect(value).lessThan(Number.MAX_SAFE_INTEGER); | ||
| }); | ||
|
|
||
| it('Throws when min > max', () => { | ||
| expect(() => { | ||
| randomInt(100, 0); | ||
| }).toThrow(); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.