feat: throw on negative ratings
This commit is contained in:
@@ -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
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user