feat: throw on negative ratings

This commit is contained in:
Lukas Obermann
2026-02-26 00:06:03 +01:00
parent 4e8b54eacd
commit 31247de30b
2 changed files with 20 additions and 1 deletions
+10
View File
@@ -50,6 +50,7 @@ export const getAdventurePointsForActivation = (improvementCost: ImprovementCost
*
* This only calculates a single step. To calculate the sum of adventure points for going from and to an arbitrary rating, use {@link calculateAdventurePointsFromImprovementCost} with a rating range.
*
* @throws {RangeError} if the rating is negative, since adventure points are not defined for negative ratings.
* @example
* getAdventurePointsForRating("A", 1) // returns 1
* getAdventurePointsForRating("A", 12) // returns 1
@@ -60,6 +61,10 @@ export const getAdventurePointsForRating = (
improvementCost: ImprovementCost,
rating: number,
): number => {
if (rating < 0) {
throw new RangeError("Adventure points are not defined for negative ratings.")
}
const base = getBase(improvementCost)
const lastRatingOfSameValue = getLastRatingOfConstantValue(improvementCost)
@@ -73,12 +78,17 @@ export const getAdventurePointsForRating = (
* @param from The source rating to increment from.
* @param to The target rating to increment to.
* @returns The change in spent adventure points when changing the rating from the source rating to the target rating.
* @throws {RangeError} if at least one of the given ratings is negative, since adventure points are not defined for negative ratings.
*/
export const getAdventurePointsForRatingRange = (
improvementCost: ImprovementCost,
from: number,
to: number,
): number => {
if (from < 0 || to < 0) {
throw new RangeError("Adventure points are not defined for negative ratings.")
}
const base = getBase(improvementCost)
const lastRatingOfSameValue = getLastRatingOfConstantValue(improvementCost)
+10 -1
View File
@@ -1,4 +1,4 @@
import { equal } from "node:assert/strict"
import { equal, throws } from "node:assert/strict"
import { describe, it } from "node:test"
import {
getAdventurePointsForActivation,
@@ -35,6 +35,10 @@ describe("getAdventurePointsForRating", () => {
equal(getAdventurePointsForRating("E", 15), 30)
equal(getAdventurePointsForRating("E", 16), 45)
})
it("throws a RangeError for negative ratings", () => {
throws(() => getAdventurePointsForRating("A", -1), RangeError)
})
})
describe("getAdventurePointsForRatingRange", () => {
@@ -60,4 +64,9 @@ describe("getAdventurePointsForRatingRange", () => {
equal(getAdventurePointsForRatingRange("D", 15, 1), -80)
equal(getAdventurePointsForRatingRange("E", 16, 1), -270)
})
it("throws a RangeError for negative ratings", () => {
throws(() => getAdventurePointsForRatingRange("A", -1, 2), RangeError)
throws(() => getAdventurePointsForRatingRange("A", 1, -2), RangeError)
})
})