-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathstringescape.js
More file actions
39 lines (32 loc) · 944 Bytes
/
stringescape.js
File metadata and controls
39 lines (32 loc) · 944 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
var hex = '0123456789abcdef'.split('');
module.exports = function stringEncode(input, opts) {
opts = opts || {};
var escaped = "";
for (var i = 0; i < input.length; i++) {
escaped = escaped + encodeChar(input.charAt(i), opts.preserveNewlines);
}
return escaped;
}
function encodeChar(inputChar, preserveNewlines) {
var character = inputChar.charAt(0);
var characterCode = inputChar.charCodeAt(0);
switch(character) {
case '\n':
if (!preserveNewlines) return "\\n";
else return character;
case '\r':
if (!preserveNewlines) return "\\r";
else return character;
case '\'': return "\\'";
case '"': return "\\\"";
case '\&': return "\\&";
case '\\': return "\\\\";
case '\t': return "\\t";
case '\b': return "\\b";
case '\f': return "\\f";
case '/': return "\\x2F";
case '<': return "\\x3C";
case '>': return "\\x3E";
}
return inputChar;
}