Skip to content

Commit

Permalink
Remove "Named Groups". (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxjrvs authored Sep 5, 2024
1 parent a2f8eed commit be10d64
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 404 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.5.7",
"version": "4.5.8",
"private": false,
"author": "Alex Jarvis",
"icon": "https://raw.githubusercontent.com/RANDSUM/randsum-ts/main/icon.webp",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './Die'
export { default as roll } from './roll'
export { default as validateDiceNotation } from './validateDiceNotation'
export { default as optionsToNotation } from './optionsToNotation'
export { default as parseRollArguments } from './roll/parseRollArguments'
export { default as parameterizeRollArgument } from './roll/parseRollArguments/parameterizeRollArguments'
export { default as generateRollResult } from './roll/generateRollResult'
Expand Down
25 changes: 12 additions & 13 deletions src/matchPattern.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
export const coreNotationPattern = /(?<coreNotationMatch>^\d+[Dd](\d+|{.*}))/
export const coreNotationPattern = /^\d+[Dd](\d+|{.*})/

const dropHighestMatch = /(?<dropHighMatch>[Hh]\d*)/
const dropLowestMatch = /(?<dropLowMatch>[Ll]\d*)/
const dropConstraintsMatch =
/(?<dropConstraintsMatch>[Dd]{([<>]?\d+,)*([<>]?\d+)})/
export const dropHighestPattern = /[Hh]\d*/g
export const dropLowestPattern = /[Ll]\d*/g
export const dropConstraintsPattern = /[Dd]{([<>]?\d+,)*([<>]?\d+)}/g

const explodeMatch = /(?<explodeMatch>!)/
const uniqueMatch = /(?<uniqueMatch>[Uu]({(\d+,)*(\d+)})?)/
export const explodePattern = /!/g
export const uniquePattern = /[Uu]({(\d+,)*(\d+)})?/g

const replaceMatch = /(?<replaceMatch>[Vv]{([<>]?\d+=?\d+,)*([<>]?\d+=?\d+)})/
export const replacePattern = /[Vv]{([<>]?\d+=?\d+,)*([<>]?\d+=?\d+)}/g

const rerollMatch = /(?<rerollMatch>[Rr]{([<>]?\d,)*([<>]?\d)}\d*)/
export const rerollPattern = /[Rr]{([<>]?\d,)*([<>]?\d)}\d*/g

const capMatch = /(?<capMatch>[Cc]{([<>]?\d+,)*([<>]?\d+)})/
const plusMatch = /(?<plusMatch>\+\d+)/
const minusMatch = /(?<minusMatch>-\d+)/
export const capPattern = /[Cc]{([<>]?\d+,)*([<>]?\d+)}/g
export const plusPattern = /\+\d+/g
export const minusPattern = /-\d+/g

export const modifierRollPatterns = new RegExp(
`${dropHighestMatch.source}|${dropLowestMatch.source}|${dropConstraintsMatch.source}|${explodeMatch.source}|${uniqueMatch.source}|${replaceMatch.source}|${rerollMatch.source}|${capMatch.source}|${plusMatch.source}|${minusMatch.source}`
`${dropHighestPattern.source}|${dropLowestPattern.source}|${dropConstraintsPattern.source}|${explodePattern.source}|${uniquePattern.source}|${replacePattern.source}|${rerollPattern.source}|${capPattern.source}|${plusPattern.source}|${minusPattern.source}`
)

export const completeRollPattern = new RegExp(
Expand Down
8 changes: 8 additions & 0 deletions src/optionsToNotation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DiceNotation, DicePoolOptions } from '~types'
import formatNotation from '../roll/parseRollArguments/formatNotation'

export default function optionsToNotation(
options: DicePoolOptions
): DiceNotation {
return formatNotation(options)
}
15 changes: 1 addition & 14 deletions src/roll/parseRollArguments/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@ export const isDiceNotation = (argument: unknown): argument is DiceNotation => {

const cleanArg = argument.replace(/\s/g, '')

const matches = []

let parsed: RegExpExecArray | null
while ((parsed = completeRollPattern.exec(cleanArg))) {
if (parsed && parsed.groups) {
matches.push(Object.values(parsed.groups))
}
}

const remaining = matches.flat().reduce((acc, curr) => {
return acc.replace(curr, '')
}, cleanArg)

return remaining.length === 0
return cleanArg.replace(completeRollPattern, '').length === 0
}

export const isCustomSides = (
Expand Down
Loading

0 comments on commit be10d64

Please sign in to comment.