Skip to content

Commit f67b826

Browse files
committed
Add capture groups
1 parent c7e5459 commit f67b826

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/group.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import group from "./group";
2+
import VerEx from "./verbalexpressions";
3+
import { anything } from "./constants";
4+
import { maybe } from "./maybe";
5+
6+
describe("group", () => {
7+
it("should export a function", () => {
8+
expect(group).toBeInstanceOf(Function);
9+
});
10+
11+
it("should add a capture gorup that to the expression", () => {
12+
const exp = VerEx("foo", group("bar"), "baz");
13+
const [, result] = exp.exec("foobarbaz");
14+
expect(result).toEqual("bar");
15+
});
16+
17+
it("should work with multiple arguments", () => {
18+
const exp = VerEx(group("http", maybe("s")), "://", group(anything));
19+
const [, protocol, domain] = exp.exec("https://google.com");
20+
expect(protocol).toEqual("https");
21+
expect(domain).toEqual("google.com");
22+
});
23+
});

src/group.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Expression } from "./types";
2+
import { compile } from "./verbalexpressions";
3+
4+
export default function group(...inputs: Expression[]) {
5+
return new RegExp(`(${compile(...inputs)})`);
6+
}

0 commit comments

Comments
 (0)