Skip to content

Commit

Permalink
Properly guard against options guards calling true on D objects
Browse files Browse the repository at this point in the history
  • Loading branch information
alxjrvs committed Sep 24, 2024
1 parent bebcd44 commit 488f1a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function isDicePoolOptions(
): argument is RandsumRollOptions {
return (
typeof argument === 'object' &&
argument instanceof D === false &&
(argument as RandsumRollOptions).sides !== undefined
)
}
Expand Down
31 changes: 12 additions & 19 deletions src/models/ArgumentsModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,18 @@ function formDicePools<Sides extends string | number = string | number>(
}

function toOptions(argument: RandsumRollArgument): RandsumRollOptions {
if (isDicePoolOptions(argument)) {
return argument
}

if (isD(argument)) {
return argument.toOptions()
}

if (isDiceNotationArg(argument)) {
return NotationModel.toOptions(argument)
}

const sides = Array.isArray(argument)
? argument.map(String)
: Number(argument)

return {
quantity: 1,
sides
switch (true) {
case isDicePoolOptions(argument):
return argument
case isD(argument):
return argument.toOptions()
case isDiceNotationArg(argument):
return NotationModel.toOptions(argument)
default:
return {
quantity: 1,
sides: Array.isArray(argument) ? argument.map(String) : Number(argument)
}
}
}

Expand Down

0 comments on commit 488f1a8

Please sign in to comment.