action-semantic-pull-request icon indicating copy to clipboard operation
action-semantic-pull-request copied to clipboard

Support optional regexp flags

Open jbergstroem opened this issue 1 year ago • 3 comments

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:

  1. Allow passing regex literal instead of a string to RegExp()
  2. Allow users to set the RegExp() options (currently hardcoded to g)

Which one is preferred? I can take a stab at a PR.

jbergstroem avatar Feb 13 '24 12:02 jbergstroem

Allow passing regex literal instead of a string to RegExp()

Can you elaborate how that would work?

amannn avatar Feb 15 '24 07:02 amannn

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?

jbergstroem avatar Feb 15 '24 21:02 jbergstroem

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.

amannn avatar Feb 16 '24 20:02 amannn