Skip to content

Commit

Permalink
regExpEscape -> escape
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-rowe committed May 4, 2023
1 parent c91fb67 commit c8673a4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 102 deletions.
6 changes: 3 additions & 3 deletions regexp/regexp_escape.ts → regexp/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ const RX_REGEXP_ESCAPE = new RegExp(
*
* @example
* ```ts
* import { regExpEscape } from "https://deno.land/std@$STD_VERSION/regexp/mod.ts";
* import { escape } 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(`^${regExpEscape(".")}$`, "u");
* const re = new RegExp(`^${escape(".")}$`, "u");
*
* assertEquals("^\\.$", re.source);
* assertMatch(".", re);
* assertNotMatch("a", re);
* ```
*/
export function regExpEscape(str: string) {
export function escape(str: string) {
return str.replaceAll(
RX_REGEXP_ESCAPE,
(m) => reservedCharMap[m as keyof typeof reservedCharMap],
Expand Down
100 changes: 100 additions & 0 deletions regexp/escape_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { escape } from "./escape.ts";
import {
assertEquals,
assertMatch,
assertNotMatch,
} from "../testing/asserts.ts";

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_REGEXP_FLAGS = "gimsuy";

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

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

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

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

assertMatch(".", re);
assertMatch("$", re);
assertMatch("*", re);
assertMatch("+", re);
assertMatch("[", re);
assertMatch("]", re);
assertMatch("(", re);
assertMatch(")", re);
assertMatch("{", re);
assertMatch("}", re);
assertMatch("|", re);
assertMatch("\\", re);
assertMatch("<", re);
assertMatch(">", re);

assertNotMatch("a", re);
});
});
await t.step("all ASCII", 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_REGEXP_FLAGS) {
new RegExp(escape(char), flag);
}
}
});
await t.step("within character class", () => {
for (const char of ALL_ASCII) {
for (const flag of ALL_REGEXP_FLAGS) {
new RegExp(`[${escape(char)}]`, flag);
}
}
});
await t.step("matches self", () => {
for (const char of ALL_ASCII) {
for (const flag of ALL_REGEXP_FLAGS) {
assertMatch(char, new RegExp(`^${escape(char)}$`, flag));
}
}
});
await t.step("doesn't match any other chars", () => {
for (const char of ALL_ASCII) {
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(`^${escape(char)}$`, flag),
);
}
}
}
});
});
});
});
});
2 changes: 1 addition & 1 deletion regexp/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
* @module
*/

export * from "./regexp_escape.ts";
export * from "./escape.ts";
98 changes: 0 additions & 98 deletions regexp/regexp_escape_test.ts

This file was deleted.

0 comments on commit c8673a4

Please sign in to comment.