Skip to content

Commit

Permalink
util/random/rollDiceDifferentValue and randomEnumDifferentValue: Allo…
Browse files Browse the repository at this point in the history
…w to provide multiple current values
  • Loading branch information
stefanseifert committed May 9, 2024
1 parent 33efcbc commit 7cfc3d3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
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)
})
})

0 comments on commit 7cfc3d3

Please sign in to comment.