Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: permutateRegex #156

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

gikf
Copy link
Member

@gikf gikf commented Feb 26, 2024

Checklist:


Capturing groups in individual regex

  • It's rather easy to think of situation where it might be handy - ie. ("|')thing\1, (?<m>"|")thing\k<m>. It'd be good to support it.
  • If individual regex has capturing group, it will likely break. Named capturing group will surely break - group names have to be unique. Reference to non-named capturing group will reference to the same capturing group from every permutation, so that most likely will also not work as intended.
  • What comes to mind is replacing the group reference or name when joining permutations, to ensure their uniqueness and that reference is to the right thing. In such case expecting the capturing group to be named named would make it much easier, than finding/replacing unnamed groups and their numbered references.
  • Any ideas?

@naomi-lgbt
Copy link
Member

Yes this would be so helpful for saving me on some of the regex I've written.

Copy link
Contributor

@ojeytonwilliams ojeytonwilliams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @gikf, this looks really useful!

Comment on lines +18 to +22
const source1 = `titleInput\\.value\\s*(?:!==|!=)\\s*currentTask\\.title`;
const regex1 = /dateInput\.value\s*(?:!==|!=)\s*currentTask\.date/;
const source2 = `descriptionInput\\.value\\s*(?:!==|!=)\\s*currentTask\\.description`;

permutateRegex([source1, regex1, source2]).source === new RegExp(/(?:titleInput\.value\s*(?:!==|!=)\s*currentTask\.title\s*\|\|\s*dateInput\.value\s*(?:!==|!=)\s*currentTask\.date\s*\|\|\s*descriptionInput\.value\s*(?:!==|!=)\s*currentTask\.description|dateInput\.value\s*(?:!==|!=)\s*currentTask\.date\s*\|\|\s*titleInput\.value\s*(?:!==|!=)\s*currentTask\.title\s*\|\|\s*descriptionInput\.value\s*(?:!==|!=)\s*currentTask\.description|descriptionInput\.value\s*(?:!==|!=)\s*currentTask\.description\s*\|\|\s*titleInput\.value\s*(?:!==|!=)\s*currentTask\.title\s*\|\|\s*dateInput\.value\s*(?:!==|!=)\s*currentTask\.date|titleInput\.value\s*(?:!==|!=)\s*currentTask\.title\s*\|\|\s*descriptionInput\.value\s*(?:!==|!=)\s*currentTask\.description\s*\|\|\s*dateInput\.value\s*(?:!==|!=)\s*currentTask\.date|dateInput\.value\s*(?:!==|!=)\s*currentTask\.date\s*\|\|\s*descriptionInput\.value\s*(?:!==|!=)\s*currentTask\.description\s*\|\|\s*titleInput\.value\s*(?:!==|!=)\s*currentTask\.title|descriptionInput\.value\s*(?:!==|!=)\s*currentTask\.description\s*\|\|\s*dateInput\.value\s*(?:!==|!=)\s*currentTask\.date\s*\|\|\s*titleInput\.value\s*(?:!==|!=)\s*currentTask\.title)/).source;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about smaller sources? It's quite tricky to see what the functions actually doing with these. Unless I'm missing something permutateRegex(['a', \b\, 'c']) should capture everything we care about.

Also, could you document the separator? It wasn't obvious to me what that did until I looked into the source.

expect(regex.test("b||c||a")).toBe(true);
expect(regex.test("c||a||b")).toBe(true);
expect(regex.test("c||b||a")).toBe(true);
expect(regex.source).not.toEqual("(?:)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(regex.source).not.toEqual("(?:)");

Do we need this? The next test checks what it doesn't match, after all.

Comment on lines +188 to +189

console.log(regex);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(regex);

function _permutations(permutation: (string | RegExp)[]) {
const permutations: (string | RegExp)[][] = [];

function permute(array: (string | RegExp)[], length: number) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function permute(array: (string | RegExp)[], length: number) {
// Heap's algorithm
function permute(array: (string | RegExp)[], length: number) {

Just so people know what they're looking at.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants