-
Notifications
You must be signed in to change notification settings - Fork 2
/
quotation.js
27 lines (24 loc) · 849 Bytes
/
quotation.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
const SINGLE_QM = "'";
const DOUBLE_QM = '"';
const RIGHT_SINGLE_QM = "’";
const RIGHT_DOUBLE_QM = "”";
const SINGLE_QM_REGEX = new RegExp(SINGLE_QM, "g");
const DOUBLE_QM_REGEX = new RegExp(DOUBLE_QM, "g");
const RIGHT_SINGLE_QM_REGEX = new RegExp(RIGHT_SINGLE_QM, "g");
const RIGHT_DOUBLE_QM_REGEX = new RegExp(RIGHT_DOUBLE_QM, "g");
const escapeQuotationMarks = (/** @type {string} */ text) => {
if (text == null) return "";
return text
.replace(SINGLE_QM_REGEX, RIGHT_SINGLE_QM)
.replace(DOUBLE_QM_REGEX, RIGHT_DOUBLE_QM);
};
const unescapeQuotationMarks = (/** @type {string} */ text) => {
if (text == null) return null;
return text
.replace(RIGHT_SINGLE_QM_REGEX, SINGLE_QM)
.replace(RIGHT_DOUBLE_QM_REGEX, SINGLE_QM);
};
module.exports = {
escape: escapeQuotationMarks,
unescape: unescapeQuotationMarks,
};