Skip to content

Commit

Permalink
Add capture groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jehna committed Jul 20, 2019
1 parent c7e5459 commit f67b826
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/group.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import group from "./group";
import VerEx from "./verbalexpressions";
import { anything } from "./constants";
import { maybe } from "./maybe";

describe("group", () => {
it("should export a function", () => {
expect(group).toBeInstanceOf(Function);
});

it("should add a capture gorup that to the expression", () => {
const exp = VerEx("foo", group("bar"), "baz");
const [, result] = exp.exec("foobarbaz");
expect(result).toEqual("bar");
});

it("should work with multiple arguments", () => {
const exp = VerEx(group("http", maybe("s")), "://", group(anything));
const [, protocol, domain] = exp.exec("https://google.com");
expect(protocol).toEqual("https");
expect(domain).toEqual("google.com");
});
});
6 changes: 6 additions & 0 deletions src/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Expression } from "./types";
import { compile } from "./verbalexpressions";

export default function group(...inputs: Expression[]) {
return new RegExp(`(${compile(...inputs)})`);
}

0 comments on commit f67b826

Please sign in to comment.