From a2f8eedd290701f1d83f82a0d43b0e01c6004e3d Mon Sep 17 00:00:00 2001 From: Alex Jarvis Date: Mon, 2 Sep 2024 19:26:32 -0400 Subject: [PATCH] No more "MatchAll" (#613) * Move matchPatterns * No More Matchall * 4.5.7 * Format --- package.json | 2 +- src/{constants.ts => matchPattern.ts} | 0 src/roll/parseRollArguments/guards.ts | 15 ++++++++++----- src/roll/parseRollArguments/parseNotation.ts | 15 ++++++++++----- tsconfig.json | 2 +- 5 files changed, 22 insertions(+), 12 deletions(-) rename src/{constants.ts => matchPattern.ts} (100%) diff --git a/package.json b/package.json index 58ee8514..a7f8f560 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/constants.ts b/src/matchPattern.ts similarity index 100% rename from src/constants.ts rename to src/matchPattern.ts diff --git a/src/roll/parseRollArguments/guards.ts b/src/roll/parseRollArguments/guards.ts index d51358a4..d1419385 100644 --- a/src/roll/parseRollArguments/guards.ts +++ b/src/roll/parseRollArguments/guards.ts @@ -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 => { @@ -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) diff --git a/src/roll/parseRollArguments/parseNotation.ts b/src/roll/parseRollArguments/parseNotation.ts index 11484cf7..57fdf1f1 100644 --- a/src/roll/parseRollArguments/parseNotation.ts +++ b/src/roll/parseRollArguments/parseNotation.ts @@ -1,4 +1,4 @@ -import { completeRollPattern } from '~constants' +import { completeRollPattern } from '~matchPattern' import { DiceNotation, DicePoolOptions } from '~types' import { isCoreNotationMatch, @@ -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 diff --git a/tsconfig.json b/tsconfig.json index b3001123..428ab004 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"],