action-semantic-pull-request
action-semantic-pull-request copied to clipboard
Support optional regexp flags
In an attempt to catch additional characters while checking for case in the subject, I ran into a limitation where I need to pass the unicode flag to RegExp():
> const strings = ["information", "Information", "Ínformation", "ínformation"];
undefined
> const old = new RegExp("^(?![A-Z]).+$", "g");
undefined
> const new = new RegExp("^(?!\\p{Lu}).+$", "gu");
undefined
> strings.forEach((s) => { console.log(`${s}: ${old.test(s)}`) });
information: true
Information: false
Ínformation: true
ínformation: false
undefined
> strings.forEach((s) => { console.log(`${s}: ${new.test(s)}`) });
information: true
Information: false
Ínformation: false
ínformation: true
undefined
I see two paths here:
- Allow passing regex literal instead of a string to
RegExp() - Allow users to set the
RegExp()options (currently hardcoded tog)
Which one is preferred? I can take a stab at a PR.
Allow passing regex literal instead of a string to
RegExp()
Can you elaborate how that would work?
Allow passing regex literal instead of a string to
RegExp()Can you elaborate how that would work?
Removing the option flag:
const re = new RegExp(/^(?!\p{Lu}).+$/gu);
Edit: ..or do you mean how we would support both types in the code path?
So this is where we read the subjectPattern from the user:
https://github.com/amannn/action-semantic-pull-request/blob/67cbd7a15a6eeea0c3a0dffff4768fa5653de05c/src/parseConfig.js#L26
… and here we construct the regex based on it:
https://github.com/amannn/action-semantic-pull-request/blob/67cbd7a15a6eeea0c3a0dffff4768fa5653de05c/src/validatePrTitle.js#L123
I understand that we could accept another option for providing the flags:
new RegExp(subjectPattern, subjectPatternFlags)
I'd just be curious if there's another option since you've mentioned that you can think of two.