-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: main
Are you sure you want to change the base?
feat: permutateRegex #156
Conversation
Yes this would be so helpful for saving me on some of the regex I've written. |
There was a problem hiding this 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!
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; |
There was a problem hiding this comment.
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("(?:)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(regex.source).not.toEqual("(?:)"); |
Do we need this? The next test checks what it doesn't match, after all.
|
||
console.log(regex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log(regex); |
function _permutations(permutation: (string | RegExp)[]) { | ||
const permutations: (string | RegExp)[][] = []; | ||
|
||
function permute(array: (string | RegExp)[], length: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Checklist:
Update index.md
)a
,b
andc
, which checks code written asa || b || c
, it should match botha || b || c
andb || a || c
, and any other permutation.elementsSeparator
is a bit arbitrarily picked...Capturing groups in individual regex
("|')thing\1
,(?<m>"|")thing\k<m>
. It'd be good to support it.