-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change around output of ParseRollArgument, Rename to parameterizeRoll…
…Argument (#592)
- Loading branch information
Showing
7 changed files
with
86 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
export * from './Die' | ||
export { default as roll } from './roll' | ||
export { | ||
default as parseRollArguments, | ||
parseRollArgument | ||
} from './roll/parse-roll-arguments' | ||
export { default as parseRollArguments } from './roll/parse-roll-arguments' | ||
export { default as parameterizeRollArgument } from './roll/parse-roll-arguments/parameterize-roll-argument' | ||
export { default as generateRollResult } from './roll/generate-roll-result' | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { coreNotationPattern } from '~constants' | ||
import { DiceNotation, DicePoolOptions, RollArgument } from '~types' | ||
|
||
export const isDiceNotation = ( | ||
argument: unknown | ||
): argument is DiceNotation<number> | DiceNotation<string> => | ||
!!coreNotationPattern.test(String(argument)) | ||
|
||
export const isCustomSides = ( | ||
argument: RollArgument | undefined | ||
): argument is string[] => | ||
Array.isArray(argument) && argument.every((arg) => typeof arg === 'string') | ||
|
||
export const isDicePoolOptions = ( | ||
argument: unknown | ||
): argument is DicePoolOptions<number> | DicePoolOptions<string> => | ||
typeof argument === 'object' && | ||
(argument as DicePoolOptions<number> | DicePoolOptions<string>).sides !== | ||
undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/roll/parse-roll-arguments/parameterize-roll-argument.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CoreRollArgument, DicePoolOptions, DicePoolParameters } from '~types' | ||
import { dieFactory } from '~Die' | ||
import formatDescription from './format-description.ts/index.ts' | ||
import formatNotation from './format-notation/index.ts' | ||
import { isDiceNotation, isDicePoolOptions } from './guards.ts' | ||
import parseNotation from './parse-notation.ts' | ||
|
||
function parseDiceOptions( | ||
options: CoreRollArgument | undefined | ||
): DicePoolOptions<string> | DicePoolOptions<number> { | ||
if (isDicePoolOptions(options)) { | ||
return options | ||
} | ||
|
||
if (isDiceNotation(options)) { | ||
return parseNotation(options) | ||
} | ||
|
||
const defaultQuantity = 1 | ||
|
||
if (Array.isArray(options)) { | ||
return { | ||
quantity: defaultQuantity, | ||
sides: options.map(String) | ||
} | ||
} | ||
|
||
return { | ||
quantity: defaultQuantity, | ||
sides: Number(options || 20) | ||
} | ||
} | ||
|
||
function parameterizeRollArgument<D extends number | string>( | ||
argument: CoreRollArgument | undefined | ||
): DicePoolParameters<D> { | ||
const options = parseDiceOptions(argument) | ||
const die = dieFactory(options.sides) | ||
return { | ||
options, | ||
argument, | ||
die, | ||
notation: formatNotation(options), | ||
description: formatDescription(options) | ||
} as DicePoolParameters<D> | ||
} | ||
|
||
export default parameterizeRollArgument |