Skip to content

Commit

Permalink
Refactor replaceAllUnescapedWithEscaped function to include optional …
Browse files Browse the repository at this point in the history
…wrapping of escaped characters
  • Loading branch information
Mearman committed Mar 10, 2024
1 parent 875ddf8 commit b54cade
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/util/escapeCharacters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ export function replaceAllEscapedWithUnescaped(input: string): string {
);
}

export function replaceAllUnescapedWithEscaped(input: string): string {
export function replaceAllUnescapedWithEscaped(
input: string,
wrap: boolean = false
): string {
let escapedResult = "";
for (let i = 0; i < input.length; i++) {
const matched: string = EscapedCharacterValues.reduce((prev, escaped) => {
Expand All @@ -101,8 +104,12 @@ export function replaceAllUnescapedWithEscaped(input: string): string {
if (!matched) {
const unescaped = input[i];
if (unescaped in EscapeCharacters) {
const escaped = toEscaped(unescaped as UnescapedCharacter);
escapedResult += escaped;
const escaped = toEscaped(unescaped as UnescapedCharacter)
if (wrap) {
escapedResult += `${escaped}`;
} else {
escapedResult += escaped;
}
} else {
escapedResult += unescaped;
}
Expand Down

0 comments on commit b54cade

Please sign in to comment.