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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/curriculum-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ const regex1 = /a\s/;
const regex2 = /b/;
concatRegex(regex1, regex2).source === "a\\sb";
```

## permutateRegex

Permutates regular expressions or source strings, to create regex matching elements in any order.

```javascript
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;
Comment on lines +18 to +22
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.

```
71 changes: 71 additions & 0 deletions lib/__tests__/curriculum-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("(?:)");
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.

});

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
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);


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);
});
});
52 changes: 52 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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.

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;
}
Expand Down
Loading