Skip to content

Commit

Permalink
refactor(marshal): Use String#replace rather than replaceAll
Browse files Browse the repository at this point in the history
Fixes failures on Node.js v14
  • Loading branch information
gibson042 committed Sep 7, 2023
1 parent 85aa08c commit 1150f34
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions packages/marshal/src/encodePassable.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,28 +242,26 @@ stringEscapes['^'.charCodeAt(0)] = '_@';
stringEscapes['_'.charCodeAt(0)] = '__';

const encodeStringWithEscapes = str =>
`s${str.replaceAll(/[\0-!^_]/g, ch => stringEscapes[ch.charCodeAt(0)])}`;
`s${str.replace(/[\0-!^_]/g, ch => stringEscapes[ch.charCodeAt(0)])}`;
const decodeStringWithEscapes = encoded => {
return encoded
.slice(1)
.replaceAll(/([!_])(.|\n)?/g, (esc, prefix, suffix) => {
switch (esc) {
case '!_':
return ' ';
case '!|':
return '!';
case '_@':
return '^';
case '__':
return '_';
default: {
const ch = /** @type {string} */ (suffix);
(prefix === '!' && ch >= '!' && ch <= '@') ||
Fail`invalid string escape: ${q(esc)}`;
return String.fromCharCode(ch.charCodeAt(0) - 0x21);
}
return encoded.slice(1).replace(/([!_])(.|\n)?/g, (esc, prefix, suffix) => {
switch (esc) {
case '!_':
return ' ';
case '!|':
return '!';
case '_@':
return '^';
case '__':
return '_';
default: {
const ch = /** @type {string} */ (suffix);
(prefix === '!' && ch >= '!' && ch <= '@') ||
Fail`invalid string escape: ${q(esc)}`;
return String.fromCharCode(ch.charCodeAt(0) - 0x21);
}
});
}
});
};

const encodeStringWithoutEscapes = str => `s${str}`;
Expand Down

0 comments on commit 1150f34

Please sign in to comment.