Skip to content

Commit

Permalink
Change around output of ParseRollArgument, Rename to parameterizeRoll…
Browse files Browse the repository at this point in the history
…Argument (#592)
  • Loading branch information
alxjrvs authored May 4, 2024
1 parent bfd075b commit a6d3bbd
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "randsum",
"version": "4.1.1",
"version": "4.1.2",
"private": false,
"author": "Alex Jarvis",
"icon": "https://raw.githubusercontent.com/RANDSUM/randsum-ts/main/icon.webp",
Expand Down
6 changes: 5 additions & 1 deletion src/Die/factories.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { CustomSidesDie, StandardDie } from './constants'
import SingleDie from './single-die'

function dieFactory(sides: number): SingleDie<number>
function dieFactory(sides: string[]): SingleDie<string>
function dieFactory<D extends string | number>(
sides: D extends number ? number : string[]
): SingleDie<D>
function dieFactory(sides: number | string[]): SingleDie<string | number> {
function dieFactory(
sides: number | string[]
): SingleDie<string> | SingleDie<number> {
return Array.isArray(sides)
? new CustomSidesDie(sides.map(String))
: new StandardDie(Number(sides))
Expand Down
6 changes: 2 additions & 4 deletions src/index.ts
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'
6 changes: 4 additions & 2 deletions src/roll/parse-roll-arguments/format-notation/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DicePoolOptions } from '~types'
import { DiceNotation, DicePoolOptions } from '~types'
import {
capNotation,
dropNotation,
Expand Down Expand Up @@ -40,7 +40,9 @@ function formatCoreNotation({
return `${quantity}d${formattedSides}`
}

function formatNotation(options: DicePoolOptions<string | number>) {
function formatNotation(
options: DicePoolOptions<number> | DicePoolOptions<string>
) {
return `${formatCoreNotation(options)}${formatModifierNotation(options)}`
}

Expand Down
19 changes: 19 additions & 0 deletions src/roll/parse-roll-arguments/guards.ts
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
75 changes: 7 additions & 68 deletions src/roll/parse-roll-arguments/index.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,6 @@
import { coreNotationPattern } from '~constants'
import { dieFactory } from '~Die'
import {
CoreRollArgument,
DiceNotation,
DicePoolOptions,
DicePoolParameters,
RollArgument,
RollParameters
} from '~types'
import parseNotation from './parse-notation'
import formatNotation from './format-notation'
import formatDescription from './format-description.ts'

const isDicePoolOptions = (
argument: unknown
): argument is DicePoolOptions<number> | DicePoolOptions<string> =>
typeof argument === 'object' &&
(argument as DicePoolOptions<number> | DicePoolOptions<string>).sides !==
undefined

export const isDiceNotation = (
argument: unknown
): argument is DiceNotation<number> | DiceNotation<string> =>
!!coreNotationPattern.test(String(argument))

function parseDiceOptions(
options: CoreRollArgument | undefined
): DicePoolOptions<string | number> {
if (isDicePoolOptions(options)) {
return options
}

if (isDiceNotation(options)) {
return parseNotation(options)
}

return {
quantity: 1,
sides: Array.isArray(options) ? options.map(String) : Number(options || 20)
}
}

export function parseRollArgument(
argument: CoreRollArgument | undefined
): RollParameters['dicePools'] {
const id = crypto.randomUUID()
const options = parseDiceOptions(argument)

return {
[id]: {
options,
argument,
die: dieFactory(options.sides),
notation: formatNotation(options),
description: formatDescription(options)
} as DicePoolParameters<number> | DicePoolParameters<string>
}
}

const isCustomSides = (
argument: RollArgument | undefined
): argument is string[] =>
Array.isArray(argument) && argument.every((arg) => typeof arg === 'string')
import { CoreRollArgument, RollArgument, RollParameters } from '~types'
import { isCustomSides } from './guards.ts'
import parameterizeRollArgument from './parameterize-roll-argument.ts'

const normalizeArguments = (
argument: RollArgument | undefined
Expand All @@ -83,10 +22,10 @@ const normalizeArguments = (

function parseRollArguments(argument: RollArgument): RollParameters {
return {
dicePools: normalizeArguments(argument).reduce(
(acc, arg) => ({ ...acc, ...parseRollArgument(arg) }),
{}
)
dicePools: normalizeArguments(argument).reduce((acc, arg) => {
const id = crypto.randomUUID()
return { ...acc, [id]: parameterizeRollArgument(arg) }
}, {})
}
}

Expand Down
48 changes: 48 additions & 0 deletions src/roll/parse-roll-arguments/parameterize-roll-argument.ts
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

0 comments on commit a6d3bbd

Please sign in to comment.