Skip to content

Commit

Permalink
fix: handle escapes better in .quote()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Mar 1, 2024
1 parent 0e9d8ce commit f292792
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
24 changes: 9 additions & 15 deletions utils/string_utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "../_test.deps.ts";
import { escapeChar, escapeForWithinString, getStringFromStrOrFunc } from "./string_utils.ts";
import { escapeForWithinString, getStringFromStrOrFunc } from "./string_utils.ts";

describe("escapeForWithinString", () => {
function doTest(input: string, expected: string) {
Expand All @@ -9,27 +9,21 @@ describe("escapeForWithinString", () => {
it("should escape the quotes and newline", () => {
doTest(`"testing\n this out"`, `\\"testing\\\n this out\\"`);
});
});

describe("escapeChar", () => {
function doTest(input: string, char: string, expected: string) {
expect(escapeChar(input, char)).to.equal(expected);
function doQuoteTest(input: string, quote: string, expected: string) {
expect(escapeForWithinString(input, quote)).to.equal(expected);
}

it("should throw when specifying a char length > 1", () => {
expect(() => escapeChar("", "ab")).to.throw();
});

it("should throw when specifying a char length < 1", () => {
expect(() => escapeChar("", "")).to.throw();
});

it("should escape the single quotes when specified", () => {
doTest(`'testing "this" out'`, `'`, `\\'testing "this" out\\'`);
doQuoteTest(`'testing "this" out'`, `'`, `\\'testing "this" out\\'`);
});

it("should escape regardless of if the character is already escaped", () => {
doTest(`"testing \\"this\\" out"`, `"`, `\\"testing \\\\"this\\\\" out\\"`);
doQuoteTest(`"testing \\"this\\" out"`, `"`, `\\"testing \\\\\\"this\\\\\\" out\\"`);
});

it("should escape unicode escape sequences", () => {
doQuoteTest(`\\u0009`, `"`, `\\\\u0009`);
});
});

Expand Down
21 changes: 9 additions & 12 deletions utils/string_utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const newlineRegex = /(\r?\n)/g;

/** @internal */
export function escapeForWithinString(str: string, quoteKind: string) {
return escapeChar(str, quoteKind).replace(newlineRegex, "\\$1");
}

/** @internal */
export function escapeChar(str: string, char: string) {
if (char.length !== 1) {
throw new Error(`Specified char must be one character long.`);
}

let result = "";
// todo: reduce appends (don't go char by char)
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
if (str[i] === quoteKind) {
result += "\\";
} else if (str[i] === "\r" && str[i + 1] === "\n") {
result += "\\";
i++; // skip the \r
} else if (str[i] === "\n") {
result += "\\";
} else if (str[i] === "\\") {
result += "\\";
}
result += str[i];
Expand Down

0 comments on commit f292792

Please sign in to comment.