Skip to content
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

util/random/rollDiceDifferentValue and randomEnumDifferentValue: Allow to provide multiple current values #9

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/util/random/randomEnumDifferentValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import randomEnum from './randomEnum'
/**
* Gets a random enum value from all available enums.
* @param anEnum Enum type
* @param currentValue Current value which will not be returned
* @param currentValue Current value(s) which will not be returned
* @returns Random enum value
*/
export default function randomEnumDifferentValue<T extends Record<string, unknown>> (anEnum: T, currentValue: T[keyof T]): T[keyof T] {
if (Object.keys(anEnum).length < 2 && Object.values(anEnum).length < 2) {
throw new Error(`Unable to randomly choose a different value from an enum with a single value`)
export default function randomEnumDifferentValue<T extends Record<string, unknown>> (anEnum: T, ...currentValue: T[keyof T][]): T[keyof T] {
const totalValues = Math.max(Object.keys(anEnum).length, Object.values(anEnum).length)
if (totalValues < 2 || currentValue.length > totalValues - 1) {
throw new Error(`Unable to randomly choose a different value from an enum with a single value.`)
}
const newValue = randomEnum(anEnum)
if (newValue != currentValue) {
if (!currentValue.includes(newValue)) {
return newValue
}
else {
return randomEnumDifferentValue(anEnum, currentValue)
return randomEnumDifferentValue(anEnum, ...currentValue)
}
}
12 changes: 6 additions & 6 deletions src/util/random/rollDiceDifferentValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import rollDice from './rollDice'
/**
* Rolls a dice, and ensures to return a different value than the given current value.
* @param maxValue Max. dice value
* @param currentValue Current value
* @param currentValue Current value(s)
* @returns Dice value
*/
export default function rollDiceDifferentValue(maxValue: number, currentValue: number) : number {
if (maxValue < 2) {
throw new Error(`Unable to roll a dice ${maxValue} with a different value`)
export default function rollDiceDifferentValue(maxValue: number, ...currentValue: number[]) : number {
if (maxValue < 2 || currentValue.length > maxValue - 1) {
throw new Error(`Unable to roll a dice ${maxValue} with a different value.`)
}
const newNumber = rollDice(maxValue)
if (newNumber != currentValue) {
if (!currentValue.includes(newNumber)) {
return newNumber
}
else {
return rollDiceDifferentValue(maxValue, currentValue)
return rollDiceDifferentValue(maxValue, ...currentValue)
}
}
10 changes: 10 additions & 0 deletions tests/unit/util/random/randomEnumDifferentValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ describe('util/random/randomEnumDifferentValue.spec', () => {
expect(value).to.not.eq(StringEnum.THREE)
})

it('string enum multi', () => {
const value = randomEnumDifferentValue(StringEnum, StringEnum.THREE, StringEnum.TWO)

expect(value).to.eq(StringEnum.ONE)
})

it('single value int enum', () => {
expect(() => randomEnumDifferentValue(SingleValueIntEnum, SingleValueIntEnum.ONE)).to.throw(Error)
})

it('single value string enum', () => {
expect(() => randomEnumDifferentValue(SingleValueStringEnum, SingleValueStringEnum.ONE)).to.throw(Error)
})

it('multi-too-much-current-values', () => {
expect(() => randomEnumDifferentValue(StringEnum, StringEnum.ONE, StringEnum.TWO, StringEnum.THREE)).to.throw(Error)
})
})

enum IntEnum {
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/util/random/rollDiceDifferentValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@ describe('util/random/rollDice', () => {
}
})

it('D6-multi', () => {
for (let currentValue=1; currentValue<6; currentValue++) {
const currentValues = []
for (let i=1; i<=currentValue; i++) {
currentValues.push(i)
}
const value = rollDiceDifferentValue(6, ...currentValues)

expect(value).greaterThan(currentValue)
expect(value).lessThanOrEqual(6)
}
})

it('D1', () => {
expect(() => rollDiceDifferentValue(1, 1)).to.throw(Error)
})

it('D0', () => {
expect(() => rollDiceDifferentValue(0, 0)).to.throw(Error)
})

it('multi-too-much-current-values', () => {
expect(() => rollDiceDifferentValue(3, 1, 2, 3)).to.throw(Error)
})
})