Skip to content

Commit

Permalink
Regex -> regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-rowe committed May 2, 2023
1 parent beb794b commit c91fb67
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
11 changes: 0 additions & 11 deletions regex/mod.ts

This file was deleted.

11 changes: 11 additions & 0 deletions regexp/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
* Functions for tasks related to regular expression (regexps), such as
* escaping text for interpolation into a regexp
*
* @module
*/

export * from "./regexp_escape.ts";
14 changes: 7 additions & 7 deletions regex/regex_escape.ts → regexp/regexp_escape.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

// // For future forward-compatibility with regex `v` flag, reservedCharMap is
// // For future forward-compatibility with regexp `v` flag, reservedCharMap is
// // autogenerated from the ClassSetReservedDoublePunctuator,
// // ClassSetSyntaxCharacter, and ClassSetReservedPunctuator categories in the
// // draft spec.
Expand Down Expand Up @@ -54,30 +54,30 @@ const reservedCharMap = {
"|": "\\|",
};

const RX_REGEX_ESCAPE = new RegExp(
const RX_REGEXP_ESCAPE = new RegExp(
`[${Object.values(reservedCharMap).join("")}]`,
"gu",
);

/**
* Escapes arbitrary text for interpolation into a regex, such that it will
* Escapes arbitrary text for interpolation into a regexp, such that it will
* match exactly that text and nothing else.
*
* @example
* ```ts
* import { regexEscape } from "https://deno.land/std@$STD_VERSION/regex/regex_escape.ts";
* import { regExpEscape } from "https://deno.land/std@$STD_VERSION/regexp/mod.ts";
* import { assertEquals, assertMatch, assertNotMatch } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
*
* const re = new RegExp(`^${regexEscape(".")}$`, "u");
* const re = new RegExp(`^${regExpEscape(".")}$`, "u");
*
* assertEquals("^\\.$", re.source);
* assertMatch(".", re);
* assertNotMatch("a", re);
* ```
*/
export function regexEscape(str: string) {
export function regExpEscape(str: string) {
return str.replaceAll(
RX_REGEX_ESCAPE,
RX_REGEXP_ESCAPE,
(m) => reservedCharMap[m as keyof typeof reservedCharMap],
);
}
33 changes: 18 additions & 15 deletions regex/regex_escape_test.ts → regexp/regexp_escape_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { regexEscape } from "./regex_escape.ts";
import { regExpEscape } from "./regexp_escape.ts";
import {
assertEquals,
assertMatch,
Expand All @@ -9,32 +9,32 @@ import {

const ALL_ASCII =
"\x00\x01\x02\x03\x04\x05\x06\x07\b\t\n\v\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7F";
const ALL_REGEX_FLAGS = "gimsuy";
const ALL_REGEXP_FLAGS = "gimsuy";

Deno.test("regexEscape", async (t) => {
Deno.test("regExpEscape", async (t) => {
await t.step("examples", async (t) => {
await t.step("`.` matches literal `.`", () => {
const re = new RegExp(`^${regexEscape(".")}$`, "u");
const re = new RegExp(`^${regExpEscape(".")}$`, "u");

assertEquals("^\\.$", re.source);
assertMatch(".", re);
assertNotMatch("a", re);
});
await t.step("`$` matches literal `$`", () => {
const re = new RegExp(`^${regexEscape("$")}$`);
const re = new RegExp(`^${regExpEscape("$")}$`);

assertMatch("$", re);
assertNotMatch("", re);
});
await t.step("`*` matches literal `*`", () => {
const re = new RegExp(`^${regexEscape("a*")}$`);
const re = new RegExp(`^${regExpEscape("a*")}$`);

assertMatch("a*", re);
assertNotMatch("", re);
assertNotMatch("aaa", re);
});
await t.step("escapes work correctly within character class", () => {
const re = new RegExp(`^[${regexEscape(".$*+[](){}|\\<>")}]$`);
const re = new RegExp(`^[${regExpEscape(".$*+[](){}|\\<>")}]$`);

assertMatch(".", re);
assertMatch("$", re);
Expand All @@ -58,34 +58,37 @@ Deno.test("regexEscape", async (t) => {
await t.step("interpolates without erroring", async (t) => {
await t.step("outside character class", () => {
for (const char of ALL_ASCII) {
for (const flag of ALL_REGEX_FLAGS) {
new RegExp(regexEscape(char), flag);
for (const flag of ALL_REGEXP_FLAGS) {
new RegExp(regExpEscape(char), flag);
}
}
});
await t.step("within character class", () => {
for (const char of ALL_ASCII) {
for (const flag of ALL_REGEX_FLAGS) {
new RegExp(`[${regexEscape(char)}]`, flag);
for (const flag of ALL_REGEXP_FLAGS) {
new RegExp(`[${regExpEscape(char)}]`, flag);
}
}
});
await t.step("matches self", () => {
for (const char of ALL_ASCII) {
for (const flag of ALL_REGEX_FLAGS) {
assertMatch(char, new RegExp(`^${regexEscape(char)}$`, flag));
for (const flag of ALL_REGEXP_FLAGS) {
assertMatch(char, new RegExp(`^${regExpEscape(char)}$`, flag));
}
}
});
await t.step("doesn't match any other chars", () => {
for (const char of ALL_ASCII) {
for (const flag of ALL_REGEX_FLAGS) {
for (const flag of ALL_REGEXP_FLAGS) {
if (flag === "i") continue;

for (const char2 of ALL_ASCII) {
if (char2 === char) continue;

assertNotMatch(char2, new RegExp(`^${regexEscape(char)}$`, flag));
assertNotMatch(
char2,
new RegExp(`^${regExpEscape(char)}$`, flag),
);
}
}
}
Expand Down

0 comments on commit c91fb67

Please sign in to comment.