-
Notifications
You must be signed in to change notification settings - Fork 2
/
tag.js
28 lines (24 loc) · 777 Bytes
/
tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const COLON = ":";
const DASH = "-";
const ESCAPE_TAG_COLON = "__colon__";
const ESCAPE_TAG_DASH = "__dash__";
const COLON_REGEX = new RegExp(COLON, "g");
const DASH_REGEX = new RegExp(DASH, "g");
const ESCAPE_TAG_COLON_REGEX = new RegExp(ESCAPE_TAG_COLON, "g");
const ESCAPE_TAG_DASH_REGEX = new RegExp(ESCAPE_TAG_DASH, "g");
const escapeTag = (/** @type {string} */ text) => {
if (text == null) return "";
return text
.replace(COLON_REGEX, ESCAPE_TAG_COLON)
.replace(DASH_REGEX, ESCAPE_TAG_DASH);
};
const unescapeTag = (/** @type {string} */ text) => {
if (text == null) return null;
return text
.replace(ESCAPE_TAG_COLON_REGEX, COLON)
.replace(ESCAPE_TAG_DASH_REGEX, DASH);
};
module.exports = {
escape: escapeTag,
unescape: unescapeTag,
};