From 13103eff7dcce6f64da1dd8a6ac1d75f07d5d493 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Thu, 9 May 2024 12:33:31 +0200 Subject: [PATCH] util/random/rollDiceDifferentValue and randomEnumDifferentValue: Allow to provide multiple current values --- src/util/random/randomEnumDifferentValue.ts | 13 +++++++------ src/util/random/rollDiceDifferentValue.ts | 12 ++++++------ .../random/randomEnumDifferentValue.spec.ts | 10 ++++++++++ .../util/random/rollDiceDifferentValue.spec.ts | 17 +++++++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/util/random/randomEnumDifferentValue.ts b/src/util/random/randomEnumDifferentValue.ts index 3262a23..3c19e31 100644 --- a/src/util/random/randomEnumDifferentValue.ts +++ b/src/util/random/randomEnumDifferentValue.ts @@ -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> (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> (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) } } diff --git a/src/util/random/rollDiceDifferentValue.ts b/src/util/random/rollDiceDifferentValue.ts index 9e48db4..cd651fc 100644 --- a/src/util/random/rollDiceDifferentValue.ts +++ b/src/util/random/rollDiceDifferentValue.ts @@ -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) } } diff --git a/tests/unit/util/random/randomEnumDifferentValue.spec.ts b/tests/unit/util/random/randomEnumDifferentValue.spec.ts index e94c22c..fef8427 100644 --- a/tests/unit/util/random/randomEnumDifferentValue.spec.ts +++ b/tests/unit/util/random/randomEnumDifferentValue.spec.ts @@ -16,6 +16,12 @@ 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) }) @@ -23,6 +29,10 @@ describe('util/random/randomEnumDifferentValue.spec', () => { 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 { diff --git a/tests/unit/util/random/rollDiceDifferentValue.spec.ts b/tests/unit/util/random/rollDiceDifferentValue.spec.ts index 52faed3..ce7e3e8 100644 --- a/tests/unit/util/random/rollDiceDifferentValue.spec.ts +++ b/tests/unit/util/random/rollDiceDifferentValue.spec.ts @@ -12,6 +12,19 @@ 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) }) @@ -19,4 +32,8 @@ describe('util/random/rollDice', () => { it('D0', () => { expect(() => rollDiceDifferentValue(0, 0)).to.throw(Error) }) + + it('multi-too-much-current-values', () => { + expect(() => rollDiceDifferentValue(3, 1, 2, 3)).to.throw(Error) + }) })