Avoid String.prototype.replaceAll

Not well supported in web browsers, except Firefox.
This commit is contained in:
Simon Ser 2020-06-28 15:40:57 +02:00
parent 73f14f38fa
commit deb8d4c01e
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48

View file

@ -20,7 +20,7 @@ export const ERR_SASLALREADY = "907";
export const STD_CHANNEL_TYPES = "#&+!";
var tagsEscape = {
const tagEscapeMap = {
";": "\\:",
" ": "\\s",
"\\": "\\\\",
@ -28,6 +28,16 @@ var tagsEscape = {
"\n": "\\n",
};
const tagUnescapeMap = Object.fromEntries(Object.entries(tagEscapeMap).map(([from, to]) => [to, from]));
function escapeTag(s) {
return s.replace(/[; \\\r\n]/, (ch) => tagEscapeMap[ch]);
}
function unescapeTag(s) {
return s.replace(/\\[:s\\rn]/, (seq) => tagUnescapeMap[seq]);
}
function parseTags(s) {
var tags = {};
s.split(";").forEach(function(s) {
@ -39,10 +49,7 @@ function parseTags(s) {
throw new Error("expected an equal sign in tag");
}
var k = parts[0];
var v = parts[1];
for (var ch in tagsEscape) {
v = v.replaceAll(tagsEscape[ch], ch);
}
var v = unescapeTag(parts[1]);
if (v.endsWith("\\")) {
v = v.slice(0, v.length - 1)
}
@ -54,10 +61,7 @@ function parseTags(s) {
function formatTags(tags) {
var l = [];
for (var k in tags) {
var v = tags[k];
for (var ch in tagsEscape) {
v = v.replaceAll(ch, tagsEscape[ch]);
}
var v = escapeTag(tags[k]);
l.push(k + "=" + v);
}
return l.join(";");