Skip to content

Commit

Permalink
No more "MatchAll" (#613)
Browse files Browse the repository at this point in the history
* Move matchPatterns

* No More Matchall

* 4.5.7

* Format
  • Loading branch information
alxjrvs authored Sep 2, 2024
1 parent fbde519 commit a2f8eed
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 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.6",
"version": "4.5.7",
"private": false,
"author": "Alex Jarvis",
"icon": "https://raw.githubusercontent.com/RANDSUM/randsum-ts/main/icon.webp",
Expand Down
File renamed without changes.
15 changes: 10 additions & 5 deletions src/roll/parseRollArguments/guards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { completeRollPattern, coreNotationPattern } from '~constants'
import { completeRollPattern, coreNotationPattern } from '~matchPattern'
import { DiceNotation, DicePoolOptions, Modifiers, RollArgument } from '~types'

export const isDiceNotation = (argument: unknown): argument is DiceNotation => {
Expand All @@ -7,11 +7,16 @@ export const isDiceNotation = (argument: unknown): argument is DiceNotation => {

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

const matches = [...cleanArg.matchAll(completeRollPattern)].map(
(arr) => arr[0]
)
const matches = []

const remaining = matches.reduce((acc, curr) => {
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)

Expand Down
15 changes: 10 additions & 5 deletions src/roll/parseRollArguments/parseNotation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { completeRollPattern } from '~constants'
import { completeRollPattern } from '~matchPattern'
import { DiceNotation, DicePoolOptions } from '~types'
import {
isCoreNotationMatch,
Expand All @@ -8,10 +8,15 @@ import {
parseModifiers
} from './parseModifiers'

const findMatches = (notations: string): Match[] =>
[...notations.matchAll(completeRollPattern)].map(
({ groups: match }) => match as Match
)
const findMatches = (notations: string): Match[] => {
const matches = []
let parsed: RegExpExecArray | null
while ((parsed = completeRollPattern.exec(notations))) {
matches.push(parsed.groups as Match)
}

return matches
}

const parseNotation = (
notationString: DiceNotation
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"paths": {
"~roll": ["./src/roll/index.ts"],
"~Die": ["./src/Die/index.ts"],
"~constants": ["./src/constants.ts"],
"~matchPattern": ["./src/matchPattern.ts"],
"~types": ["./src/types.ts"]
},
"lib": ["ESNext"],
Expand Down

0 comments on commit a2f8eed

Please sign in to comment.