1 var toString = require('../lang/toString');
4 * Escape string into unicode sequences
6 function escapeUnicode(str, shouldEscapePrintable){
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)) {
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);
19 module.exports = escapeUnicode;