-
-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -128,3 +128,74 @@ describe("concatRegex", () => { | |||||
expect(regEx.source).toBe(".*b\\s"); | ||||||
}); | ||||||
}); | ||||||
|
||||||
describe("permutateRegex", () => { | ||||||
it("returns a Regex", () => { | ||||||
const { permutateRegex } = helper; | ||||||
|
||||||
expect(permutateRegex([/a/, /b/])).toBeInstanceOf(RegExp); | ||||||
expect(permutateRegex([/a/, "b"])).toBeInstanceOf(RegExp); | ||||||
expect(permutateRegex(["a", "b"])).toBeInstanceOf(RegExp); | ||||||
}); | ||||||
|
||||||
it("returns regex matching all permutations", () => { | ||||||
const { permutateRegex } = helper; | ||||||
const regex = permutateRegex(["a", "b", /c/]); | ||||||
|
||||||
expect(regex.test("a||b||c")).toBe(true); | ||||||
expect(regex.test("a||c||b")).toBe(true); | ||||||
expect(regex.test("b||a||c")).toBe(true); | ||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Do we need this? The next test checks what it doesn't match, after all. |
||||||
}); | ||||||
|
||||||
it("returns regex not matching invalid permutation", () => { | ||||||
const { permutateRegex } = helper; | ||||||
const regex = permutateRegex(["a", "b", "c"]); | ||||||
|
||||||
expect(regex.test("")).toBe(false); | ||||||
expect(regex.test("a")).toBe(false); | ||||||
expect(regex.test("a||a")).toBe(false); | ||||||
expect(regex.test("a||a||a")).toBe(false); | ||||||
expect(regex.test("b")).toBe(false); | ||||||
expect(regex.test("b||b")).toBe(false); | ||||||
expect(regex.test("b||b||b")).toBe(false); | ||||||
expect(regex.test("c")).toBe(false); | ||||||
expect(regex.test("c||c")).toBe(false); | ||||||
expect(regex.test("c||c||c")).toBe(false); | ||||||
expect(regex.test("a||b")).toBe(false); | ||||||
expect(regex.test("a||b||a")).toBe(false); | ||||||
expect(regex.test("a||b||b")).toBe(false); | ||||||
}); | ||||||
|
||||||
it("returns regex using custom elementsSeparator", () => { | ||||||
const { permutateRegex } = helper; | ||||||
const regex = permutateRegex(["a", "b", "c"], { elementsSeparator: "," }); | ||||||
|
||||||
expect(regex.test("a,b,c")).toBe(true); | ||||||
expect(regex.test("a,c,b")).toBe(true); | ||||||
expect(regex.test("b,a,c")).toBe(true); | ||||||
expect(regex.test("b,c,a")).toBe(true); | ||||||
expect(regex.test("c,a,b")).toBe(true); | ||||||
expect(regex.test("c,b,a")).toBe(true); | ||||||
}); | ||||||
|
||||||
it("returns capturing regex when capture option is true", () => { | ||||||
const { permutateRegex } = helper; | ||||||
const regex = permutateRegex(["a", "b", "c"], { capture: true }); | ||||||
|
||||||
console.log(regex); | ||||||
Comment on lines
+188
to
+189
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
expect("b||c||a".match(regex)?.length).toEqual(2); | ||||||
expect("b||c||a".match(regex)?.[1]).toEqual("b||c||a"); | ||||||
}); | ||||||
|
||||||
it("returns not capturing regex when capture option is false", () => { | ||||||
const { permutateRegex } = helper; | ||||||
const regex = permutateRegex(["a", "b", "c"], { capture: false }); | ||||||
|
||||||
expect("b||c||a".match(regex)?.length).toEqual(1); | ||||||
}); | ||||||
}); |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -84,6 +84,58 @@ export function concatRegex(...regexes: (string | RegExp)[]) { | |||||||
return new RegExp(source); | ||||||||
} | ||||||||
|
||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Just so people know what they're looking at. |
||||||||
if (length === 1) { | ||||||||
permutations.push(array.slice()); | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
for (let i = 0; i < length; i++) { | ||||||||
permute(array, length - 1); | ||||||||
if (length % 2 === 1) { | ||||||||
[array[0], array[length - 1]] = [array[length - 1], array[0]]; | ||||||||
} else { | ||||||||
[array[i], array[length - 1]] = [array[length - 1], array[i]]; | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
permute(permutation, permutation.length); | ||||||||
return permutations; | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* Creates regex matching regular expressions or source strings in any order. | ||||||||
* @param {(string | RegExp)[]} regexes | ||||||||
* @param {Object} [options] | ||||||||
* @param {boolean} [options.capture=false] If `true`, returned regex will be capturing. Defaults to `false`. | ||||||||
* @param {string} [options.elementsSeparator=String.raw`\s*\|\|\s*`] Separator added between individual regexes within single permutation. Defaults to `\s*\|\|\s*`. | ||||||||
* @param {string} [options.permutationsSeparator='|'] Separator added between different permutations. Defaults to `|`. | ||||||||
* @returns {RegExp} | ||||||||
*/ | ||||||||
|
||||||||
export function permutateRegex( | ||||||||
regexes: (string | RegExp)[], | ||||||||
{ | ||||||||
capture = false, | ||||||||
elementsSeparator = String.raw`\s*\|\|\s*`, | ||||||||
permutationsSeparator = "|", | ||||||||
}: { | ||||||||
capture?: boolean; | ||||||||
elementsSeparator?: string; | ||||||||
permutationsSeparator?: string; | ||||||||
} = {} | ||||||||
): RegExp { | ||||||||
const permutations = _permutations(regexes.map((r) => new RegExp(r).source)); | ||||||||
const source = permutations | ||||||||
.map((p) => p.join(elementsSeparator)) | ||||||||
.join(permutationsSeparator); | ||||||||
return new RegExp(`(${capture ? "" : "?:"}${source})`); | ||||||||
} | ||||||||
|
||||||||
export interface ExtendedStyleRule extends CSSStyleRule { | ||||||||
isDeclaredAfter: (selector: string) => boolean; | ||||||||
} | ||||||||
|
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.