define(function() { return function str_escape(s) {
if (s.toSource) {
A simple string escape function.
define(function() { return function str_escape(s) {
if (s.toSource) {
If available, abuse toSource()
to properly quote a string
value.
return s.toSource().slice(12,-2);
}
Erg, use hand-coded version.
var quotes = '"';
if (s.indexOf('"') !== -1 && s.indexOf("'") === -1) {
quotes = "'";
}
var table = {};
table["\n"] = "n";
table["\r"] = "r";
table["\f"] = "f";
table["\b"] = "b";
table["\t"] = "t";
table["\\"] = "\\";
table[quotes] = quotes;
var result = "", i=0;
while (i < s.length) {
var c = s.charAt(i);
if (table.hasOwnProperty(c)) {
result += "\\" + table[c];
} else if (c < ' ' || c > '~') {
XXX allow some accented UTF-8 characters (printable ones)?
var cc = c.charCodeAt(0).toString(16);
while (cc.length < 4) {
cc = "0" + cc;
}
result += "\\u" + cc;
} else {
result += c;
}
i += 1;
}
return quotes + result + quotes;
};
});