Skip to content

Commit e5873f6

Browse files
committed
Remove broken support for regular expressions
It may return at some point in the future. Ref. #10 and #11.
1 parent bd5bf18 commit e5873f6

File tree

4 files changed

+56
-82
lines changed

4 files changed

+56
-82
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,9 @@ jsesc('foo 𝌆 bar');
7878
// → 'foo \\uD834\\uDF06 bar'
7979
```
8080

81-
Instead of a string, the `value` can also be a regular expression, an array, or an object. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
81+
Instead of a string, the `value` can also be an array, or an object. In such cases, `jsesc` will return a stringified version of the value where any characters that are not printable ASCII symbols are escaped in the same way.
8282

8383
```js
84-
// Escaping a regular expression
85-
jsesc(/©𝌆/g);
86-
// → '/\\xA9\\uD834\\uDF06/g'
87-
8884
// Escaping an array
8985
jsesc([
9086
'Ich ♥ Bücher', 'foo 𝌆 bar'
@@ -186,8 +182,6 @@ jsesc([ 'Ich ♥ Bücher': 'foo 𝌆 bar' ], {
186182
// → '[\'\x49\x63\x68\x20\u2665\x20\x42\xFC\x63\x68\x65\x72\',\'\x66\x6F\x6F\x20\uD834\uDF06\x20\x62\x61\x72\']'
187183
```
188184

189-
This setting has no effect on the output for regular expressions. Those only use escape sequences for non-printable ASCII symbols and non-ASCII symbols, regardless of the value of the `escapeEverything` setting.
190-
191185
#### `compact`
192186

193187
The `compact` option takes a boolean value (`true` or `false`), and defaults to `true` (enabled). When enabled, the output for arrays and objects will be as compact as possible; it won’t be formatted nicely.
@@ -235,7 +229,7 @@ jsesc([ 'Ich ♥ Bücher', 'foo 𝌆 bar' ], {
235229
// → '[\n \'Ich \u2665 B\xFCcher\',\n\ t\'foo \uD834\uDF06 bar\'\n]'
236230
```
237231

238-
This setting has no effect on the output for strings or regular expressions.
232+
This setting has no effect on the output for strings.
239233

240234
#### `json`
241235

@@ -263,9 +257,14 @@ jsesc([ 'foo\x00bar', [1, '©', { 'foo': true, 'qux': null }], 42 ], {
263257
'json': true
264258
});
265259
// → '["foo\\u0000bar",[1,"\\u00A9",{"foo":true,"qux":null}],42]'
260+
// Values that aren’t allowed in JSON are run through `JSON.stringify()`:
261+
jsesc([ undefined, -Infinity ], {
262+
'json': true
263+
});
264+
// → '[null,null]'
266265
```
267266

268-
**Note:** Using this option on objects or arrays that contain non-string values relies on `JSON.parse()`. For legacy environments like IE ≤ 7, use [a `JSON` polyfill](http://bestiejs.github.io/json3/).
267+
**Note:** Using this option on objects or arrays that contain non-string values relies on `JSON.stringify()`. For legacy environments like IE ≤ 7, use [a `JSON` polyfill](http://bestiejs.github.io/json3/).
269268

270269
### `jsesc.version`
271270

jsesc.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*! http://mths.be/jsesc v0.4.1 by @mathias */
2-
;(function(root, evil) {
2+
;(function(root) {
33

44
// Detect free variables `exports`
55
var freeExports = typeof exports == 'object' && exports;
@@ -54,9 +54,6 @@
5454
// simple, but good enough for what we need
5555
return toString.call(value) == '[object Object]';
5656
};
57-
var isRegExp = function(value) {
58-
return toString.call(value) == '[object RegExp]';
59-
};
6057
var isString = function(value) {
6158
return typeof value == 'string' ||
6259
toString.call(value) == '[object String]';
@@ -83,6 +80,19 @@
8380
var regexDigit = /[0-9]/;
8481
var regexWhitelist = /[\x20\x21\x23-\x26\x28-\x5B\x5D-\x7E]/;
8582

83+
var stringFromCharCode = String.fromCharCode;
84+
// Modified version of `ucs2encode`; see http://mths.be/punycode
85+
var codePointToSymbol = function(codePoint, strict) {
86+
var output = '';
87+
if (codePoint > 0xFFFF) {
88+
codePoint -= 0x10000;
89+
output += stringFromCharCode(codePoint >>> 10 & 0x3FF | 0xD800);
90+
codePoint = 0xDC00 | codePoint & 0x3FF;
91+
}
92+
output += stringFromCharCode(codePoint);
93+
return output;
94+
};
95+
8696
var jsesc = function(argument, options) {
8797
// Handle options
8898
var defaults = {
@@ -91,7 +101,7 @@
91101
'wrap': false,
92102
'compact': true,
93103
'indent': '\t',
94-
'__indent': '',
104+
'__indent__': '',
95105
};
96106
options = extend(defaults, options);
97107
if (options.quotes != 'single' && options.quotes != 'double') {
@@ -114,9 +124,9 @@
114124
if (isArray(argument)) {
115125
result = [];
116126
options.wrap = true;
117-
oldIndent = options.__indent;
127+
oldIndent = options.__indent__;
118128
indent += oldIndent;
119-
options.__indent = indent;
129+
options.__indent__ = indent;
120130
forEach(argument, function(value) {
121131
isEmpty = false;
122132
result.push(
@@ -129,17 +139,6 @@
129139
}
130140
return '[' + newLine + result.join(',' + newLine) + newLine +
131141
(compact ? '' : oldIndent) + ']';
132-
} else if (!json && isRegExp(argument)) {
133-
return '/' + jsesc(
134-
evil(
135-
'\'' + argument.source.replace(regexEval, jsesc) + '\''
136-
),
137-
extend(options, {
138-
'wrap': false,
139-
'escapeEverything': false
140-
}
141-
)) + '/' + (argument.global ? 'g' : '') +
142-
(argument.ignoreCase ? 'i' : '') + (argument.multiline ? 'm' : '');
143142
} else if (!isObject(argument)) {
144143
if (json) {
145144
// For some values (e.g. `undefined`, `function` objects),
@@ -150,9 +149,9 @@
150149
} else { // it’s an object
151150
result = [];
152151
options.wrap = true;
153-
oldIndent = options.__indent;
152+
oldIndent = options.__indent__;
154153
indent += oldIndent;
155-
options.__indent = indent;
154+
options.__indent__ = indent;
156155
forOwn(argument, function(key, value) {
157156
isEmpty = false;
158157
result.push(
@@ -244,4 +243,4 @@
244243
root.jsesc = jsesc;
245244
}
246245

247-
}(this, eval));
246+
}(this));

src/jsesc.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*! http://mths.be/jsesc v<%= version %> by @mathias */
2-
;(function(root, evil) {
2+
;(function(root) {
33

44
// Detect free variables `exports`
55
var freeExports = typeof exports == 'object' && exports;
@@ -54,9 +54,6 @@
5454
// simple, but good enough for what we need
5555
return toString.call(value) == '[object Object]';
5656
};
57-
var isRegExp = function(value) {
58-
return toString.call(value) == '[object RegExp]';
59-
};
6057
var isString = function(value) {
6158
return typeof value == 'string' ||
6259
toString.call(value) == '[object String]';
@@ -83,6 +80,19 @@
8380
var regexDigit = /[0-9]/;
8481
var regexWhitelist = /<%= whitelist %>/;
8582

83+
var stringFromCharCode = String.fromCharCode;
84+
// Modified version of `ucs2encode`; see http://mths.be/punycode
85+
var codePointToSymbol = function(codePoint, strict) {
86+
var output = '';
87+
if (codePoint > 0xFFFF) {
88+
codePoint -= 0x10000;
89+
output += stringFromCharCode(codePoint >>> 10 & 0x3FF | 0xD800);
90+
codePoint = 0xDC00 | codePoint & 0x3FF;
91+
}
92+
output += stringFromCharCode(codePoint);
93+
return output;
94+
};
95+
8696
var jsesc = function(argument, options) {
8797
// Handle options
8898
var defaults = {
@@ -91,7 +101,7 @@
91101
'wrap': false,
92102
'compact': true,
93103
'indent': '\t',
94-
'__indent': '',
104+
'__indent__': '',
95105
};
96106
options = extend(defaults, options);
97107
if (options.quotes != 'single' && options.quotes != 'double') {
@@ -114,9 +124,9 @@
114124
if (isArray(argument)) {
115125
result = [];
116126
options.wrap = true;
117-
oldIndent = options.__indent;
127+
oldIndent = options.__indent__;
118128
indent += oldIndent;
119-
options.__indent = indent;
129+
options.__indent__ = indent;
120130
forEach(argument, function(value) {
121131
isEmpty = false;
122132
result.push(
@@ -129,17 +139,6 @@
129139
}
130140
return '[' + newLine + result.join(',' + newLine) + newLine +
131141
(compact ? '' : oldIndent) + ']';
132-
} else if (!json && isRegExp(argument)) {
133-
return '/' + jsesc(
134-
evil(
135-
'\'' + argument.source.replace(regexEval, jsesc) + '\''
136-
),
137-
extend(options, {
138-
'wrap': false,
139-
'escapeEverything': false
140-
}
141-
)) + '/' + (argument.global ? 'g' : '') +
142-
(argument.ignoreCase ? 'i' : '') + (argument.multiline ? 'm' : '');
143142
} else if (!isObject(argument)) {
144143
if (json) {
145144
// For some values (e.g. `undefined`, `function` objects),
@@ -150,9 +149,9 @@
150149
} else { // it’s an object
151150
result = [];
152151
options.wrap = true;
153-
oldIndent = options.__indent;
152+
oldIndent = options.__indent__;
154153
indent += oldIndent;
155-
options.__indent = indent;
154+
options.__indent__ = indent;
156155
forOwn(argument, function(key, value) {
157156
isEmpty = false;
158157
result.push(
@@ -244,4 +243,4 @@
244243
root.jsesc = jsesc;
245244
}
246245

247-
}(this, eval));
246+
}(this));

tests/tests.js

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -206,30 +206,6 @@
206206
'["\\u0066\\u006F\\u006F\\u0000\\u0062\\u0061\\u0072\\uFFFD\\u0062\\u0061\\u007A","\\u0066\\u006F\\u006F\\u0000\\u0062\\u0061\\u0072\\uFFFD\\u0062\\u0061\\u007A"]',
207207
'JSON-stringifying a flat array with `escapeEverything: true`'
208208
);
209-
equal(
210-
jsesc(/foo©bar𝌆[a-z0-9]ö/ig),
211-
'/foo\\xA9bar\\uD834\\uDF06[a-z0-9]\\xF6/gi',
212-
'Escaping a regular expression'
213-
);
214-
equal(
215-
jsesc(/foo\xA9bar\uD834\uDF06[a-z0-9]\xF6/ig),
216-
'/foo\\xA9bar\\uD834\\uDF06[a-z0-9]\\xF6/gi',
217-
'Escaping an already-escaped regular expression'
218-
);
219-
equal(
220-
jsesc(/foo©bar𝌆[a-z0-9]ö/, {
221-
'escapeEverything': true // should ignore the setting for the regex source part
222-
}),
223-
'/foo\\xA9bar\\uD834\\uDF06[a-z0-9]\\xF6/',
224-
'Escaping a regular expression with `escapeEverything: true`'
225-
);
226-
equal(
227-
jsesc(['abc', /foo©bar𝌆[a-z0-9]ö/mig], {
228-
'escapeEverything': true // should ignore the setting for the regex source part
229-
}),
230-
'[\'\\x61\\x62\\x63\',/foo\\xA9bar\\uD834\\uDF06[a-z0-9]\\xF6/gim]',
231-
'Escaping an array containing a regular expression with `escapeEverything: true`'
232-
);
233209
});
234210

235211
if (runExtendedTests) {
@@ -305,31 +281,32 @@
305281
var testArray = [
306282
undefined, Infinity, new Number(Infinity), -Infinity,
307283
new Number(-Infinity), 0, new Number(0), -0, new Number(-0), +0,
308-
new Number(+0), /foo[z-©]/g, new RegExp(/foo[z-©]/g), new Function(),
309-
'str', function zomg() { return 'desu'; }, null, true, new Boolean(true),
310-
false, new Boolean(false),
311-
{ "foo": 42, "baz": /löl/g, "hah": [ 1, 2, 3, { "foo" : 42 } ] }
284+
new Number(+0), new Function(), 'str',
285+
function zomg() { return 'desu'; }, null, true, new Boolean(true),
286+
false, new Boolean(false), {
287+
"foo": 42, "hah": [ 1, 2, 3, { "foo" : 42 } ]
288+
}
312289
];
313290
equal(
314291
jsesc(testArray, {
315292
'json': false
316293
}),
317-
'[undefined,Infinity,Infinity,-Infinity,-Infinity,0,0,0,0,0,0,/foo[z-\\xA9]/g,/foo[z-\\xA9]/g,function anonymous() {\n\n},\'str\',function zomg() { return \'desu\'; },null,true,true,false,false,{\'foo\':42,\'baz\':/l\\xF6l/g,\'hah\':[1,2,3,{\'foo\':42}]}]',
294+
'[undefined,Infinity,Infinity,-Infinity,-Infinity,0,0,0,0,0,0,function anonymous() {\n\n},\'str\',function zomg() { return \'desu\'; },null,true,true,false,false,{\'foo\':42,\'hah\':[1,2,3,{\'foo\':42}]}]',
318295
'Escaping a non-flat array with all kinds of values'
319296
);
320297
equal(
321298
jsesc(testArray, {
322299
'json': true
323300
}),
324-
'[null,null,null,null,null,0,0,0,0,0,0,{},{},null,"str",null,null,true,true,false,false,{"foo":42,"baz":{},"hah":[1,2,3,{"foo":42}]}]',
301+
'[null,null,null,null,null,0,0,0,0,0,0,null,"str",null,null,true,true,false,false,{"foo":42,"hah":[1,2,3,{"foo":42}]}]',
325302
'Escaping a non-flat array with all kinds of values, with `json: true`'
326303
);
327304
equal(
328305
jsesc(testArray, {
329306
'json': true,
330307
'compact': false
331308
}),
332-
'[\n\tnull,\n\tnull,\n\tnull,\n\tnull,\n\tnull,\n\t0,\n\t0,\n\t0,\n\t0,\n\t0,\n\t0,\n\t{},\n\t{},\n\tnull,\n\t"str",\n\tnull,\n\tnull,\n\ttrue,\n\ttrue,\n\tfalse,\n\tfalse,\n\t{\n\t\t"foo": 42,\n\t\t"baz": {},\n\t\t"hah": [\n\t\t\t1,\n\t\t\t2,\n\t\t\t3,\n\t\t\t{\n\t\t\t\t"foo": 42\n\t\t\t}\n\t\t]\n\t}\n]',
309+
'[\n\tnull,\n\tnull,\n\tnull,\n\tnull,\n\tnull,\n\t0,\n\t0,\n\t0,\n\t0,\n\t0,\n\t0,\n\tnull,\n\t"str",\n\tnull,\n\tnull,\n\ttrue,\n\ttrue,\n\tfalse,\n\tfalse,\n\t{\n\t\t"foo": 42,\n\t\t"hah": [\n\t\t\t1,\n\t\t\t2,\n\t\t\t3,\n\t\t\t{\n\t\t\t\t"foo": 42\n\t\t\t}\n\t\t]\n\t}\n]',
333310
'Escaping a non-flat array with all kinds of values, with `json: true, compact: false`'
334311
);
335312
}

0 commit comments

Comments
 (0)