-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `escapeFull()` replaces special characters like newline with two-character literals of their escape sequences like `"\n"` - `escapeTerse()` is converts special characters into single-character unicode symbols, to preserve string length. Importantly, newline becomes `"¶"` and tab becomes `"⇥"`
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { assertEquals, describe, it } from "../_deps/testing.ts"; | ||
import { escapeFull, escapeTerse } from "./escape.ts"; | ||
|
||
describe("escapeFull", () => { | ||
it("escapes newlines", () => { | ||
assertEquals(escapeFull("\n\n"), "\\n\\n"); | ||
assertEquals(escapeFull("\r\r"), "\\r\\r"); | ||
assertEquals(escapeFull("\r\n\r\n"), "\\r\\n\\r\\n"); | ||
}); | ||
|
||
it("escapes tabs", () => assertEquals(escapeFull("\t\t"), "\\t\\t")); | ||
|
||
it("escapes vertical tabs", () => assertEquals(escapeFull("\v\v"), "\\v\\v")); | ||
|
||
it("escapes form feeds", () => assertEquals(escapeFull("\f\f"), "\\f\\f")); | ||
|
||
it("escapes backspaces", () => assertEquals(escapeFull("\b\b"), "\\b\\b")); | ||
}); | ||
|
||
describe("escapeTerse", () => { | ||
it("escapes newlines", () => { | ||
assertEquals(escapeTerse("\n\n"), "¶¶"); | ||
assertEquals(escapeTerse("\r\r"), "␍␍"); | ||
assertEquals(escapeTerse("\r\n\r\n"), "␍¶␍¶"); | ||
}); | ||
|
||
it("escapes tabs", () => assertEquals(escapeTerse("\t\t"), "⇥⇥")); | ||
|
||
it("escapes vertical tabs", () => assertEquals(escapeTerse("\v\v"), "␋␋")); | ||
|
||
it("escapes form feeds", () => assertEquals(escapeTerse("\f\f"), "␌␌")); | ||
|
||
it("escapes backspaces", () => assertEquals(escapeTerse("\b\b"), "␈␈")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** Replaces special characters with their escape sequences. | ||
* | ||
* Note: `escape` is a global function in JavaScript, so we use `escapeFull`. */ | ||
export function escapeFull(str: string): string { | ||
return str | ||
.replaceAll("\n", "\\n") | ||
.replaceAll("\r", "\\r") | ||
.replaceAll("\t", "\\t") | ||
.replaceAll("\f", "\\f") | ||
.replaceAll("\v", "\\v") | ||
.replaceAll("\b", "\\b"); | ||
} | ||
|
||
/** Replaces special characters with single Unicode symbols that represent | ||
* them, ensuring that the output length is the same as the input length. | ||
* | ||
* - `\n` becomes `¶` | ||
* - `\r` becomes `␍` | ||
* - `\t` becomes `⇥` | ||
* - `\f` becomes `␌` | ||
* - `\v` becomes `␋` | ||
* - `\b` becomes `␈` */ | ||
export function escapeTerse(str: string): string { | ||
return str | ||
.replaceAll("\n", "¶") | ||
.replaceAll("\r", "␍") | ||
.replaceAll("\t", "⇥") | ||
.replaceAll("\f", "␌") | ||
.replaceAll("\v", "␋") | ||
.replaceAll("\b", "␈"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters