Skip to content

Commit da6b182

Browse files
committed
feat: add resultModifier flow on Parser blueprint
1 parent 463755e commit da6b182

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/blueprints/Parser.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export class Parser<MatcherKeys extends readonly string[] = string[]> {
77
#matcher: RegExp;
88
#matcherKeys: MatcherKeys;
99
#inputModifier?: (input: string) => string;
10+
#resultModifier?: (
11+
result: ParseResult<MatcherKeys>
12+
) => ParseResult<MatcherKeys>;
1013

1114
/**
1215
* Creates a new Parser instance.
@@ -19,14 +22,18 @@ export class Parser<MatcherKeys extends readonly string[] = string[]> {
1922
constructor(
2023
matcher: RegExp,
2124
matcherKeys: MatcherKeys,
22-
inputModifier?: (input: string) => string
25+
inputModifier?: (input: string) => string,
26+
resultModifier?: (
27+
result: ParseResult<MatcherKeys>
28+
) => ParseResult<MatcherKeys>
2329
) {
2430
if (!matcher.global) {
2531
throw new TypeError("argument 'matcher' must be a global regex.");
2632
}
2733
this.#matcher = matcher;
2834
this.#matcherKeys = matcherKeys;
2935
this.#inputModifier = inputModifier;
36+
this.#resultModifier = resultModifier;
3037
}
3138

3239
/**
@@ -62,7 +69,13 @@ export class Parser<MatcherKeys extends readonly string[] = string[]> {
6269
);
6370

6471
for (const matchError of matches) {
65-
results.push(this.#constructResult(matchError));
72+
let result = this.#constructResult(matchError);
73+
74+
if (this.#resultModifier) {
75+
result = this.#resultModifier({ ...result });
76+
}
77+
78+
results.push(result);
6679
}
6780

6881
return results;

0 commit comments

Comments
 (0)