1 | var toString = require('../lang/toString');
|
2 |
|
3 | /**
|
4 | * Escape string into unicode sequences
|
5 | */
|
6 | function escapeUnicode(str, shouldEscapePrintable){
|
7 | str = toString(str);
|
8 | return str.replace(/[\s\S]/g, function(ch){
|
9 | // skip printable ASCII chars if we should not escape them
|
10 | if (!shouldEscapePrintable && (/[\x20-\x7E]/).test(ch)) {
|
11 | return ch;
|
12 | }
|
13 | // we use "000" and slice(-4) for brevity, need to pad zeros,
|
14 | // unicode escape always have 4 chars after "\u"
|
15 | return '\\u'+ ('000'+ ch.charCodeAt(0).toString(16)).slice(-4);
|
16 | });
|
17 | }
|
18 |
|
19 | module.exports = escapeUnicode;
|
20 |
|
21 |
|